CF84 CF84 - 14 days ago 10
Python Question

Pandas: set different ylim and titles for different subplots

Say I have this dataframe:

Type Cat1 Cat2 Cat3
0 A 0.384000 0.393000 0.458000
1 B 0.603337 0.381470 0.299773
2 C 0.585797 0.452570 0.317607
3 D 0.324715 0.765212 0.717755


That I plot like this (from here):

axes = df.set_index('Type').plot.bar(subplots=True, legend=False)
plt.subplots_adjust(hspace=0.35)


enter image description here

My problem: how can I set a different
ylim
for each subplot? And how can I modify things like the font size of the
Cat
titles?

Answer

the plotting method returns an array of axes in your axes variable. You can access each one and set the title and ylim.

axes = df.set_index('Type').plot.bar(subplots=True, legend=False)
plt.subplots_adjust(hspace=0.35)
axes[0].set_ylim(0, 1.1)
axes[0].set_title('hello')

enter image description here