2

请参考以下示例。添加了两个子图,并在每个子图中插入了一个 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

图输出: 数字

4

2 回答 2

0

后来我在这里发布了同样的问题,就是回应。

TL,DR 不支持。

于 2011-08-30T08:10:46.883 回答
0

我相信一个问题是您的第一个轴 ( ax_1) 没有您要f在其行列表 ( ) 中添加 ( ) 的行ax_1.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()复制颜色等。

于 2011-08-19T05:55:16.627 回答