4

I've seen several question asking how to add a secondary x or y axis but none asking if adding both at the same time is possible.

I know ax.twinx() and ax.twiny() are used to add either another y or x axis respectively, but how would one go about adding both?

Here's an example of two curves with different x,y ranges:

import matplotlib.pyplot as plt
import numpy as np

# Generate two sets of random data.
x1 = np.random.randn(50)
y1 = np.linspace(0, 1, 50)
x2 = np.random.randn(20)+15.
y2 = np.linspace(10, 20, 20)

# Plot both curves.
fig = plt.figure()

ax1 = fig.add_subplot(121)
plt.plot(x1, y1, c='r')

ax2 = fig.add_subplot(122)
plt.plot(x2, y2, c='b')

plt.show()

This results in:

enter image description here

and what I'm after is something like this:

enter image description here

where the right y axis and the top x axis correspond to the blue curve.

Can this be done?

Gabriel
  • 40,504
  • 73
  • 230
  • 404
  • 2
    Good question, I was looking into this myself. :) – Russia Must Remove Putin Mar 06 '14 at 15:32
  • just use both, the third `axes` will be what you want. – tacaswell Mar 06 '14 at 15:40
  • @tcaswell -- This was what I was about to suggest trying. It's good to see that you've already answered this before :) – mgilson Mar 06 '14 at 15:46
  • @tcaswell indeed this is a duplicate of that question, I missed that. I'll vote to close. Thanks you! – Gabriel Mar 06 '14 at 16:11
  • 1
    @Gabriel That is why SO has the duplicate linking scheme, not everyone describes the same problem with the same words. The more sign posts the better. – tacaswell Mar 06 '14 at 16:12
  • 1
    @tcaswell if you have the time please stop by a new question that came about after applying your answer: http://stackoverflow.com/questions/22230926/no-label-for-secondary-y-axis-in-plot. Thanks! – Gabriel Mar 06 '14 at 17:11

0 Answers0