スライダーで動的に変化する水平バー プロットを作成しようとしています。私は matplotib Web サイトのレシピに従っていますが、データに対してはうまく機能しline
ます。コード:
def interactive_pyramid(ages_matrix, year_labels):
fig, axs = plt.subplots()
plt.subplots_adjust(bottom=0.25)
l, = axs.plot(range(len(ages_matrix[0, :])), ages_matrix[0, :])
axs.annotate(year_labels[0], xy=(0.85, 0.85), xycoords="axes fraction")
axs.set_ylim([0,800])
pprint (dir(axs))
axcolor = 'lightgoldenrodyellow'
axyear = plt.axes([0.25, 0.1, 0.5, 0.1], axisbg=axcolor)
syear = Slider(axyear, 'Year', 0, ages_matrix.shape[0] - 1, 0)
def update(val):
year = syear.val
# axs.barh(range(len(ages_matrix[0, :])), ages_matrix[val, :])
l.set_ydata(ages_matrix[val, :])
# axs.annotate(year_labels[year], xy=(0.85, 0.85), xycoords="axes fraction")
axs.Annotation.remove()
fig.canvas.draw()
syear.on_changed(update)
ages_matrix は で2d ndarray
、 year_labels は1d ndarray
2 つの主な質問:
- メソッドを使用して
axs.barh()
オブジェクトを返さない場合はset_ydata()
、y データを変更できません。axs オブジェクトにデータを再度描画すると、以前の情報が消去されないため、チャートが乱雑になります。 - 注釈でも同じことが起こります - 前の注釈は消去されません。
斧を効率よく消して、もう一度引く方法はありますか?キャンバスをリフレッシュする方法はありますか?