5

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?

mannaroth
  • 1,473
  • 3
  • 17
  • 38
  • Could you post the figure? I am not sure what you mean by saying "tick labels are displayed on the inside of the figure". – Aung Jun 22 '14 at 20:52
  • Hmm, I get the same minor tick mirroring you do (which is odd...), but your suggestion of using AA.Axes fixes everything in mine (x ticks are normal). I'm using matplotlib version 1.3.1, what version are you using? – Ajean Jun 23 '14 at 00:22
  • I've edited the post to show the figures. I'm using matplotlib 1.1.1rc (a centralized software instalation). Thanks! – mannaroth Jun 23 '14 at 11:21

1 Answers1

2

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

mannaroth
  • 1,473
  • 3
  • 17
  • 38
  • Glad you worked it out! The difference between the versions may mean that the x axis behavior was something buggy that was addressed somewhere in between. You might submit a bug/feature for the tick mirroring, I don't think that should be happening at all... :) – Ajean Jun 23 '14 at 16:22