微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

3 x 2 子图具有不同的 yaxis 和标题以及相同的 x 轴

如何解决3 x 2 子图具有不同的 yaxis 和标题以及相同的 x 轴

我想创建 3 x 2 的网格,以在一个子图中绘制每列的六列数据框。 每个子图应该有不同的标题。在 xaxis 上,我有索引,而 yaxis 是不同的。

我使用虚拟值创建的示例数据框。

import pandas as pd

data = {'Column 1':  [1,2,3,4,5,6],'Column 2': [10,20,30,40,50,60],'Column 3': [50,100,150,200,250,300],'Column 4':[1,'Column 5': [10,'Column 6': [50,300]
        }

Xnew = pd.DataFrame (data,columns = ['Column 1','Column 2','Column 3','Column 4','Column 5','Column 6'])

print (Xnew)

我试过下面的代码。我得到了六个大小不等的子图,折线图的颜色相同,标题不可见。一些子图的图形也被截断了。

fig = plt.figure() 

yaxis0 = Xnew['Column 1']
yaxis1 = Xnew['Column 2']
yaxis2 = Xnew['Column 3']
yaxis3 = Xnew['Column 4']
yaxis4 = Xnew['Column 5']
yaxis5 = Xnew['Column 6']

ax0 = fig.add_subplot(1,1) 
ax1 = fig.add_subplot(1,2) 
ax2 = fig.add_subplot(2,1)
ax3 = fig.add_subplot(2,2)
ax4 = fig.add_subplot(3,1)
ax5 = fig.add_subplot(3,2) 

# subplot 1: Box plot
yaxis0.plot(kind='line',ax=ax0) 
ax0.set_title('Quantity of Column 1 in compounds')
ax0.set_xlabel('ID')
ax0.set_ylabel('Column 1')

# subplot 2: Line plot
yaxis1.plot(kind='line',ax=ax1) 
ax1.set_title('Quantity of Column 2 in compounds')
ax1.set_xlabel('ID')
ax1.set_ylabel('Column 2')

yaxis2.plot(kind='line',ax=ax2) 
ax2.set_title('Quantity of Column 3 in compounds')
ax2.set_xlabel('ID')
ax2.set_ylabel('Column 3')

# subplot 2: Line plot
yaxis3.plot(kind='line',ax=ax3) 
ax3.set_title('Quantity of Column 4 in compounds')
ax3.set_xlabel('ID')
ax3.set_ylabel('Column 4')

yaxis4.plot(kind='line',ax=ax4) 
ax4.set_title('Quantity of Column 5 in compounds')
ax4.set_xlabel('ID')
ax4.set_ylabel('Column 5')

# subplot 2: Line plot
yaxis5.plot(kind='line',ax=ax5) 
ax5.set_title('Quantity of Column 6 in compounds')
ax5.set_xlabel('ID')
ax5.set_ylabel('Column 6')


plt.show()

不明白怎么回事。正确的做法是什么?

解决方法

我建议使用 plt.subplots() 创建子图网格 axes,然后迭代 axes[row][col]

nrows,ncols = 3,2
fig,axes = plt.subplots(nrows=nrows,ncols=ncols,figsize=(8,8))

for index,column in enumerate(Xnew.columns):
    row,col = index // ncols,index % ncols
    ax = axes[row][col]

    Xnew[column].plot(kind='line',ax=ax)
    ax.set_title(f'Quantity of {column} in compounds')
    ax.set_xlabel('ID')
    ax.set_ylabel(column)
    
plt.tight_layout()

plt.subplots() grid


非循环版本:

fig,axes = plt.subplots(nrows=3,ncols=2,8))

yaxis0 = Xnew['Column 1']
yaxis1 = Xnew['Column 2']
yaxis2 = Xnew['Column 3']
yaxis3 = Xnew['Column 4']
yaxis4 = Xnew['Column 5']
yaxis5 = Xnew['Column 6']

# Subplot 1
ax0 = axes[0][0]
yaxis0.plot(kind='line',ax=ax0) 
ax0.set_title('Quantity of Column 1 in compounds')
ax0.set_xlabel('ID')
ax0.set_ylabel('Column 1')

# Subplot 2
ax1 = axes[0][1]
yaxis1.plot(kind='line',ax=ax1) 
ax1.set_title('Quantity of Column 2 in compounds')
ax1.set_xlabel('ID')
ax1.set_ylabel('Column 2')

# Subplot 3
ax2 = axes[1][0]
yaxis2.plot(kind='line',ax=ax2) 
ax2.set_title('Quantity of Column 3 in compounds')
ax2.set_xlabel('ID')
ax2.set_ylabel('Column 3')

# Subplot 4
ax3 = axes[1][1]
yaxis3.plot(kind='line',ax=ax3) 
ax3.set_title('Quantity of Column 4 in compounds')
ax3.set_xlabel('ID')
ax3.set_ylabel('Column 4')

# Subplot 5
ax4 = axes[2][0]
yaxis4.plot(kind='line',ax=ax4) 
ax4.set_title('Quantity of Column 5 in compounds')
ax4.set_xlabel('ID')
ax4.set_ylabel('Column 5')

# Subplot 6
ax5 = axes[2][1]
yaxis5.plot(kind='line',ax=ax5) 
ax5.set_title('Quantity of Column 6 in compounds')
ax5.set_xlabel('ID')
ax5.set_ylabel('Column 6')

plt.tight_layout()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?