使用 matplotlib 在单独的子图中绘制熊猫系列

Plot panda series in separate subplots using matplotlib

希望得到一些帮助,我正在尝试使用 pandas 和 matplotlib 在单独的子图中绘制模拟数据,到目前为止我的代码是:

import matplotlib.pylab as plt
import pandas as pd
fig, ax = plt.subplots(2, 3)
for i in range(2):
    for j in range(50, 101, 10):
        for e in range(3):
            Var=(700* j)/ 100
            Names1 = ['ig','M_GZ']
            Data1 = pd.read_csv('~/File/JTL_'+str(Var)+'/GZ.csv', names=Names1)
            ig = Data1['ig']
            M_GZ=Data1['M_GZ']
            MGZ = Data1[Data1.M_GZ != 0]
            ax[i, e].plot(MGZ['ig'][:4], MGZ['M_GZ'][:4], '--v', linewidth=1.75)
plt.tight_layout()
plt.show()

但代码为我提供了同一图的 6 个重复副本: 我尝试更改循环并使用不同的变体,而不是 Var 的每次迭代都有自己的情节:

fig = plt.figure()
for i in range(1, 7):
      ax = fig.add_subplot(2, 3, i)
            for j in range(50, 101, 10):                
                     Var=(700* j)/ 100
                     Names1 = ['ig','M_GZ']
                     Data1 = pd.read_csv('~/File/JTL_'+str(Var)+'/GZ.csv', names=Names1)
                     ig = Data1['ig']
                     M_GZ=Data1['M_GZ']
                     MGZ = Data1[Data1.M_GZ != 0]
                     ax.plot(MGZ['ig'][:4], MGZ['M_GZ'][:4], '--v', linewidth=1.75)
plt.tight_layout()
plt.show()

但这并没有改变我仍然得到与上面相同的情节。任何帮助将不胜感激,我希望每个子图包含一组数据而不是所有六个

这是一个 Link Dataframes 之一,每个子目录 ~/File/JTL_'+str(Var)+'/ 包含此文件的副本,总共有 6 个

问题出在你的循环中

for i in range(2):  # Iterating rows of the plot
    for j in range(50, 101, 10): # Iterating your file names
        for e in range(3): # iterating the columns of the plot

最终结果是您迭代了每个文件名

的所有列

对于两个工作,您的循环中应该只有两个嵌套级别。潜在代码(已更新):

import matplotlib.pylab as plt
import pandas as pd
fig, ax = plt.subplots(2, 3)
for row in range(2):
    for col in range(3):
        f_index = range(50, 101, 10)[row+1 * col]
        print row, col, f_index
        Var=(700* f_index)/ 100
        Names1 = ['ig','M_GZ']
        Data1 = pd.read_csv('~/File/JTL_'+str(Var)+'/GZ.csv', names=Names1)
        ig = Data1['ig']
        M_GZ=Data1['M_GZ']
        MGZ = Data1[Data1.M_GZ != 0]
        ax[row, col].plot(MGZ['ig'][:4], MGZ['M_GZ'][:4], '--v',linewidth=1.75)
plt.tight_layout()
plt.show()