在 Matplotlib 中更改对象的轴后如何刷新图形?

发布于 2024-11-30 06:31:16 字数 1250 浏览 1 评论 0原文

请参考下面的例子。添加两个子图,并向每个子图插入一个 Line2D 图。然后,我将第二个子图中的 Line2D 轴更改为第一个子图。从 get_geometry 输出来看,这是成功的。然而,在实际图中,两个 Line2D 图仍然位于其原始子图中。

我在这里缺少什么?如何刷新图形以反映轴的变化?

显然这是一个相当愚蠢的例子,真正的应用程序更多的是动态的。

脚本:

import matplotlib.pyplot as plt

fig = plt.figure()  
ax_1 = fig.add_subplot(2,1,1)
ax_2 = fig.add_subplot(2,1,2)
ax_1.plot([0,1,2],[0,1,2])
ax_2.plot([0,1,2],[2,1,0])

print 'before'
for line in ax_1.get_lines():
    print line.get_ydata()
    print line.get_axes().get_geometry()
    print id(line.get_axes())

for line in ax_2.get_lines():
    print line.get_ydata()
    print line.get_axes().get_geometry()
    print id(line.get_axes())

f = ax_2.get_lines()[0]
f.set_axes(ax_1)

print 'after'
for line in ax_1.get_lines():
    print line.get_ydata()
    print line.get_axes().get_geometry()
    print id(line.get_axes())

for line in ax_2.get_lines():
    print line.get_ydata()
    print line.get_axes().get_geometry()
    print id(line.get_axes())

plt.show()

输出:

before
[0 1 2]
(2, 1, 1)
4330504912
[2 1 0]
(2, 1, 2)
4336262288
after
[0 1 2]
(2, 1, 1)
4330504912
[2 1 0]
(2, 1, 1)
4330504912

图输出: 图

Please refer to the example below. Two subplots are added, and to each a Line2D plot is inserted. I then change the Line2D's axes in the second subplot to be the first subplot. Judging by the get_geometry output this is successful. However in the actual figure the two Line2D plots are still in their original subplots.

What am I missing here? How can I refresh the figure to reflect the axes change?

Obviously this is a fairly moronic example, the real application is more of a dynamic nature.

Script:

import matplotlib.pyplot as plt

fig = plt.figure()  
ax_1 = fig.add_subplot(2,1,1)
ax_2 = fig.add_subplot(2,1,2)
ax_1.plot([0,1,2],[0,1,2])
ax_2.plot([0,1,2],[2,1,0])

print 'before'
for line in ax_1.get_lines():
    print line.get_ydata()
    print line.get_axes().get_geometry()
    print id(line.get_axes())

for line in ax_2.get_lines():
    print line.get_ydata()
    print line.get_axes().get_geometry()
    print id(line.get_axes())

f = ax_2.get_lines()[0]
f.set_axes(ax_1)

print 'after'
for line in ax_1.get_lines():
    print line.get_ydata()
    print line.get_axes().get_geometry()
    print id(line.get_axes())

for line in ax_2.get_lines():
    print line.get_ydata()
    print line.get_axes().get_geometry()
    print id(line.get_axes())

plt.show()

Output:

before
[0 1 2]
(2, 1, 1)
4330504912
[2 1 0]
(2, 1, 2)
4336262288
after
[0 1 2]
(2, 1, 1)
4330504912
[2 1 0]
(2, 1, 1)
4330504912

Figure output:
Figure

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

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

发布评论

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

评论(2

無處可尋 2024-12-07 06:31:16

我认为一个问题是您的第一个轴 (ax_1) 在其行列表 (ax_1) 中没有您想要添加的行 (f) .lines)。

您可以将第二个图中的线“复制”到第一个图中

f = ax_2.lines.pop()  # Removes the line from the second plot
ax_1.plot(*f.get_data())  # Draws a new line in the first plot, but with the coordinates of the second line

(使用此方法,显然不需要执行f.set_axes(ax_1))。我猜想,还可以使用 plot() 的其他参数来复制颜色等。

I believe that one problem is that your first axes (ax_1) do not have the line that you want to add (f) in their list of lines (ax_1.lines).

You can "copy" the line from the second plot to the first one with

f = ax_2.lines.pop()  # Removes the line from the second plot
ax_1.plot(*f.get_data())  # Draws a new line in the first plot, but with the coordinates of the second line

(with this method, there is obviously no need to do f.set_axes(ax_1)). Other arguments to plot() could be used in order to also copy the color, etc., I guess.

瀞厅☆埖开 2024-12-07 06:31:16

后来我在这里发布了同样的问题,以及这是响应。

TL,博士
不支持。

Later on I posted the same question here, and this was the response.

TL,DR
It's not supported.

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