简体   繁体   English

与熊猫的子图

[英]Subplots with Pandas

I want to create a figure with six subplots in pandas, which is 3x3. 我想在熊猫中创建一个包含六个子图的图形,即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. 然后我为其他四个图表执行此操作以及不同的x和y值。

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? 我如何实际绘制到我在开始时创建的子图?

You need to tell df.plot() which subplot you want to use: 你需要告诉df.plot()你要使用哪个子图:

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. 无需分配,因为您已经有了对返回的子图对象的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM