简体   繁体   中英

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. The "snr" (green) value is plotting fine but the "data_rate"(red) value seems to be static. 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.

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

See "Plotting multiple sets of data" here for more options.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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