【问题标题】:Plot in subplot figure not showing未显示子图中的绘图
【发布时间】:2017-12-15 01:00:23
【问题描述】:

其中一个图形框中的图未显示。两个图都在一个图框内,但没有分别绘制在一个图框内。下面是我的意思的图像:

地块应该在每个图框之间划分。这是绘图的代码:

ax = fig.add_subplot(2,1,1)
cm = plt.get_cmap('gist_rainbow')
ax.set_color_cycle([cm(1.*i/NUM_COLORS_RIGHT) for i in range(NUM_COLORS_RIGHT)])
ax.grid(True)
plt.title('Right IRI data per mile for verification runs:')
plt.tick_params(axis='both', which='major', labelsize=8)
plt.hold(True)

ax = fig.add_subplot(2,1,2)
cm = plt.get_cmap('gist_rainbow')
ax.set_color_cycle([cm(1.*i/NUM_COLORS_LEFT) for i in range(NUM_COLORS_LEFT)])
ax.grid(True)
plt.title('Left IRI data per mile for verification runs:')
plt.tick_params(axis='both', which='major', labelsize=8)
plt.hold(True)

for i in range(NUM_COLORS_RIGHT):
    plt.subplot(2,1,1)
    ax.plot(dataR[i],linewidth=2)
    if dataR[i] != dataR[-1]:
        continue
    else: break

for d in range(NUM_COLORS_LEFT):
    plt.subplot(2,1,2)
    ax.plot(dataL[d],label = d,linewidth=2)
    ax.legend(loc='lower right',borderaxespad=-4,bbox_to_anchor=(1.062, 0.2),ncol=1)
    if dataL[d] != dataL[-1]:
        continue
    else: break

plt.show()
plt.close('all') 

因此,代码从另一个函数中获取数据,并将该数据放入右侧和左侧数据的列表列表中。变量 NUM_COLORS_RIGHT 和 NUM_COLORS_LEFT 分别表示每边列表中的列表数量。然后使用这些变量来确定应该用于绘制数据的颜色数量,以便在绘制不确定数量的文本文件时利用颜色图并防止重复使用颜色。

两个图都在底部,所以我需要将图从 dataR[I] 移到上图中,然后从图中移出 dataL[d]。

【问题讨论】:

    标签: python-2.7 matplotlib text


    【解决方案1】:

    您混合了两种 matplotlib 编码风格 - 面向对象的接口和状态机。您对两个子图都使用变量名称 ax,因此对第一个子图的引用丢失了,因此您无法使用 ax.plot 在其上绘图。

    要解决这个问题,您可以将两个轴称为彼此不同的东西:

    ax1 = fig.add_subplot(2,1,1)
    cm = plt.get_cmap('gist_rainbow')
    ax1.set_color_cycle([cm(1.*i/NUM_COLORS_RIGHT) for i in range(NUM_COLORS_RIGHT)])
    ax1.grid(True)
    ax1.set_title('Right IRI data per mile for verification runs:')
    ax1.tick_params(axis='both', which='major', labelsize=8)
    plt.hold(True)
    
    ax2 = fig.add_subplot(2,1,2)
    cm = plt.get_cmap('gist_rainbow')
    ax2.set_color_cycle([cm(1.*i/NUM_COLORS_LEFT) for i in range(NUM_COLORS_LEFT)])
    ax2.grid(True)
    ax2.set_title('Left IRI data per mile for verification runs:')
    ax2.tick_params(axis='both', which='major', labelsize=8)
    plt.hold(True)
    
    for i in range(NUM_COLORS_RIGHT):
        ax1.plot(dataR[i],linewidth=2)
        if dataR[i] != dataR[-1]:
            continue
        else: break
    
    for d in range(NUM_COLORS_LEFT):
        ax2.plot(dataL[d],label = d,linewidth=2)
        ax2.legend(loc='lower right',borderaxespad=-4,bbox_to_anchor=(1.062, 0.2),ncol=1)
        if dataL[d] != dataL[-1]:
            continue
        else: break
    
    plt.show()
    plt.close('all') 
    

    如果您不想命名两个子图,可以使用plt.subplot 再次激活相关子图,但随后您需要使用plt.plot 而不是ax.plot

    ax = fig.add_subplot(2,1,1)
    cm = plt.get_cmap('gist_rainbow')
    ax.set_color_cycle([cm(1.*i/NUM_COLORS_RIGHT) for i in range(NUM_COLORS_RIGHT)])
    ax.grid(True)
    plt.title('Right IRI data per mile for verification runs:')
    plt.tick_params(axis='both', which='major', labelsize=8)
    plt.hold(True)
    
    ax = fig.add_subplot(2,1,2)
    cm = plt.get_cmap('gist_rainbow')
    ax.set_color_cycle([cm(1.*i/NUM_COLORS_LEFT) for i in range(NUM_COLORS_LEFT)])
    ax.grid(True)
    plt.title('Left IRI data per mile for verification runs:')
    plt.tick_params(axis='both', which='major', labelsize=8)
    plt.hold(True)
    
    for i in range(NUM_COLORS_RIGHT):
        plt.subplot(2,1,1)
        plt.plot(dataR[i],linewidth=2)
        if dataR[i] != dataR[-1]:
            continue
        else: break
    
    for d in range(NUM_COLORS_LEFT):
        plt.subplot(2,1,2)
        plt.plot(dataL[d],label = d,linewidth=2)
        plt.legend(loc='lower right',borderaxespad=-4,bbox_to_anchor=(1.062, 0.2),ncol=1)
        if dataL[d] != dataL[-1]:
            continue
        else: break
    
    plt.show()
    plt.close('all') 
    

    请注意,第二个示例中的行为将在未来版本的 matplotlib 中被弃用,因此第一种方法可能是最安全的。

    MatplotlibDeprecationWarning:使用与先前轴相同的参数添加轴当前重用早期实例。在未来的版本中,将始终创建并返回一个新实例。同时,可以通过将唯一标签传递给每个坐标区实例来抑制此警告,并确保未来的行为。

    【讨论】:

    • 哇,这很有意义。我现在就试试看
    猜你喜欢
    • 1970-01-01
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    相关资源
    最近更新 更多