Nba app to pull up and visualize scores via heatmap
To upload files, please first save the app
import streamlit as st
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import numpy as np
from datetime import datetime, timedelta
st.title("NBA Scores Heatmap Visualization")
# Sample data generation (in real app, this would come from an API)
teams = [
"Celtics", "Nets", "Knicks", "76ers", "Raptors",
"Bulls", "Cavaliers", "Pistons", "Pacers", "Bucks",
"Hawks", "Hornets", "Heat", "Magic", "Wizards",
"Nuggets", "Timberwolves", "Thunder", "Blazers", "Jazz",
"Warriors", "Clippers", "Lakers", "Suns", "Kings",
"Mavericks", "Rockets", "Grizzlies", "Pelicans", "Spurs"
]
# Create sample scores data
def generate_sample_data():
data = []
current_date = datetime.now() - timedelta(days=30)
for _ in range(300): # Generate last 30 days of games
team1, team2 = np.random.choice(teams, size=2, replace=False)
score1 = np.random.randint(85, 135)
score2 = np.random.randint(85, 135)
data.append({
'Date': current_date.strftime('%Y-%m-%d'),
'Team1': team1,
'Team2': team2,
'Score1': score1,
'Score2': score2
})
current_date += timedelta(days=1)
return pd.DataFrame(data)
# Generate and display the data
df = generate_sample_data()
# Create team performance matrix
performance_matrix = pd.DataFrame(0, index=teams, columns=teams)
for _, game in df.iterrows():
team1, team2 = game['Team1'], game['Team2']
score1, score2 = game['Score1'], game['Score2']
performance_matrix.loc[team1, team2] = score1
performance_matrix.loc[team2, team1] = score2
# Create heatmap
st.subheader("NBA Scores Heatmap")
st.write("This heatmap shows the scoring patterns between teams. Darker colors indicate higher scores.")
fig, ax = plt.subplots(figsize=(15, 12))
sns.heatmap(performance_matrix,
cmap='YlOrRd',
annot=True,
fmt='.0f',
cbar_kws={'label': 'Points Scored'},
square=True)
plt.xticks(rotation=45, ha='right')
plt.yticks(rotation=0)
plt.title('NBA Team Scoring Matrix')
st.pyplot(fig)
# Display recent games
st.subheader("Recent Games")
st.dataframe(df.sort_values('Date', ascending=False).head(10))
# Basic statistics
st.subheader("Team Statistics")
team_stats = pd.DataFrame()
for team in teams:
team_games = df[(df['Team1'] == team) | (df['Team2'] == team)]
total_points = (
team_games[team_games['Team1'] == team]['Score1'].sum() +
team_games[team_games['Team2'] == team]['Score2'].sum()
)
games_played = len(team_games)
avg_points = total_points / games_played if games_played > 0 else 0
team_stats = pd.concat([team_stats, pd.DataFrame({
'Team': [team],
'Games Played': [games_played],
'Total Points': [total_points],
'Average Points': [avg_points]
})])
team_stats = team_stats.sort_values('Average Points', ascending=False).reset_index(drop=True)
st.dataframe(team_stats)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?