Matplotlib寄生虫对数轴剔除不需要的镜像 [英] Matplotlib parasite logarithmic axis ticks unwanted mirrorring

查看:155
本文介绍了Matplotlib寄生虫对数轴剔除不需要的镜像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to make a plot with two y-axes, one of them logarithmic and one linear, using host_subplot from mpl_toolkits.axes_grid1. The figure looks ok, with the exception of the minor ticks from the secondary y-axis (right) being also displayed on the primary y-axis (left), on the inside of the figure.

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)

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

I can fix the mirrored minor logarithmic axis ticks by using:

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

但是,这带来了另一个问题,即x轴刻度标签也显示在图形的内部,就像x轴标签一样.

However, this creates another problem, namely that the x-axis tick labels are displayed on the inside of the figure, just as the x-axis label is too.

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

Any ideas on how to circumvent the problems?

推荐答案

我找到了解决此问题的解决方法,但没有使用mpl_toolkits.axes_grid1中的host_subplot.相反,我使用matplotlib轴,如下所示:

I found a workaround that solves the problem, but not by using host_subplot from mpl_toolkits.axes_grid1. Instead, I use matplotlib axes, as follows:

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)

这将产生正确的图.在我看来,以前的问题是第一种情况(不使用axes_class = AA.host_subplot中的轴)的刻度线(set_minor_locator)的模糊分配.

This produces the right plot. It looks to me that the problem before was the fuzzy assignment of the ticks (set_minor_locator) in the first case (without using axes_class=AA.Axes in host_subplot).

这篇关于Matplotlib寄生虫对数轴剔除不需要的镜像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆