简体   繁体   English

同一图表上的多个图 - Matplotlib

[英]Multiple plots on same chart - Matplotlib

I'm plotting real-time data using parsed data from a file that is being repeatedly opened.我正在使用重复打开的文件中的解析数据绘制实时数据。 I'm deriving and plotting two different values on the same chart.我在同一张图表上推导和绘制两个不同的值。 The Y axis scales up and down according the values of each. Y 轴根据每个值向上和向下缩放。 The "snr" (green) value is plotting fine but the "data_rate"(red) value seems to be static. “snr”(绿色)值绘制得很好,但“data_rate”(红色)值似乎是静态的。 See screenshot.见截图。 图表

import matplotlib.pyplot as plt
import csv
import datetime
from matplotlib.animation import FuncAnimation
import numpy as np


x = []
y = []
z = []

rssi_val = []

def animate(i):
    with open('stats.txt', 'r') as searchfile:
        time = (searchfile.read(8))
        for line in searchfile:
            if 'agrCtlRSSI:' in line:
                rssi_val = line[16:20]
                rssi_val=int(rssi_val)
            if 'agrCtlNoise:' in line:
                noise_val = (line[16:20])
                noise_val=int(noise_val)
            if 'maxRate:' in line:
                data_rate = (line[16:20])
                data_rate=int(data_rate)


    snr = ((noise_val - rssi_val) * -1)
    #begin test
    y.append(snr)
    x.append(time)
    z.append(data_rate)
    #end test

#begin test
    plt.cla()

    #Verify values are not empty
    print("SNR = ", snr)
    print("DR = ", data_rate)

    plt.plot(snr,label='Signal')
    plt.plot(data_rate,label='Data Rate')

    plt.legend(loc='upper right')

    plt.plot(x,y,z)



    ax=plt.gca()
    ax.tick_params('x',labelrotation=60)
#end test

ani = FuncAnimation(plt.gcf(), animate, interval=1000)

plt.show()

The simplest way is to call plot twice.最简单的方法是调用 plot 两次。

plt.plot(x,y)
plt.plot(x,z)

See "Plotting multiple sets of data" here for more options.有关更多选项,请参阅 此处的“绘制多组数据”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM