通过Matplotlib子图迭代循环

时间:2018-01-12 06:00:58

标签: python matplotlib

我想迭代matplotlib子图,在每个子图中绘制一条跟踪tr。但是,我目前得到的输出在每个子图中绘制了所有8条迹线。如何将一条迹线绘制到自己的子图中?我怀疑它与我的for循环索引有关,但我似乎无法确定这个问题所在。感谢。

简化代码如下:

traces = glob.glob('*.data')
fig,ax = plt.subplots(nrows=4, ncols=2)
index = 0 
for index, ix in enumerate(traces): 
    tr = obspy.read(traces[index])[0]

    *do stuff* 

     for i in range(4):
        for j in range(2):
            ax[i, j].plot(time,tr.data, color='k')
            index+=1

编辑:下面的代码已经解决了这个问题。

traces = glob.glob('*.data')
fig,axes = plt.subplots(nrows=4, ncols=2)
index = 0 
for tr, ax in zip(traces, axes.reshape(-1)):
    ** do stuff **
    tr = obspy.read(traces[index])[0]
    ax.plot(time,tr_trim.data, color='k')
    ax.set_ylabel("Raw Data")
    ax.set_xticks([]);
    index +=1
plt.show()

0 个答案:

没有答案
相关问题