我想在python脚本中反转我的aitoff投影的x轴。下面是一个例子
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()将显示错误。
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.
如果我删除plt.gca().invert_xaxis(),它将工作良好。我有什么办法能修好它吗?
我为您提供了一个解决方案,但是需要一些特殊的plt.pause
才能使其工作(如果省略了这些暂停,则在操作后根本不会为我显示xticks):
与其反转x轴,不如尝试同时翻转x坐标和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()