Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 更改mpl_工具包中的轴线范围新建_固定_轴_Python_Matplotlib_Multiple Axes - Fatal编程技术网

Python 更改mpl_工具包中的轴线范围新建_固定_轴

Python 更改mpl_工具包中的轴线范围新建_固定_轴,python,matplotlib,multiple-axes,Python,Matplotlib,Multiple Axes,我正在努力修改我的代码以定义辅助x轴的特定范围。下面是创建2个x轴的相关代码片段及其生成的输出: from matplotlib import pyplot as plt import matplotlib.ticker as ticker from mpl_toolkits.axes_grid.parasite_axes import SubplotHost ... x = np.arange(1, len(metric1)+1) # the label locations

我正在努力修改我的代码以定义辅助x轴的特定范围。下面是创建2个x轴的相关代码片段及其生成的输出:

from matplotlib import pyplot as plt
import matplotlib.ticker as ticker
from mpl_toolkits.axes_grid.parasite_axes import SubplotHost

...
    x = np.arange(1, len(metric1)+1)  # the label locations
    width = 0.3  # the width of the bars

    fig1 = plt.figure()
    ax1 = SubplotHost(fig1, 111)
    fig1.add_subplot(ax1)
    ax1.axis((0, 14, 0, 20))
    ax1.bar(x, [t[2] for t in metric1], width, label='metric1')
    ax1.bar(x + width, [t[2] for t in metric2], width, label='metric2')
    ax1.bar(x + 2*width, [t[2] for t in metric3], width, label='metric3')

    ax1.set_xticks(x+width)
    ax1.set_xticklabels(['BN', 'B', 'DO', 'N', 'BN', 'B', 'DO', 'N', 'BN', 'B', 'DO', 'N', 'BN', 'B', 'DO', 'N'])
    ax1.axis["bottom"].major_ticks.set_ticksize(0)
    ax2 = ax1.twiny()
    offset = 0, -25 # Position of the second axis
    new_axisline = ax2.get_grid_helper().new_fixed_axis
    ax2.axis["bottom"] = new_axisline(loc="bottom", axes=ax2, offset=offset)
    ax2.axis["top"].set_visible(False)
    ax2.axis["bottom"].minor_ticks.set_ticksize(0)
    ax2.axis["bottom"].major_ticks.set_ticksize(15)

    ax2.set_xticks([0.058, 0.3434, 0.63, 0.915])
    ax2.xaxis.set_major_formatter(ticker.NullFormatter())
    ax2.xaxis.set_minor_locator(ticker.FixedLocator([0.20125, 0.48825, 0.776]))
    ax2.xaxis.set_minor_formatter(ticker.FixedFormatter(['foo', 'bar', 'foo2']))
...
这是电流输出:


我想要的是,第二个x轴(foo,bar,foo2)线不超过第一个和最后一个x记号,如下所示(我在MS paint中编辑由于没有其他答案,我可以建议一种不优雅的方式来做你需要的事情

您可以隐藏轴线并自己“手动”创建一条线:

import matplotlib.lines as lines

ax2.axis["bottom"].line.set_visible(False)

p1 = ax2.axis["bottom"].line.get_extents().get_points()

x1 = 0.058 * (p1[1][0]-p1[0][0]) / (1) + p1[0][0]
x2 = 0.915 * (p1[1][0]-p1[0][0]) / (1) + p1[0][0]

newL = lines.Line2D([x1,x2], [p1[0][1],p1[1][1]], transform=None, axes=ax2,color="k",linewidth=0.5)
ax2.lines.extend([newL,])
在一个简单的例子中,它给出了如下内容:

与之相反:

另类

创建多轴的一种替代方法是使用棘(无寄生轴):

在这种情况下,只需更改脊椎的边界即可完成所需的操作

par2.spines["right"].set_bounds(10,30)
我们得到这个:


显然,这并没有严格地回答您问题的标题,不幸的是,我不知道新固定轴的正确方法,因为它可以用于脊椎。我希望“手动”创建的线可以解决您的问题,以防没有其他人提供更好的解决方案。

ax2.set\xticks([0.0,0.3434,0.63,1.0])
将第二个xticks设置设置为[0.0,.而不是[0.0,….1.0]实现目标?不幸的是,没有,将first设置为0.0,last设置为1.0只会拉伸整个过程,因此在第一个和最后一个刻度之外没有“尾巴”,但第一个和最后一个刻度现在位于0.0和1.0,这不是我需要的。刻度的位置需要保持固定。我只想强制轴线从e首先打勾并在最后一个打勾处结束,而不是跨越整个“主”x轴。非常感谢@fdireito!这就成功了。但是,我得到了一个AttributeError,它的行
p1=ax2.axis[“bottom”].line.get_extensions().get_points()
告诉我“BezierPath没有属性get_extensts()),因此,我没有“检索”原始轴的y坐标,而是一直玩到得到正确的y位置