在循环绘图时如何保留以前的数据集?

时间:2016-06-03 18:43:23

标签: python matplotlib

我有一个数据段,我正在循环加载,然后在图表上绘图,我希望在以前的段上绘制新的数据段,这样当我完成时,所有数据都将是绘制在一张图上。我在下面列出了绘图片段,目前只为22个不同的数据片段制作了22个不同的绘图。

    fig = pl.figure()
    ax1 = pl.subplot(211)
    ax2 = pl.subplot(212)
    external_temp4 = segment.acs.channels["T_BRO4"]
    external_temp2 = segment.acs.channels["T_BRO2"]
    board_temp = board_ss_dataset.channels["mb_temp2"]
    time = (segment.acs.times-segment.acs.times[0])/3600
    board_time = (board_ss_dataset.times-board_ss_dataset.times[0])/3600

    ax1.plot(time, external_temp2,'.k', label = 'BRC2 external temp')
    if num_board+56>=56 and num_board+56<=61:
        ax1.plot(board_time, board_temp, '.', label = "board {0:d}".format(num_board+56))
    box1 = ax1.get_position()
    ax1.set_position([box1.x0, box1.y0, box1.width*0.8, box1.height])
    ax1.set_ylabel("Temperature $^o$C")
    ax1.legend(prop={'size':7},loc='center left', bbox_to_anchor=(1, 0.5))

    ax2.plot(time, external_temp4,'.k', label = 'BRC4 external temp')
    if num_board+56>=70 and num_board+56<=77:
        ax2.plot(board_time, board_temp, 'r.', label = "board {0:d}".format(num_board+56))
    box2 = ax2.get_position()
    ax2.set_position([box2.x0, box2.y0, box2.width*0.8, box2.height])
    ax2.legend(prop={'size':7},loc='center left', bbox_to_anchor=(1, 0.5))
    ax2.set_xlabel("Time in hours {0:d}".format(num_board+1))
    ax2.set_ylabel("Temperature $^o$C")
    pl.show() 

1 个答案:

答案 0 :(得分:1)

假设这一切都在一个循环中,那么每次都不要调用fig = pl.figure()。这是创造新人物的过程。当您调用任何绘图命令时,它将自动绘制到焦点图形,这始终是最近创建的图形。你可能想要这样的东西:

fig = pl.figure()
ax1 = pl.subplot(211)
ax2 = pl.subplot(212)
for ...:
    external_temp4 = segment.acs.channels["T_BRO4"]
    external_temp2 = segment.acs.channels["T_BRO2"]
    board_temp = board_ss_dataset.channels["mb_temp2"]
    time = (segment.acs.times-segment.acs.times[0])/3600
    board_time = (board_ss_dataset.times-board_ss_dataset.times[0])/3600

    ax1.plot(time, external_temp2,'.k', label = 'BRC2 external temp')
    if num_board+56>=56 and num_board+56<=61:
        ax1.plot(board_time, board_temp, '.', label = "board {0:d}".format(num_board+56))

    ax2.plot(time, external_temp4,'.k', label = 'BRC4 external temp')
    if num_board+56>=70 and num_board+56<=77:
        ax2.plot(board_time, board_temp, 'r.', label = "board {0:d}".format(num_board+56))

box1 = ax1.get_position()
ax1.set_position([box1.x0, box1.y0, box1.width*0.8, box1.height])
ax1.set_ylabel("Temperature $^o$C")
ax1.legend(prop={'size':7},loc='center left', bbox_to_anchor=(1, 0.5))

box2 = ax2.get_position()
ax2.set_position([box2.x0, box2.y0, box2.width*0.8, box2.height])
ax2.legend(prop={'size':7},loc='center left', bbox_to_anchor=(1, 0.5))
ax2.set_xlabel("Time in hours {0:d}".format(num_board+1))
ax2.set_ylabel("Temperature $^o$C")
pl.show()