在子图中没有显示的情节

时间:2017-12-14 16:50:44

标签: python-2.7 matplotlib text

其中一个数字框中的图表未显示。两个图都在一个图框内,但没有在每个图中绘制。以下是我的意思:enter image description here

这些图应分别在一个数字框之间分配。以下是绘图的代码:

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]。

1 个答案:

答案 0 :(得分: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:使用与先前轴相同的参数添加轴当前重用前一个实例。在将来的版本中,将始终创建并返回新实例。同时,通过将唯一标签传递给每个轴实例,可以抑制此警告,并确保未来的行为。

相关问题