从mpl_toolkits matplotlib。导入host_subplot - ylim对右轴的控制?

时间:2022-04-17 23:44:17

The focus of my question pertains to getting control over the right hand side y-axis limits. The code:

我的问题的重点是控制右手边y轴的极限。代码:

if y2_lim != None:
    par2.set_ylim(y2_lim)

Has no affect before or after the plot call. I am stumped. Any input help would be greatly appreciated, especially an explanation of why it doesn't work. This type of conundrum happens too often with Matplotlib. Thanks, Charles

没有影响之前或之后的情节调用。我难住了。任何输入帮助都将非常感谢,特别是它为什么不工作的解释。这种谜题在Matplotlib经常出现。谢谢你,查尔斯

from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
import matplotlib.pyplot as plt

def plotTwoAxis(aData1, aData2, anXLabel, aY1Label, aY2Label, aTitle, 
                aSubtitle, aLegend1Label, aLegend2Label, y1_lim = None, y2_lim = None):

host = host_subplot(111, axes_class=AA.Axes)

par1 = host.twinx()
par2 = host.twinx()

offset = 60
new_fixed_axis = par2.get_grid_helper().new_fixed_axis
par2.axis["right"] = new_fixed_axis(loc="right",
                                    axes=par2,
                                    offset=(offset, 0))

par2.axis["right"].toggle(all=True)

host.set_xlim(0, 24)

host.set_xlabel(anXLabel)
host.set_ylabel(aY1Label)
par1.set_ylabel(aY2Label)

if y1_lim != None:
    par1.set_ylim(y1_lim)
if y2_lim != None:
    par2.set_ylim(y2_lim)

p1, = host.plot(time, aData1, label=aLegend1Label)
p2, = par1.plot(time, aData2, label=aLegend2Label)

host.legend()

host.axis["left"].label.set_color(p1.get_color())
par1.axis["right"].label.set_color(p2.get_color())

plt.title(aTitle + aSubtitle)

plt.draw()
plt.show()

1 个解决方案

#1


0  

You have only provided the bottom argument to the set_ylim method. Also, you should call the set_ylim method after the plots if you haven't disabled the autoscale of the axes. If you want the bottom limit of the y-axes to default to 0, then uo can modify your code like this:

您只提供了set_ylim方法的底部参数。同样,如果没有禁用坐标轴的自动缩放,应该在绘图之后调用set_ylim方法。如果您希望y轴的底限为0,则uo可以这样修改您的代码:

from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
import matplotlib.pyplot as plt

def plotTwoAxis(aData1, aData2, anXLabel, aY1Label, aY2Label, aTitle, 
                aSubtitle, aLegend1Label, aLegend2Label,
                y1_bottom = 0, y1_lim = None, # bottom and top limits respectively
                y2_bottom = 0, y2_lim = None): # bottom and top limits, respectively

    host = host_subplot(111, axes_class=AA.Axes)

    par1 = host.twinx()
    par2 = host.twinx()

    offset = 60
    new_fixed_axis = par2.get_grid_helper().new_fixed_axis
    par2.axis["right"] = new_fixed_axis(loc="right",
                                    axes=par2,
                                    offset=(offset, 0))

    par2.axis["right"].toggle(all=True)

    host.set_xlim(0, 24)

    host.set_xlabel(anXLabel)
    host.set_ylabel(aY1Label)
    par1.set_ylabel(aY2Label)

    p1, = host.plot(time, aData1, label=aLegend1Label)
    p2, = par1.plot(time, aData2, label=aLegend2Label)

    if y1_lim != None:
        par1.set_ylim(y1_bottom, y1_lim)
    if y2_lim != None:
        par2.set_ylim(y2_bottom, y2_lim)

    host.legend()

    host.axis["left"].label.set_color(p1.get_color())
    par1.axis["right"].label.set_color(p2.get_color())

    plt.title(aTitle + aSubtitle)

    plt.draw()
    plt.show()

This makes an output with the desired y-limits:

这使得输出具有所需的y限制:

import numpy as np

time = np.arange(25)

plotTwoAxis(np.random.rand(25),
            np.random.rand(25),
            'x label 1', 'y label 1', 'y label 2','Title', ' Subtitle',
            'Plot 1','Plot 2', y1_lim = 0.5, y2_lim = 0.75 )

从mpl_toolkits matplotlib。导入host_subplot - ylim对右轴的控制?


On a side note; is this the behavior you want with respect to the title and subtitle?

从一个方面说明;这是你想要的关于标题和副标题的行为吗?

#1


0  

You have only provided the bottom argument to the set_ylim method. Also, you should call the set_ylim method after the plots if you haven't disabled the autoscale of the axes. If you want the bottom limit of the y-axes to default to 0, then uo can modify your code like this:

您只提供了set_ylim方法的底部参数。同样,如果没有禁用坐标轴的自动缩放,应该在绘图之后调用set_ylim方法。如果您希望y轴的底限为0,则uo可以这样修改您的代码:

from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
import matplotlib.pyplot as plt

def plotTwoAxis(aData1, aData2, anXLabel, aY1Label, aY2Label, aTitle, 
                aSubtitle, aLegend1Label, aLegend2Label,
                y1_bottom = 0, y1_lim = None, # bottom and top limits respectively
                y2_bottom = 0, y2_lim = None): # bottom and top limits, respectively

    host = host_subplot(111, axes_class=AA.Axes)

    par1 = host.twinx()
    par2 = host.twinx()

    offset = 60
    new_fixed_axis = par2.get_grid_helper().new_fixed_axis
    par2.axis["right"] = new_fixed_axis(loc="right",
                                    axes=par2,
                                    offset=(offset, 0))

    par2.axis["right"].toggle(all=True)

    host.set_xlim(0, 24)

    host.set_xlabel(anXLabel)
    host.set_ylabel(aY1Label)
    par1.set_ylabel(aY2Label)

    p1, = host.plot(time, aData1, label=aLegend1Label)
    p2, = par1.plot(time, aData2, label=aLegend2Label)

    if y1_lim != None:
        par1.set_ylim(y1_bottom, y1_lim)
    if y2_lim != None:
        par2.set_ylim(y2_bottom, y2_lim)

    host.legend()

    host.axis["left"].label.set_color(p1.get_color())
    par1.axis["right"].label.set_color(p2.get_color())

    plt.title(aTitle + aSubtitle)

    plt.draw()
    plt.show()

This makes an output with the desired y-limits:

这使得输出具有所需的y限制:

import numpy as np

time = np.arange(25)

plotTwoAxis(np.random.rand(25),
            np.random.rand(25),
            'x label 1', 'y label 1', 'y label 2','Title', ' Subtitle',
            'Plot 1','Plot 2', y1_lim = 0.5, y2_lim = 0.75 )

从mpl_toolkits matplotlib。导入host_subplot - ylim对右轴的控制?


On a side note; is this the behavior you want with respect to the title and subtitle?

从一个方面说明;这是你想要的关于标题和副标题的行为吗?