【问题标题】:altering the size of a marker moving in a line in animated line plot更改动画线图中沿线移动的标记的大小
【发布时间】:2021-05-10 11:50:14
【问题描述】:

在 Python/matplotlib 程序中,我试图更改动画图中标记的大小,以使用 python 显示数组的大小。

在此代码中,如果标记移动到 x[i],y[i],则标记的大小应为 s[i]。这意味着标记点 x[i],y[i] 的大小应该显示 s[i] 在该点的值。任何关于如何制作这样一个情节的建议将不胜感激。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
x = np.linspace(0, 10, 100)
y = np.sin(x)
s = np.random.randint(10,20,100)
fig, ax = plt.subplots()
scat, = ax.plot(x[0], y[0], 'o', markersize = s[0])
def update(i, x, y, scat):
    scat.set_data(x[i], y[i])
    scat.axes.axis([0, 10, -1, 1])
    return scat,
ani = animation.FuncAnimation(fig, update, len(x), fargs=[x,y, scat],
                              interval=25, blit=False)
plt.show()*

【问题讨论】:

    标签: python matplotlib animation


    【解决方案1】:

    matplotlib.lines.Line2D 中的函数 set_markersize 允许像更新数据一样更新标记大小:

    def update(i, x, y, scat):
        scat.set_data(x[i], y[i])
        scat.set_markersize(s[i])
        scat.axes.axis([0, 10, -1, 1])
        return scat,
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-08
      • 2017-07-30
      • 1970-01-01
      相关资源
      最近更新 更多