I am creating an animated race bar chart in a Jupyter notebook/Python adapted from this example. I would like to assign a color to each column for each state. It currently assigns the column a color, but as I run the bar chart and the state names change based on number of aircraft, the color order remains the same. The example I am using includes a grouping step that I do not need, so I have assigned colors using the following:
colors = ["#adb0ff", "#ffb3ff", "#90d595", "#e48381", "#aafbff", "#f7bb5f", "#eafb50"]
I have tried to add the state IDs and then colors, but receive "Invalid RGBA argument" errors when I assign colors using the code below. States and colors in the examples below are shortened.
Example 1
colors = dict(zip(
['AL', 'AK', 'AZ'],
["#adb0ff", "#ffb3ff", "#90d595"]
))
group = df.set_index('Aircraft').to_dict()
Example 2
colors =(
['AL', 'AK', 'AZ'],
['#adb0ff', '#ffb3ff', '#90d595']
)
Here is the code I am using.
fig, ax = plt.subplots(figsize=(15, 8))
dff = dff[::-1]
ax.barh(dff['State'], dff['Aircraft'], color=colors)
for i, (Aircraft, State) in enumerate(zip(dff['Aircraft'], dff['State'])):
ax.text(Aircraft, i, State, ha='right')
ax.text(Aircraft, i, Aircraft, ha='left')
ax.text(1, 0.4, current_year, transform=ax.transAxes, size=46, ha='right')
Thank you for your help.