Home > Back-end >  .map is not mapping correct color values to a barchart
.map is not mapping correct color values to a barchart

Time:06-08

I am producing a series of barcharts, counting the values present in the column df['Journal Type'].

In order to keep the color consistent, I am using a dictionary which map all the unique values in the column with a specific color code.

Here's the code:

color_dict = {'Architecture':'#45173C' , 'Music':'#2A2060' , 'Decorative Art':'#3E4DBB' , 'Humour' :'#EBD2C2', 'Bibliophily' : '#121E36',
       'War' :'#4448C1', 'Modern Art Journal' :'#A3E0AD', 'Avant-garde Journal' :'#327728', 'Art History' :'#216356',
       'Art Journal' :'#3E6722', 'Design' :'#8C40BF', 'Urbanism' :'#A6D279', 'History' :'#3E4DBB',
       'Illustrated Supplement' :'#6B4C24', 'News' :'#C75778', 'Culture, Leisure' :'#121E36',
       'Regional Magazine' :'#ABE3D6', 'Leisure' :'#759AD1', 'Photography' :'#D4C47D', 'Politics' :'#D890DA',
       'Worldliness' :'#D7CDEE', 'Science' :'#D1ECC6', 'Exhibition Catalogue' :'#5BC870', 'Regional News' :'red',
       'Youth' :'#CDC36A', 'Fashion' :'#75D1B3', 'Cinema' :'#24516B', 'Sports' :'#4799C2', 'Children' :'#C6E8BA', 'Automobile' :'#9091DA',
       'Colonial' :'#86712D', 'Aviation' :'#D17D75', 'Religion' :'#EECDEB', 'Industry' :'#A3E0D5', 'Crimes' :'#D27979',
       'Theater' :'#D6A585', 'Woman' :'#CCC9ED', 'Military' :'#C8CF6E', 'Philanthropy' :'#3DB8A3', 'Black Lives' :'#5C3C1F',
       'Illustrated Magazine' :'#B6C5E7', 'Economics': '#D09671'}

df['Journal Type'].value_counts().plot(kind='bar', figsize=(15, 15), color= df['Journal Type'].map(color_dict))

The result however, does not really take into account all the colors specified in the vocabulary, but (it appears) only the very first ones, as it is possible to all.png

The first columns, for example, are all mapped as #45173C, even though it is not at all the right color. Any idea what is going on?

This is a list of unique values of df['Journal Type']:

array(['Architecture', 'Music', 'Decorative Art', 'Humour', 'Bibliophily',
       'War', 'Modern Art Journal', 'Avant-garde Journal', 'Art History',
       'Art Journal', 'Design', 'Urbanism', 'History',
       'Illustrated Supplement', 'News', 'Culture, Leisure',
       'Regional Magazine', 'Leisure', 'Photography', 'Politics',
       'Worldliness', 'Science', 'Exhibition Catalogue', 'Regional News',
       'Youth', 'Fashion', 'Cinema', 'Sports', 'Children', 'Automobile',
       'Colonial', 'Aviation', 'Religion', 'Industry', 'Crimes',
       'Theater', 'Woman', 'Military', 'Philanthropy', 'Black Lives',
       'Illustrated Magazine', 'Economics'], dtype=object)

CodePudding user response:

The answer of @gloo works perfectly. In the meanwhile I managed to obtain the same result with seaborn. If anyone is interested, see the code below:

sns.set(rc={'figure.figsize':(11.7,8.27)})
colors = {'Architecture':'#45173C' , 'Music':'#2A2060' , 'Decorative Art':'#3E4DBB' , 'Humour' :'#EBD2C2', 'Bibliophily' : '#121E36',
       'War' :'#4448C1', 'Modern Art Journal' :'#A3E0AD', 'Avant-garde Journal' :'#327728', 'Art History' :'#216356',
       'Art Journal' :'#3E6722', 'Design' :'#8C40BF', 'Urbanism' :'#A6D279', 'History' :'#3E4DBB',
       'Illustrated Supplement' :'#6B4C24', 'News' :'#C75778', 'Culture, Leisure' :'#121E36',
       'Regional Magazine' :'#ABE3D6', 'Leisure' :'#759AD1', 'Photography' :'#D4C47D', 'Politics' :'#D890DA',
       'Worldliness' :'#D7CDEE', 'Science' :'#D1ECC6', 'Exhibition Catalogue' :'#5BC870', 'Regional News' :'red',
       'Youth' :'#CDC36A', 'Fashion' :'#75D1B3', 'Cinema' :'#24516B', 'Sports' :'#4799C2', 'Children' :'#C6E8BA', 'Automobile' :'#9091DA',
       'Colonial' :'#86712D', 'Aviation' :'#D17D75', 'Religion' :'#EECDEB', 'Industry' :'#A3E0D5', 'Crimes' :'#D27979',
       'Theater' :'#D6A585', 'Woman' :'#CCC9ED', 'Military' :'#C8CF6E', 'Philanthropy' :'#3DB8A3', 'Black Lives' :'#5C3C1F',
       'Illustrated Magazine' :'#B6C5E7', 'Economics': '#D09671'}

title = "Journal Type"


barchart = sns.countplot(y=df['Journal Type'], palette=colors)
barchart.axes.set_title(title,fontsize=20)
fig = barchart.get_figure()
fig.savefig("all.png") 

CodePudding user response:

Try the following:

journal_values = df['Journal Type'].value_counts()
journal_values.plot(kind='bar', figsize=(15, 15), color=[color_dict.get(key, "red") for key in journal_values.keys()])

value_counts() returns a Series containing counts of unique values, with the keys being the journal type and the value being the count of that journal type. df['Journal Type'].map(color_dict) returns a new Series of the corresponding colors over the indexes of df['Journal Type'], not the indexes of the value counts of df['Journal Type'].

  • Related