在 Matplotlib 中更改对象的轴后如何刷新图形?
请参考下面的例子。添加两个子图,并向每个子图插入一个 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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为一个问题是您的第一个轴 (
ax_1
) 在其行列表 (ax_1) 中没有您想要添加的行 (
)。f
) .lines您可以将第二个图中的线“复制”到第一个图中
(使用此方法,显然不需要执行
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
(with this method, there is obviously no need to do
f.set_axes(ax_1)
). Other arguments toplot()
could be used in order to also copy the color, etc., I guess.后来我在这里发布了同样的问题,以及这是响应。
TL,博士
不支持。
Later on I posted the same question here, and this was the response.
TL,DR
It's not supported.