反向y轴,绘制w。t matplotlib ?

时间:2021-08-27 08:32:05

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.

这个答案很好地说明了如何反转y轴。然而,我现在希望画出所有的点,等等,关于这个反向的坐标系。

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.

对我来说,如果我使用一个oopstyle的图形,也就是。

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.

但是对于上面列出的两个非oop样式,创建了一个带有反向y轴的新图形,但是这条线不在那里。

1 个解决方案

#1


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:

我仍然不能使用您发布的代码片段来复制您的原始错误(这真的是您的代码吗?),但是您所描述的声音可能是由于您在快速连续调用plt.gca()两次时所造成的。你可以试着在两个数字之间插入一个短暂的停顿:

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.

然而,作为更普遍点我会强烈建议您避免使用gca()和gcf()除了方便在交互式会话-更神谕的轴或图对象明确,这使得它的方式更容易跟踪哪些轴/数据被修改。

#1


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:

我仍然不能使用您发布的代码片段来复制您的原始错误(这真的是您的代码吗?),但是您所描述的声音可能是由于您在快速连续调用plt.gca()两次时所造成的。你可以试着在两个数字之间插入一个短暂的停顿:

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.

然而,作为更普遍点我会强烈建议您避免使用gca()和gcf()除了方便在交互式会话-更神谕的轴或图对象明确,这使得它的方式更容易跟踪哪些轴/数据被修改。