0

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.

Kiki
  • 1
  • 1
  • 1
    Reading the next [question](https://stackoverflow.com/questions/53531429/valueerror-invalid-rgba-argument-what-is-causing-this-error) (which looks similar) seems the problem was generated because the length of the colors list was not the same than the data provided. So, I suggest you to double-check that the list of colors length is equals to the number of bars you want to print. – edgarzamora Nov 10 '19 at 23:49
  • I was trying to assign a color for all of the states which is where I was running into an error. I ended up adding another column to my data and grouping the states per region, then assigning the color per region. Thanks for the help! – Kiki Nov 11 '19 at 05:48

0 Answers0