3

Pardon my poor python skills, I am rather new to the language!

None the less, I am confused by the results I am getting from twinx() currently. I am not sure why when I twin the x axis, the ticks on the right hand side y axis seem to double.

import matplotlib.pyplot as plt

x = linspace(0,2*pi,100)
y = sin(x) + 100*rand(len(x))
z = cos(x) + 100*rand(len(x))
data = []
data.append(y)
data.append(z)

fig = plt.figure(1)
for kk in range(len(data)):
    ax1 = fig.add_subplot(111)
    ax1.plot(x.T, data[kk], 'b.-')

plt.show()

The first plot displays (to my mind) the correct behavior left Y axis

fig2 = plt.figure(2)
for kk in range(len(data)):
    ax3 = fig2.add_subplot(111)
    ax4 = ax3.twinx()
    ax4.plot(x.T, data[kk], 'b.-')

plt.show()

While the second plot (where all I have done is flip the axis) seems to have poor y tick behavior wherein the two 'curves' each get their own tick marks. right y axis

Any thoughts as to why this might be occurring would be greatly appreciated!

Zephyr
  • 11,891
  • 53
  • 45
  • 80
not link
  • 858
  • 1
  • 16
  • 29
  • https://stackoverflow.com/questions/20243683/matplotlib-align-twinx-tick-marks That questions answert your qestion realy well. – NameVergessen Dec 27 '19 at 02:18

2 Answers2

2

Because that's what twinx is supposed to do :)

Seriously, though, the point of twinx is to create an independent y-axis on the same plot. By default, it shows the ticks for the second, independent y-axis on the right hand side of the plot.

The idea is that you can do something like this using twinx (or twiny if you want two independent x-axes):

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
ax2 = ax.twinx()

x = np.linspace(0, 10, 100)

ax.plot(x, np.sin(x), color='blue')
ax.set_ylabel(ylabel='Y-Value 1', color='blue')
ax.set_xlabel('Same X-values')

ax2.plot(x, x**3, color='green')
ax2.set_ylabel('Y-Value 2', color='green')

plt.show()

enter image description here

If you just want two curves that share the same axes, just plot them on the same axes. E.g.:

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()

x = np.linspace(0, 10, 100)
ax.plot(x, x, label='$y=x$')
ax.plot(x, 3 * x, label='$y=3x$')
ax.legend(loc='upper left')

ax.set(xlabel='Same X-values', ylabel='Same Y-values')

plt.show()

enter image description here

Joe Kington
  • 275,208
  • 71
  • 604
  • 463
  • Thanks for your reply. Please see my updated post with plots. I wonder if it has anything to do with my install. I'm using OS X 10.8.2 with Python 2.7.3 and Ipython 0.12.1 – not link Dec 17 '12 at 02:04
  • Thanks for adding the images showing the problem. That's definitely not normal behavior. How did you install matplotlib? (e.g. `homebrew`, one of the binaries from the matplotlib site, etc) – Joe Kington Dec 17 '12 at 04:23
  • Actually, there's probably a simpler explanation. Don't use calls like `plt.figure(1)`, as it won't create a new figure, it just returns figure number 1. Just use `fig = plt.figure()` instead and leave out the number argument entirely. You're probably adding new axes on top of the old axes on an existing figure when you intended to create a new figure. This causes the ticks from the previous axes to "poke out" from behind the new axes you just added. – Joe Kington Dec 17 '12 at 04:31
  • I tried it without the number in the plt.figure() command as you recommended and I get the same result as earlier. I installed python with the EPD installer ver 7.3 for OS X and thus have matplotlib 1.1.0 (as can be seen [here](http://www.enthought.com/products/epdlibraries-past.php?ver=7.3)). – not link Dec 17 '12 at 21:05
  • Note, I just tried generating my plots on a Windows 7 computer with EPD installed and received the same results. – not link Dec 17 '12 at 21:27
2

If you take the call to twinx() and add_subplot() out of the loop, I think you'll get the figure you're after. Like so:

fig2 = plt.figure(2)
ax3 = fig2.add_subplot(111)
ax4 = ax3.twinx()
for kk in range(len(data)):
    ax4.plot(x.T, data[kk], 'b.-')

plt.show()

By the end of your loop you're calling twinx() twice, so you naturally get two twinned axes. Since they have different scales, the numbers overlap in an awkward way.

Tanaya
  • 21
  • 1
  • Solved this for me, I was calling twinx() inside of a loop, and it appears to have written the y-ticks multiple times. – Mansweet Sep 09 '16 at 23:32