0

I have tried multiple codes to condition the bar plot colour to a particular value. It seems that the color function only checks for the first item in the index (in this case Germany) and sets the condition for all other items in the index. I would really appreciate if anybody could help:

colors = ['red' if 'Germany' else 'lightgrey' for x in first5_countries.index] #it colors all bars red
colors = ['r' if 'IT' else 'b' for index in first5_countries.index] #it colors everything red
colors = ['r' if pop_mln>85 else 'b' for pop_mln in first5_countries.pop_mln] #all bars blue
colors = ['r' if index=='Italy' else 'b' for index in first5_countries.index] #all bars blue
colors = ['b', 'b', 'b', 'r', 'b'] #yields blue

The whole code:

sorted_df = population_2019.sort_values(by='pop_mln', ascending=False)
first5_countries = sorted_df[:5]
colors = ['r' if index=='Italy' else 'b' for index in first5_countries.index]
first5_countries[['pop_mln']].plot.bar(figsize=(20,5), legend=False, color=colors)
plt.ylabel('Total population (in million)', size=12)
plt.xticks(rotation=30, ha='right')
plt.xlabel('')
plt.grid(axis='y')
plt.show()

Printout of first5_countries:

    geo sex age year    total_pop   pop_mln
geo_full                        
Germany DE  T   TOTAL   2019    83019213.0  83.019213
France  FR  T   TOTAL   2019    67012883.0  67.012883
United Kingdom  UK  T   TOTAL   2019    66647112.0  66.647112
Italy   IT  T   TOTAL   2019    60359546.0  60.359546
Spain   ES  T   TOTAL   2019    46937060.0  46.937060

population

first5_countries.index.values

array(['Germany', 'France', 'United Kingdom', 'Italy', 'Spain'], dtype=object)

2
  • Try color = ['lightgrey', 'lightgrey', 'lightgrey', 'red', 'lightgrey']. Or just create a color variable list before. Commented Jul 16, 2020 at 18:51
  • @SergeyDyshko I tried this method but it also does not work - I assigned the first column with country names as an index and it does not seem to be picked up when called out :( Commented Jul 17, 2020 at 9:46

1 Answer 1

1

You can define your colors like this:

colors = ['red' if x=='Italy' else 'lightgray' for x in first5_countries.index]

And then pass to the plot function:

first5_countries['population_mln'].plot.bar(figsize=(20,5),color=colors, legend=False)

Together, you would do:

colors = ['red' if x=='Italy' else 'lightgray' for x in first5_countries.index]

first5_countries['pop_mln'].plot.bar(figsize=(20,5),color=colors, legend=False)

Output would be something like this:

enter image description here

Sign up to request clarification or add additional context in comments.

7 Comments

That's useful, thank you but the function does not seem to pick up the country name Italy (nor any other). The function does something weird, eg. colors = ['red' if x=='Germany' else 'yellow' for x in first5_countries.index] returns every bar in red while colors = ['lightgrey' if x=='Italy' else 'yellow' for x in first5_countries.index] returns everything in yellow
Replace first5_countries.index with first5_countries['country_col'] where country_col is the name of your country column.
Unfortunately the column name 'geo_full' is not recognised, I previously set that column as an index column with demo_pjangroup = demo_pjangroup.set_index('geo_full') - the column includes all country names
It also does not work with any other condition, eg. colors = ['lightgrey' if x>60 else 'red' for x in first5_countries['population_mln']]
@Magdalena can you update your post with a printout of first5_countries?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.