我的 FuncAnimation 出現問題,更新 xlim 后我的注釋被洗掉。這是下面帶有預覽的代碼
您可以在此處的 google colab 中嘗試代碼
- FuncAnimation-annotated-static-x.gif
- xlim 是固定的
- ylim 是動態的

- FuncAnimation-annotated-static-y.gif
- xlim 是動態的
- ylim 是固定的

- FuncAnimation-annotated-dynamic-axes.gif
- xlim 和 ylim 是動態的

在 xlim 更新的兩種情況下,我的注釋會消失:
- FuncAnimation-annotated-static-y.gif
- FuncAnimation-annotated-dynamic-axes.gif
請注意,當 xlim 是靜態時,這不會發生:
- FuncAnimation-annotated-static-axes.gif
- FuncAnimation-annotated-static-x.gif
Does anyone know why this happens or how to update the xlim without removing annotations?
Please let me know if something is unclear / worded poorly as I really need to solve this.
uj5u.com熱心網友回復:
所以問題在于我如何移動我的注釋。這是修復:
# Don't do this - updating xlim will causing the annotation do disappear
# first_image_annotation_xybox = (x1_and_x2, y1)
# first_image_annotation.xybox = first_image_annotation_xybox
#
# second_image_annotation_xybox = (x1_and_x2, y2)
# second_image_annotation.xybox = second_image_annotation_xybox
for lnum, line in enumerate(lines):
# set data for each line separately.
line.set_data(xlist[lnum], ylist[lnum])
# Do this - Update our annotations
for ann in ann_list:
ann.remove()
ann_list = []
first_image_annotation = AnnotationBbox(
first_offset_image, (x1_and_x2, y1), xycoords='data', frameon=False)
ann_list.append(ax1.add_artist(first_image_annotation))
second_image_annotation = AnnotationBbox(
second_offset_image, (x1_and_x2, y2), xycoords='data', frameon=False)
ann_list.append(ax1.add_artist(second_image_annotation))
return lines, ann_list
其余代碼相同。想知道為什么在更新 xlim 而不是更新 ylim 時會發生這種情況 ˉ\ (ツ) /ˉ
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/331267.html
標籤:python python-3.x matplotlib matplotlib-basemap matplotlib-animation
上一篇:有沒有辦法用plotly或python創建條形圖可視化,其中y軸是24小時范圍,x軸是所有作業日?
下一篇:如何根據資料框的值更改邊緣顏色
