Имея единственные значения оси Y при построении двух переменных на вторичной оси - PullRequest
2 голосов
/ 01 февраля 2020

Я пытаюсь построить три переменные на графике, используя первичную и вторичную оси с одной переменной на первичной оси и двумя на вторичной оси. Мой код

vav = floor_data[floor_data['vavId'] == i]
        vav = vav.reset_index()
        x = vav.index 
        y1 = vav['nvo_temperature_sensor_pps']
        y2 = vav['nvo_airflow']
        y3 = vav['nvo_air_damper_position']   

        fig, ax1 = plt.subplots()
        ax2 = ax1.twinx()
        ax1.plot(x, y1, 'g-')
        ax2.plot(x, y2, 'b-')
        ax2.plot(x, y3, 'r-')
        ax2 = ax1.twinx()

        ax1.set_xlabel('VAV '+str(i))
        ax1.set_ylabel('temperature ', color='g')
        ax2.set_ylabel('air flow, temperature', color='b')

        plt.show()

Я добавил все три переменные, но столкнулся с проблемой в тиках y вторичной оси. Мой график выглядит так: enter image description here

Возможно ли иметь одинарные значения y-тиков на вторичной оси для лучшей читаемости?

1 Ответ

3 голосов
/ 01 февраля 2020

Вам нужно создать новую ось твикса на узле хоста и уменьшить участок, чтобы создать пространство для дополнительной оси на правой стороне. Затем переместите новую ось в правильное положение. Некоторые описания в коде.

import matplotlib.pyplot as plt
import numpy as np

fig, host = plt.subplots()
# shrink subplot
fig.subplots_adjust(right=0.75)

# create new axis on host
par1 = host.twinx()
par2 = host.twinx()
# place second axis at far right position
par2.spines["right"].set_position(("axes", 1.2))


# define plot functions
def function_sin(x):
    return np.sin(x)


def function_parabola(x):
    return x**2


def function_line(x):
    return x+1

# plot data
x = np.linspace(0, 10, 100)
y_sin = function_sin(x)
y_parabola = function_parabola(x)
y_line = function_line(x)
host.plot(x, y_sin, "b-")
par1.plot(x, y_parabola, "r-")
par2.plot(x, y_line, "g-")

# set labels for each axis
host.set_xlabel("VAV 16")
host.set_ylabel("Temperature")
par1.set_ylabel("Temperature")
par2.set_ylabel("Air Flow")

plt.show()

Выход:

enter image description here

...