Matplotlib logarithmic axis indicates unwanted mirroring

I am trying to make a plot with two y-axes, one of which is logarithmic and one linear, using host_subplot from mpl_toolkits.axes_grid1. This figure looks normal, with the exception of small ticks from the secondary y axis (right), which is also displayed on the primary y axis (left), inside the picture.

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.

I can fix mirror small logarithmic axes with:

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

However, this creates another problem, namely, that the labels of the x-axis labels are displayed inside the figure, as well as the x-axis label.

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

Any ideas on how to get around the problems?

+4
source share
1 answer

, , host_subplot mpl_toolkits.axes_grid1. 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) ( axes_class= AA.Axes host_subplot).

+1

Source: https://habr.com/ru/post/1545581/


All Articles