I am working with seaborn, and trying to make my bar chart look better.
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
x = ['One', 'Two', 'Three', 'Four', 'Five']
y = [2, 3, 0, 4.5, 4]
y2 = [0, 0, -5, 0, 0]
sns.axes_style('white')
sns.set_style('white')
b = sns.barplot(x,y,color='pink')
sns.barplot(x,y2, color='red')
for p in b.patches:
b.annotate(
s='{:.1f}'.format(p.get_height()),
xy=(p.get_x()+p.get_width()/2.,p.get_height()),
ha='center',va='center',
xytext=(0,10),
textcoords='offset points'
)
b.set_yticks([])
sns.despine(ax=b, left=True, bottom=True)
I actually took code for labelling bars from another thread on stack overflow.
The problem I have that the bar that is negative being labeled on the positive side. I also want to get rid of zeros at the beginning of each graph, and possibly move x = ['One','Two','Three','Four','Five']
in the middle where zeros are, instead of it being on the bottom.
Here's the logic you need make only 1 call to barplot
and place the annotation on the correct side of the x-axis
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
x = ['One', 'Two', 'Three', 'Four', 'Five']
y = [2, 3, -5, 4.5, 4]
sns.axes_style('white')
sns.set_style('white')
colors = ['pink' if _y >=0 else 'red' for _y in y]
ax = sns.barplot(x, y, palette=colors)
for n, (label, _y) in enumerate(zip(x, y)):
ax.annotate(
s='{:.1f}'.format(abs(_y)),
xy=(n, _y),
ha='center',va='center',
xytext=(0,10),
textcoords='offset points',
color=color,
weight='bold'
)
ax.annotate(
s=label,
xy=(n, 0),
ha='center',va='center',
xytext=(0,10),
textcoords='offset points',
)
# axes formatting
ax.set_yticks([])
ax.set_xticks([])
sns.despine(ax=ax, bottom=True, left=True)
Thanks everyone for comments.
Here is my solution:
I have moved the negative number to the end of the list. (Doesn't make that big of a difference though)
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
x = ['One', 'Two', 'Three', 'Four', 'Five']
y = [2, 3, 4, 4.5, 0]
y2 = [0, 0, 0, 0, -5]
figure()
sns.axes_style('white')
b = sns.barplot(x,y2, color='red')
for z in b.patches[4:5]:
b.annotate(np.round(-z.get_height(),decimals=6),(z.get_x()+z.get_width()/2.,-z.get_height()),ha='center',va='center',xytext=(0,10),textcoords='offset points', color='w')
b=sns.barplot(x,y, color='blue')
for p in b.patches[5:9]:
b.annotate(np.round(p.get_height(),decimals=6),(p.get_x()+p.get_width()/2.,p.get_height()),ha='center',va='center',xytext=(0,-10),textcoords='offset points', color='w')
FYI: Final answer from AK9309 yields the following plot in my setting (python 2):