我可以在matplotlib支持的AItoff投影中反向Xaxis吗? (这不是重复的。)

发布于 2025-02-12 18:40:51 字数 1103 浏览 3 评论 0原文

我想在我的Python脚本中扭转我的AiToff投影的Xaxis。这是一个示例

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(),它将正常工作。有什么办法可以修复它?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放我走吧 2025-02-19 18:40:51

我有一个解决方法,但是需要一些特殊的plt.plt.pause s才能使其正常工作(如果省略了这些暂停,则在操纵后根本没有显示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()

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文