Matplotlib parasite logarithmic axis ticks unwanted mirrorring

十年热恋 提交于 2019-12-23 12:09:30

问题


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)

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?


回答1:


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)

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).



来源:https://stackoverflow.com/questions/24353863/matplotlib-parasite-logarithmic-axis-ticks-unwanted-mirrorring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!