我想在 pandas 中创建一个包含 6 个子图的图形,大小为 3x3。我正在根据数据框中的列进行绘图。
我正在尝试这样的事情:
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])
然后我对其他四个图也使用不同的 x 和 y 值执行此操作。
但这实际上并没有绘制到我的子图上,而是绘制到了单独的图上。我实际上将如何绘制我在开始时创建的子图?
最佳答案
您需要告诉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)
无需分配,因为您已经拥有对返回的子图对象的引用。
关于python - Pandas 的次要情节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36433744/