0

so I'm trying to annotate every bar with the corresponding values from the 'Change' column in df. And if the change is negative i want to have it red color otherwise green. Someone has idea how to do that? I was thinking of doing it with annotate function, so that it is possible to format box styles further, however if that is not really doable for this case then alright. The numbers would be placed on the right side of the bars.

games = pd.read_csv('Video_Games_Sales_as_at_22_Dec_2016.csv')
gbypub=games.Publisher.dropna().value_counts()
gnodupl=games.drop_duplicates(subset='Name')
gbpndupl=gnodupl.Publisher.dropna().value_counts() 
df1=pd.DataFrame(gbypub)
df2=pd.DataFrame(gbpndupl)
df1['Position'] = range(1, len(df1) + 1)
df2['Position'] = range(1, len(df2) + 1)
df2['Change']=df1['Position']-df2['Position']
pd.options.display.float_format = '{:,.0f}'.format
df=df2.iloc[:40]
ax = plt.figure(figsize=(5, 15))
sns.barplot(x=df['Publisher'], y=df.index, data=df, palette='summer')
plt.ylabel('Number of titles per studio', size=15)
plt.xlabel('Studios', size=14)

I used the method below:

for p in ax.patches:
    ax.annotate(str(p.get_height()), xy=(p.get_x(), p.get_height()))

however im not sure how can I apply it so it uses the data from the dataframe column that i dont plot on the ax currently, so therefore that code doesnt annotate anything.

The dataset I use: https://www.kaggle.com/rush4ratio/video-game-sales-with-ratings

So I came up to this point, and Im stuck: There are two things that I still cant figure out: which is how to make the text also change colors like bars, and how to put different column from dataframe as a annotation instead of the value. I managed to be able to add just one value from that column but the purpose is to get corresponding value from that column for every bar:

ax = plt.figure(figsize=(5, 15))


# Create example DataFrame

# Create list of colors based on a condition 
colors = ['red' if (x < 0) else 'green' for x in df['Change']]

# Create barplot 
ax = sns.barplot(x=df['Publisher'], y=df.index, data=df, palette=colors)
# Annotate every single Bar with its value, based on it's width           

for p in ax.patches:

    width = p.get_width()


    plt.text(60+p.get_width(), p.get_y()+0.55*p.get_height(),
                     df['Change'][0],
                     ha='center', va='center')
Alex T
  • 3,529
  • 12
  • 56
  • 105
  • http://stackoverflow.com/questions/19917587/matplotlib-advanced-bar-plot/19919397#19919397 – Paul H Mar 16 '17 at 16:12
  • @PaulH followed the link, favorited and +1 – piRSquared Mar 16 '17 at 16:57
  • @PaulH I tried to follow this solution but i quite dont understand why I apply this annotate bars function to dataframe and how would it be visualized? Because typically annotations are made on ax. or directly with matplotlib, so why there it is used on dataframe and how would that be visualized in my case? – Alex T Mar 17 '17 at 12:54
  • Take the time to really read it again. My solutions uses a function that direct acts on Axes objects. – Paul H Mar 17 '17 at 15:40
  • @PaulH Okay actually I understand this one, now I'm trying to convert it so that it works for horizontal barplots. Well the funny thing is that kernel died after I almost managed to do it :D, you might wanna check it here http://stackoverflow.com/questions/42861049/ipython-kernel-dies-when-running-the-code?noredirect=1#comment72829305_42861049 – Alex T Mar 17 '17 at 15:42
  • Possible duplicate of [Annotate bars with values on Pandas bar plots](http://stackoverflow.com/questions/25447700/annotate-bars-with-values-on-pandas-bar-plots) – ImportanceOfBeingErnest Apr 21 '17 at 09:58

0 Answers0