gpt4 book ai didi

python - 在保留 plot matplotlib 的同时删除注释

转载 作者:太空宇宙 更新时间:2023-11-03 12:44:47 24 4
gpt4 key购买 nike

我正在制作一系列散点图,其中我保留了每个图之间的大部分图(散点图除外)。这是这样做的:Keeping map overlay between plots in matplotlib

现在我想给情节添加注释:

for j in range(len(n)):
plt.annotate(n[j], xy = (x[j],y[j]), color = "#ecf0f1", fontsize = 4)

但是,此注释保留在绘图之间的绘图上。如何在每张图保存后清除注释?

最佳答案

您可以使用 remove() 删除艺术家。

ann = plt.annotate (...)
ann.remove()

删除后可能需要重新绘制 Canvas 。


这是一个完整的示例,删除动画中的多个注释:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation

fig, ax = plt.subplots()

x = np.arange(0, 2*np.pi, 0.01)
f = lambda x: np.sin(x)
line, = ax.plot(x, f(x))

scat = plt.scatter([], [], s=20, alpha=1, color="purple", edgecolors='none')
ann_list = []

def animate(j):
for i, a in enumerate(ann_list):
a.remove()
ann_list[:] = []

n = np.random.rand(5)*6
scat.set_offsets([(r, f(r)) for r in n])
for j in range(len(n)):
ann = plt.annotate("{:.2f}".format(n[j]), xy = (n[j],f(n[j])), color = "purple", fontsize = 12)
ann_list.append(ann)

ani = matplotlib.animation.FuncAnimation(fig, animate, frames=20, interval=360)
ani.save(__file__+".gif",writer='imagemagick', fps=3)
plt.show()

enter image description here

关于python - 在保留 plot matplotlib 的同时删除注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42314525/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com