Home > Net >  Can I reverse xaxis in Aitoff projection supported by matplotlib? (It is not duplicate.)
Can I reverse xaxis in Aitoff projection supported by matplotlib? (It is not duplicate.)

Time:07-06

I want to reverse my xaxis of my aitoff projection in my python script. Here is an example

import numpy as np
import matplotlib.pyplot as plt
X=[-1,0,1,2]
Y=[0,1,0,1]
plt.figure()
plt.subplot(111,projection="aitoff")
plt.grid(True)
plt.gca().invert_xaxis()
plt.scatter(X,Y)
plt.show()

plt.gca().invert_xaxis() will show error.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/hea/.local/lib/python3.10/site-packages/matplotlib/axes/_base.py", line 3525, in invert_xaxis
    self.xaxis.set_inverted(not self.xaxis.get_inverted())
  File "/home/hea/.local/lib/python3.10/site-packages/matplotlib/axis.py", line 2243, in set_inverted
    self.axes.set_xlim(sorted((a, b), reverse=bool(inverted)), auto=None)
  File "/home/hea/.local/lib/python3.10/site-packages/matplotlib/projections/geo.py", line 151, in set_xlim
    raise TypeError("Changing axes limits of a geographic projection is "
TypeError: Changing axes limits of a geographic projection is not supported.  Please consider using Cartopy.

If I remove plt.gca().invert_xaxis(), it will work fine. Is there any way I can fix it?

CodePudding user response:

I have a workaround for you, but some peculiar plt.pauses are required to make it work (if theses pauses are omitted, no xticks are displayed for me at all after the manipulation):

Instead of inverting the x-axis, try both flipping the x-coordinate and the xticklabels:

import numpy as np
import matplotlib.pyplot as plt
X=[-1,0,1,2]
Y=[0,1,0,1]
plt.figure()
plt.subplot(111,projection="aitoff")
plt.grid(True)

X = -np.array(X)  # flip X coordinates
plt.scatter(X,Y)

plt.pause(0.5)  # without theses pauses the ticks are not displayed at all, not sure why
ax = plt.gca()
ax.set_xticklabels(ax.get_xticklabels()[::-1])  # flip xticklabels
plt.pause(0.5) # without theses pauses the ticks are not displayed at all, not sure why
plt.show()
´´´
  • Related