3

私はこのコードを持っています:

from pylab import *
from mpl_toolkits.axes_grid.anchored_artists import AnchoredText
import numpy as np
import matplotlib.pylab as plt


fig = plt.figure()
ax = fig.add_subplot(111)

ann = AnchoredText('If you zoom in or out, i stay here\nbut can you update this text?',
                  prop=dict(size=8), frameon=True,
                  loc=2,
                  )
ann.patch.set_boxstyle("round,pad=0.,rounding_size=0.2")
ax.add_artist(ann) 

delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = (Z1 - Z2) * 10

plt.contourf(X, Y, Z)

plt.show()

set_textテキストを常に表示するように固定する方法はありますが、テキストオブジェクトのようなメソッドを使用して、含まれているテキストを更新することもできますか?ここでは、完全に固定されているAnchoredTextを使用しましたが、含まれているテキストを変更するメソッドが見つかりません。AnchoredText Matplotlibのドキュメントでは、そのようなメソッドを利用できません。AnchoredTextで実行できない場合、単純なテキストオブジェクトで実行できますか?

編集

私はDavidZwickerからの解決策を受け入れました。これは、それを必要としている人のための実用的な例です。

from pylab import *
from mpl_toolkits.axes_grid.anchored_artists import AnchoredText
import numpy as np
import matplotlib.pylab as plt
import random


fig = plt.figure()
ax = fig.add_subplot(111)

ann = AnchoredText('If you zoom in or out, i stay here\nbut can you update this text?$4.1f$ km s$^{-1}$',
                  prop=dict(size=15), frameon=True,
                  loc=2,
                  )
ann.patch.set_boxstyle("round,pad=0.,rounding_size=0.2")
ax.add_artist(ann) 

delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = (Z1 - Z2) * 10

plt.contourf(X, Y, Z)

a = ["hhhhh", "qqqqq", "aaaaaaa","ttttt","yyyyy","oooooooo"]

def setanntext(self):
         ann.txt.set_text(random.choice(a))
         plt.draw()

buttonax = plt.axes([0.7, 0.05, 0.1, 0.1])
button = Button(buttonax, 'set text',color='0.85', hovercolor='0.95')
button.on_clicked(setanntext)

plt.show()
4

1 に答える 1

5

属性にアクセスできますtxt。アンカーテキストは、次を使用して更新できます

ann.txt.set_text('new text')
plt.draw()

plt.draw()プロットを更新して新しいテキストを表示するには、が必要です。

于 2012-11-02T14:24:21.620 に答える