gpt4 book ai didi

python - Matplotlib 寄生虫对数轴刻度不需要的镜像

转载 作者:太空宇宙 更新时间:2023-11-03 11:51:57 26 4
gpt4 key购买 nike

我正在尝试使用 mpl_toolkits.axes_grid1 中的 host_subplot 使用两个 y 轴绘制一个图,其中一个是对数轴,一个是线性轴。该图看起来不错,除了辅助 y 轴(右)的次要刻度也显示在图的内部的主 y 轴(左)上。

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

host = host_subplot(111)
host.set_yticks(np.arange(-12, -3, 1.0))

par1 = host.twinx()
par1.set_ylim( 2.7040e+3, 1.3552e+7)
par1.set_yscale('log')

minorLocator_x1 = MultipleLocator(0.3333)
minorLocator_y1 = MultipleLocator(0.5)
host.xaxis.set_minor_locator(minorLocator_x1)
host.yaxis.set_minor_locator(minorLocator_y2)

The plot looks like this. You can see how the right y-axis ticks are mirrored on the left y-axis.

我可以使用以下方法修复镜像次要对数轴刻度:

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

但是,这会产生另一个问题,即 x 轴刻度标签显示在图形内部,就像 x 轴标签一样。

The x-label now won't move even if I attempt a manual offset.

关于如何规避问题的任何想法?

最佳答案

我找到了解决问题的变通方法,但不是通过使用 mpl_toolkits.axes_grid1 中的 host_subplot。相反,我使用 matplotlib 轴,如下所示:

fig, ax1 = plt.subplots()

ax1.set_xlim(-0.25, 5.1)
ax1.set_ylim(-3.75, -13)
ax2=ax1.twinx()

ax1.set_xlabel('X-label', fontdict=font)
ax1.set_ylabel('Y1-label$', rotation='horizontal', fontdict=font)
ax2.set_ylabel('Y2-label', rotation='horizontal', fontdict=font)

ax2.set_ylim(2.7040e+3, 1.3552e+7)
ax2.set_yscale('log')
ax1.set_yticks(np.arange(-12, -3, 1.0))

ml = MultipleLocator(0.5)
minorLocator = MultipleLocator(0.3333)
ax1.xaxis.set_minor_locator(minorLocator)
ax1.yaxis.set_minor_locator(ml)

这会生成正确的图。在我看来,之前的问题是第一种情况下刻度 (set_minor_locator) 的模糊分配(没有在 host_subplot 中使用 axes_class=AA.Axes)。

关于python - Matplotlib 寄生虫对数轴刻度不需要的镜像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24353863/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com