Stefano Potter
Stefano Potter

Reputation: 3577

Subplots with Pandas

I want to create a figure with six subplots in pandas, which is 3x3. I am plotting based on columns in a dataframe.

I am trying something like this:

fig=plt.figure()
ax1=fig.add_subplot(3,3,1)
ax2=fig.add_subplot(3,3,2)
ax3=fig.add_subplot(3,3,3)
ax4=fig.add_subplot(3,3,4)
ax5=fig.add_subplot(3,3,5)
ax6=fig.add_subplot(3,3,6)

ax1=df.plot(x='bnw_Value', y='bnw_Percent', title='BNW', kind='bar')
ax1.set_xlabel('Value')
ax1.set_ylabel('Percent')
ax1.legend_.remove()
ax1.set_yticks([0,5,10,20,25])
ax2=df.plot(x='php_Value', y='php_Percent', title='PHP', kind='bar')
ax2.set_xlabel('Value')
ax2.set_ylabel('Percent')
ax2.legend_.remove()
ax2.set_yticks([0,5,10,20,25])

and then I do this for the other four plots as well with different x and y values.

but this doesn't actually plot to my subplots, and instead plots to individual plots. How would I actually plot to the subplots I create in the beginning?

Upvotes: 1

Views: 4465

Answers (1)

Stop harming Monica
Stop harming Monica

Reputation: 12610

You need to tell df.plot() which subplot you want to use:

df.plot(x='bnw_Value', y='bnw_Percent', title='BNW', kind='bar', ax=ax1)
df.plot(x='php_Value', y='php_Percent', title='PHP', kind='bar', ax=ax2)

No need to assign since you already have a reference to the returned subplot object.

Upvotes: 2

Related Questions