Skip to main content
Version: main

Team colors

palette and team_colors give you colors keyed by team, so a chart can be colored without hand-picking hex codes.

import sdvplot
import polars as pl

df = pl.DataFrame({"team": ["KC", "BUF", "PHI", "DET"], "epa": [0.12, 0.10, 0.09, 0.15]})
df
teamepa
KC0.12
BUF0.1
PHI0.09
DET0.15

A palette for seaborn​

palette accepts a polars Series of abbreviations, so it can be passed straight to seaborn.

import matplotlib.pyplot as plt
import seaborn as sns

sns.barplot(
df.to_pandas(),
x="team",
y="epa",
hue="team",
palette=sdvplot.palette("nfl", teams=df["team"]),
legend=False,
)
plt.show()

png

One color per team​

team_colors returns one color per team, in the order given. which="secondary" picks the secondary color.

sdvplot.team_colors("nfl", df["team"], which="secondary")
shape: (4,)
Series: 'team' [str]
[
"#ffb612"
"#c60c30"
"#a5acaf"
"#b0b7bc"
]

Other libraries​

The palette is a plain dict of team -> hex, so other libraries take it directly (not executed here).

Plotly:

import plotly.express as px

px.bar(df.to_pandas(), x="team", y="epa", color="team",
color_discrete_map=sdvplot.palette("nfl", teams=df["team"]))

Altair:

import altair as alt

p = sdvplot.palette("nfl", teams=df["team"])
alt.Chart(df.to_pandas()).mark_bar().encode(
x="team", y="epa", color=alt.Color("team", scale=alt.Scale(domain=list(p), range=list(p.values()))),
)

Where a team's colors come from​

color_source says where each team's colors come from: nflverse or espn when a source publishes them, logo when they are the two dominant colors of the team's current logo (an approximation, for a team no source publishes colors for), and fallback for a placeholder from a colorblind-safe palette that only keeps teams apart. Check color_source before treating a color as a team's own. No source publishes OHL colors, so the colors below are read from each team's logo; every NFL team has colors from nflverse.

sdvplot.teams("ohl").select("team_id", "name", "color_primary", "color_source").head()
team_idnamecolor_primarycolor_source
1Brantford Bulldogs#ba8748logo
10Kitchener Rangers#233e95logo
11Owen Sound Attack#eec943logo
12Sudbury Wolves#a63039logo
13Flint Firebirds#0d1d41logo

Run it yourself​

Download the notebook (outputs cleared) or open it on GitHub.