简体   繁体   中英

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. 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). 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. 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

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. 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.

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)

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