Issue
I have two queries:
- I want to remove the empty bar from the bar graph (present in the first column).
- I have to use this graph in a PowerPoint presentation. How can I increase the height of the bar graph such that it fixes the height of the slide? I have tried to increase the height but it is not increasing any further. Is it possible? If not what are other options that I can try?
plt.figure(figsize=(40,20))
g = sns.catplot(x = 'Subject', y = 'EE Score',data = df , hue = 'Session',col='Grade',sharey = True,sharex = True,
hue_order=["2017-18", "2018-19", "2019-20"], kind="bar");
#plt.legend(bbox_to_anchor=(1, 1), loc=2)
g.set(ylim=(0, 100))
g.set_axis_labels("Subject", "EE Score")
ax = g.facet_axis(0,0)
for p in ax.patches:
ax.text(p.get_x() + 0.015,
p.get_height() * 1.02,
'{0:.1f}'.format(p.get_height()),
color='black', rotation='horizontal', size=12)
ax = g.facet_axis(0,1)
for p in ax.patches:
ax.text(p.get_x() + 0.015,
p.get_height() * 1.02,
'{0:.1f}'.format(p.get_height()),
color='black', rotation='horizontal', size=12)
ax = g.facet_axis(0,2)
for p in ax.patches:
ax.text(p.get_x() + 0.015,
p.get_height() * 1.02,
'{0:.1f}'.format(p.get_height()),
color='black', rotation='horizontal', size=12)
ax = g.facet_axis(0,3)
for p in ax.patches:
ax.text(p.get_x() + 0.015,
p.get_height() * 1.02,
'{0:.1f}'.format(p.get_height()),
color='black', rotation='horizontal', size=12)
#g.set_ylabel('')
plt.savefig('2.png', bbox_inches = 'tight')
Solution
Like @JohanC, I initially thought it was not possible to remove an empty category from a catplot()
. However, Michael's comment provides the solution: sharex=False
.
Notice however how the colors get shifted between the First and Second, Third class. If you want to have consistent colors, you should pass a color=
argument.
titanic = sns.load_dataset('titanic')
# remove one category
titanic.drop(titanic.loc[(titanic['class']=='First')&(titanic['who']=='child')].index, inplace=True)
g = sns.catplot(x="who", y="survived", col="class", data=titanic, kind="bar", ci=None, sharex=False)
Answered By - Diziet Asahi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.