简体   繁体   English

matplotlib中具有相同x轴的多个y轴

[英]Multiple y axis with same x axes in matplotlib

Hello I have several values to plot into a graph that share the same x axis (time) but multiple y values(temperature, umidity etc), I did this code.你好,我有几个值要绘制成一个共享相同 x 轴(时间)但多个 y 值(温度、湿度等)的图形,我做了这个代码。 I can't get the y axis scale in the right and in a good form(now there are overlapping y scale, so you can't really see what values you have for each value).我无法以正确的形式获得 y 轴比例(现在有重叠的 y 比例,因此您无法真正看到每个值的值)。 I want a graph that share the same axis but the y bars are in the right and you can understand the values of it, so the problems are the y bars that are not well shown.我想要一个共享相同轴的图形,但 y 条在右侧,您可以理解它的值,所以问题是 y 条没有很好地显示。 How can I fix it?我该如何解决? This is the problem https://i.imgur.com/G1EiYIh.png as you can see the y bar are not in a good form, one is overlapped too这是问题https://i.imgur.com/G1EiYIh.png因为你可以看到 y 条的形式不好,一个也重叠了

import serial
import time
import matplotlib.pyplot as plt
import numpy as np
from drawnow import *
arduinoData = serial.Serial('/dev/ttyACM0',9600)
time.sleep(2)
data_temperature = []                       # empty list to store the data
data_luminosity = []
data_thermistor = []
data_umidity = []
plt.ion()
counter = 0
def makePlot():

    plt.ylim(15,35)
    plt.title('Real Time Data')
    plt.grid(True)
    plt.ylabel('Temperature')
    plt.plot(data_temperature, 'ro-', label = 'Temperature')
    plt.tick_params(direction='right')
    plt.legend(loc='upper left')
    plt2 = plt.twinx()

    plt2.plot(data_umidity, 'bo-', label = 'Umidity %')
    plt2.legend(loc= 'upper right')
    plt3 = plt.twinx()
    plt3.plot(data_thermistor, 'go-', label = 'Thermistor temperature')
    plt3.legend(loc = 'lower left')


while True:
    if arduinoData.inWaiting()==0:
        pass
    time.sleep(5)
    arduinoString = arduinoData.readline()         # read a byte string
    arrayData = arduinoString.split()
    luminosity = float(arrayData[0])
    thermistorTemperature = float(arrayData[1])
    temperature = float(arrayData[2])
    umidity = float(arrayData[3])
    print(luminosity, thermistorTemperature, temperature, umidity)
    data_temperature.append(temperature)
    data_luminosity.append(luminosity)
    data_thermistor.append(thermistorTemperature)
    data_umidity.append(umidity)
    drawnow(makePlot)
    plt.pause(.000001)
    counter += 1
    if counter > 50:
        data_temperature.pop(0)
        data_umidity.pop(0)


ser.close()

If you follow the matplotlib example for multiple y axis with changing spines, your answer lies there.如果您按照matplotlib 示例获取多个 y 轴并更改脊椎,那么您的答案就在那里。 You need to move it further from the original plot, and set the visibility of its spines.您需要将其从原始图移得更远,并设置其脊椎的可见性。

plt3 = plt.twinx()
# Offset the right spine of plt3.  The ticks and label have already been
# placed on the right by twinx above.
plt3.spines["right"].set_position(("axes", 1.2))
# Having been created by twinx, plt3 has its frame off, so the line of its
# detached spine is invisible.  First, activate the frame but make the patch
# and spines invisible.
make_patch_spines_invisible(plt3)
# Second, show the right spine.
plt3.spines["right"].set_visible(True)

EDIT: And the make_patch_spines_invisible function is below, taken from the example in the link above.编辑:下面是make_patch_spines_invisible函数,取自上面链接中的示例。

def make_patch_spines_invisible(ax):
    ax.set_frame_on(True)
    ax.patch.set_visible(False)
    for sp in ax.spines.values():
        sp.set_visible(False)

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

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