更改 mpl_toolkits new_fixed_axis 中的轴线范围

如何解决更改 mpl_toolkits new_fixed_axis 中的轴线范围

我正在努力修改我的代码以定义辅助 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,20))
    ax1.bar(x,[t[2] for t in metric1],width,label='metric1')
    ax1.bar(x + width,[t[2] for t in metric2],label='metric2')
    ax1.bar(x + 2*width,[t[2] for t in metric3],label='metric3')

    ax1.set_xticks(x+width)
    ax1.set_xticklabels(['BN','B','DO','N','BN','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']))
...

这是当前的输出

This is the current output

我想要的是,不要让辅助 x 轴 (foo,bar,foo2) 线超出第一个和最后一个 x-tick,如下(我在 MS Paint ? 中编辑):>

enter image description here

感谢任何帮助。

解决方法

由于没有其他答案,我可以建议一种非优雅的方式来做您需要的事情。

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

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,])

在一个简单的例子中,它给出了这样的东西: enter image description here

相反:

enter image description here

替代

创建多轴的另一种方法是使用脊椎(无寄生轴): https://matplotlib.org/stable/gallery/ticks_and_spines/multiple_yaxis_with_spines.html

在这种情况下,只需更改脊椎的边界即可完成您需要的操作。例如,将以下行添加到链接中的代码

par2.spines["right"].set_bounds(10,30)

我们明白了:

enter image description here

显然,这并没有严格回答你的问题的标题,不幸的是,我不知道对 new_fixed_axis 这样做的正确方法,因为它可以为刺做。我希望“手动”创建的行可以解决您的问题,以防其他人提供更好的解决方案。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?