1

I would like to create a list where i can add in English speaking countries and make sure that reflects on my barplot

top9 = master_frame[master_frame['country_code'].isin(["USA","CHN","GBR","IND","CAN","ISR","FRA","DEU","SWE"])]

plt.figure(figsize=(10,5))
ax2=sns.barplot(x='country_code', y='raised_amount_usd', data=top9, estimator=np.sum,)
ax2.set_yscale('log')
ax2.set(xlabel='Funding by country', ylabel='Total amount of investments')
ax2.set_title('Investment distribution Country wise',fontsize =18, weight='bold')
plt.show()

Output:

barplot image

Here the challenge is that i need to highlight countries which have English as an official language and so my thought was to add a hash pattern to the bars of the countries and show a hue box above.

Any idea how i can achieve this??

Thank you in advance!

JohanC
  • 71,591
  • 8
  • 33
  • 66
user10528004
  • 35
  • 1
  • 2
  • 7

1 Answers1

1

I am not quite sure about the hash pattern you mentioned.

Here is my understanding, you want to achieve two things.

First, color the bars having different property.

Second, create a legend box on the plot.

The approach below is way to fulfill your purpose.


1. Color the bars.

This step can be easily done with setting up the parameter palette in the function sns.barplot. You shall input a sequence of colors into it in hex format (there also lots of other ways exist).

color_eng = "#94698b"
color_non_eng = "#4369ef"

# the sequence length is the number of bars.
palette = [color_eng, color_eng, color_non_eng, color_eng, color_non_eng]

ax2 = sns.barplot(x='country_code', y='raised_amount_usd', 
                  data=top9, estimator=np.sum, palette=palette)

2. Create a legend.

To create a customized legend box, we could set up the handles of the axes with manually created patches (I borrowed the code idea from this SO answer).

import matplotlib.patches as mpatches

p_eng = mpatches.Patch(color=color_eng, label='English speaking country')
p_non_eng = mpatches.Patch(color=color_non_eng, label='non-English speaking country')

ax2.legend(handles=[p_eng, p_non_eng])

The final code shall looks like:

import matplotlib.patches as mpatches

top9 = master_frame[master_frame['country_code'].isin(["USA","CHN","GBR","IND","CAN","ISR","FRA","DEU","SWE"])]

plt.figure(figsize=(10,5))

color_eng = "#94698b"
color_non_eng = "#4369ef"

# the sequence length is the number of bars.
# Shall be composed by you
palette = [color_eng, color_eng, color_non_eng, color_eng, .......]

ax2 = sns.barplot(x='country_code', y='raised_amount_usd', 
                  data=top9, estimator=np.sum, palette=palette)

p_eng = mpatches.Patch(color=color_eng, label='English speaking country')
p_non_eng = mpatches.Patch(color=color_non_eng, label='non-English speaking country')

ax2.legend(handles=[p_eng, p_non_eng])

ax2.set_yscale('log')
ax2.set(xlabel='Funding by country', ylabel='Total amount of investments')
ax2.set_title('Investment distribution Country wise',fontsize =18, weight='bold')
plt.show()

jadore801120
  • 77
  • 1
  • 3
  • i have tried this and i get the following error: `ValueError: Invalid RGBA argument: '4369ef' ValueError: Could not generate a palette for
    `
    – user10528004 Aug 31 '20 at 06:59