Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matplotlib parasite logarithmic axis ticks unwanted mirrorring

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)

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

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.

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

Any ideas on how to circumvent the problems?

like image 545
mannaroth Avatar asked Nov 10 '22 06:11

mannaroth


1 Answers

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

like image 118
mannaroth Avatar answered Nov 15 '22 09:11

mannaroth