简体   繁体   English

在辅助轴上绘制两个变量时具有单个 y 轴值

[英]Having a single y axis values while plotting two variables on secondary axis

I am trying to plot three variables, in a graph using primary and secondary axis with one variable on primary axis and two on secondary axis.我试图在一个图表中绘制三个变量,使用主轴和辅助轴,一个变量在主轴上,两个在辅助轴上。 My code我的代码

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

I have added all the three variables but I am facing problem in y-ticks of secondary axis.我已经添加了所有三个变量,但我在辅助轴的 y-ticks 中遇到了问题。 My plot looks like我的情节看起来像在此处输入图片说明

Is it possible to have a single y tick values on secondary axis for better readability?是否可以在辅助轴上有一个 y 刻度值以获得更好的可读性?

You need to create new twix axis on host and shrink subplot to create space for additional axis on right side.您需要在主机上创建新的 twix 轴并缩小子图为右侧的附加轴创建空间。 Then move new axis at right position.然后在正确的位置移动新轴。 Some descriptions in code.代码中的一些描述。

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

Output:输出:

在此处输入图片说明

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

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