轴绘图中的Matplotlib错误

时间:2017-12-05 23:33:06

标签: matplotlib flask

我正在使用Flask在某个端点上生成图表并将其发送到浏览器。我更新了我正在绘图的数据,但没有别的。我每隔2秒调用一次/graph端点,但情节在那段时间内发生了巨大变化。示例如下:

enter image description here

enter image description here enter image description here

这是我正在使用的代码:

@app.route('/graph')
def graph_ep():
    fig = plt.figure()

    # Matplotlib setup
    style.use('fivethirtyeight')  # Make the graphs look better

    ax1 = fig.add_subplot(1, 1, 1)

    ax1.clear()
    ax1.plot(x, y, marker='o', label="Heart Data")
    ax1.plot(x, LRL_data, marker='o', label="Lower Rate Limit")
    ax1.plot(x, URL_data, marker='o', label="Upper Rate Limit")
    ax1.set(xlabel="Time",
        ylabel="Beats per Minute",
        title="Moving Average of Heart Rate")

    ax1.legend(loc="upper center")

    ax1.grid(True)

    # Change y axis
    ax1.set_ylim(0,2.5)
    x1, x2, y1, y2 = plt.axis()
    plt.axis((x1,x2,0.0, 2.5))
    y_axis = [float(i)/10.0 for i in range(0,25,2)]
    plt.yticks(y_axis)

    time_string = str(int(time.time()))
    file_name = 'static/graphs/real/graph' + time_string + '.png'

    plt.savefig(file_name)

    return '<img src="' + file_name + '">'

如何只更新数据时,如何保持图表的所有功能(轴,轴标签,网格等)与第一张图像相同?

1 个答案:

答案 0 :(得分:0)

结果表明数组y中的数据不一致。我同时在其中有字符串和浮点数,这令人困惑matplotlib。例如,[0.0, 0.1, '0.2', '0.3', '0.4']。一旦我将字符串转换为浮点数,问题就得到了解决。但是,轴标签仍然是隐藏的。