0

This answer has beautifully showed how to reverse the y-axis. However, I now wish to draw all my dots, etc. with respect to this reversed version of coordinate system.

I find the following all fail this purpose:

plt.figure()
plt.gca().invert_yaxis()
plt.plot([1,2],[1,3]) # just a random line


plt.figure()
plt.plot([1,2],[1,3]) # just a random line
plt.gca().invert_yaxis()

How may I fix it and let it work?


For me, if I use an OOP-style figure, i.e.

fig = plt.figure()
axes = fig.add_axes([0.1, 0.1, 0.8, 0.8])
axes.plot([1,2],[1,3]) # just a random line
axes.invert_yaxis()

it works.

But for the current two non-OOP styles listed above, a new figure with a reversed y-axis is created, but the line is not there.

Community
  • 1
  • 1
Sibbs Gambling
  • 19,274
  • 42
  • 103
  • 174
  • Both of those work correctly for me. What happens if you try `fig1, ax1 = plt.subplots(1,1); ax1.invert_yaxis(); ax1.plot([1,2],[1,3])` and `fig2, ax2 = plt.subplots(1,1); ax2.plot([1,2],[1,3]); ax2.invert_yaxis()` – ali_m Oct 08 '13 at 09:05
  • @ali_m Please see the updates. :) – Sibbs Gambling Oct 08 '13 at 09:12
  • You need to call `plt.draw()` after the `axes.plot`. You never trigger a re-draw in the OO-style code. – tacaswell Oct 08 '13 at 18:26

1 Answers1

1

I still can't reproduce your original error using the snippet you posted (is that really all of your code?), but what you're describing sounds like it could be caused by a race condition when you call plt.gca() twice in quick succession. You could perhaps try inserting a short pause between plotting your two figures:

import time

plt.figure()
plt.gca().invert_yaxis()
plt.plot([1,2],[1,3]) # just a random line

time.sleep(0.1)

plt.figure()
plt.plot([1,2],[1,3]) # just a random line
plt.gca().invert_yaxis()

However, as a more general point I would strongly recommend that you avoid using gca() and gcf() except for convenience during interactive sessions - it's much more Pythonic to pass the axes or figure objects explicitly, and it makes it way easier to keep track of exactly which axes/figures are being modified.

ali_m
  • 71,714
  • 23
  • 223
  • 298