-1

我的问题的重点是控制右侧 y 轴限制。编码:

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

在情节调用之前或之后没有影响。我难住了。任何输入帮助将不胜感激,特别是解释为什么它不起作用。这种类型的难题在 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()
4

1 回答 1

0

您只bottom为该方法提供了参数set_ylimset_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()

这使得输出具有所需的 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 )

在此处输入图像描述


附带说明;这是你想要的关于标题和副标题的行为吗?

于 2013-07-16T14:33:11.863 回答