2

以下の例を参照してください。2つのサブプロットが追加され、それぞれにLine2Dプロットが挿入されます。次に、2番目のサブプロットのLine2Dの軸を最初のサブプロットに変更します。出力から判断すると、get_geometryこれは成功です。ただし、実際の図では、2つの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

1つの問題は、最初の軸( )に、追加する線()が線のリスト()に含まれax_1ていないことだと思います。fax_1.lines

2番目のプロットから最初のプロットに線を「コピー」できます。

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 に答える
0

後で同じ質問をここに投稿しましたが、これが回答でした。

TL、DR サポートされていません。

于 2011-08-30T08:10:46.883 に答える