在图形顶部绘制一个颜色块以在 python 中创建轨迹

发布于 2024-10-18 09:31:03 字数 265 浏览 1 评论 0原文

我有一组数据,我正在使用 pylab 绘制这些数据,这些数据会随着时间的推移而变化。

我可以将每个帧存储为 .png 并使用 iMovie 将它们放在一起,但我想向图中添加轨迹以说明之前时间点的位置。

我认为可以做到这一点的一种方法是在图上设置 plt.hold(True) ,然后在每个新时间在数据顶部绘制一个轴大小的白色块(透明度值) alpha<1观点。

有谁知道我该怎么做? axisbg 似乎不起作用。

非常感谢,

汉娜

I have a set of data which I am plotting using pylab which changes over time.

I can store each frame as a .png and put them together using iMovie, but I want to add trails to the plots to illustrate the position at previous time points.

One way I thought this could be done is to set plt.hold(True) on the figure, then to plot an axes-sized block of white colour with (the transparency value) alpha<1 on top of the data at each new time point.

Does anyone know how I can do this? axisbg doesn't seem to work.

Many thanks,

Hannah

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无声静候 2024-10-25 09:31:03

在一系列绘图上实现淡入淡出轨迹的另一种方法是使用 .set_alpha() 方法更改绘图项的 alpha 值(如果它适用于您所使用的特定绘图方法)使用。

您可以通过将您正在使用的特定绘图函数的输出(即绘图的“句柄”)附加到列表中来完成此操作。然后,在每个新绘图之前,您可以找到并减少该列表中每个现有项目的 alpha 值。

在以下示例中,使用 .remove() 从绘图中删除 alpha 值超出特定点的项目,然后从列表中删除它们的句柄。

import pylab as pl

#Set a decay constant; create a list to store plot handles; create figure.
DECAY = 2.0
plot_handles = []
pl.figure()

#Specific to this example: store x values for plotting sinusoid function
x_axis=pl.linspace( 0 , 2 * pl.pi , 100 )

#Specific to this example: cycle 50 times through 16 different sinusoid
frame_counter = 0
for phase in pl.linspace( 0 , 2 * pl.pi * 50 , 16 * 50 ):

    #Reduce alpha for each old item, and remove
    for handle in plot_handles:
        alpha = handle.get_alpha()
        if alpha / DECAY > 0.01 :
          handle.set_alpha( alpha / DECAY )
        else:
          handle.remove()
          plot_handles.remove( handle )

    #Add new output of calling plot function to list of handles
    plot_handles += pl.plot( pl.sin( x_axis + phase ) , 'bo' )

    #Redraw figure
    pl.draw()

    #Save image
    pl.savefig( 'frame_' + str( frame_counter ).zfill( 8 ) + '.png' )
    frame_counter += 1

An alternative way to achieve fading trails on a sequence of plots would be to change the alpha values of the plotted items using the .set_alpha() method, if it’s available for the particular plotting method that you’re using.

You can do this by appending the output of the particular plotting function you’re using (i.e. the ‘handle’ to the plot) to a list. Then, before each new plot you can find and reduce the alpha values of each existing item in that list.

In the following example, items whose alpha values drop beyond a certain point are removed from the plot using .remove() and their handle is then removed from the list.

import pylab as pl

#Set a decay constant; create a list to store plot handles; create figure.
DECAY = 2.0
plot_handles = []
pl.figure()

#Specific to this example: store x values for plotting sinusoid function
x_axis=pl.linspace( 0 , 2 * pl.pi , 100 )

#Specific to this example: cycle 50 times through 16 different sinusoid
frame_counter = 0
for phase in pl.linspace( 0 , 2 * pl.pi * 50 , 16 * 50 ):

    #Reduce alpha for each old item, and remove
    for handle in plot_handles:
        alpha = handle.get_alpha()
        if alpha / DECAY > 0.01 :
          handle.set_alpha( alpha / DECAY )
        else:
          handle.remove()
          plot_handles.remove( handle )

    #Add new output of calling plot function to list of handles
    plot_handles += pl.plot( pl.sin( x_axis + phase ) , 'bo' )

    #Redraw figure
    pl.draw()

    #Save image
    pl.savefig( 'frame_' + str( frame_counter ).zfill( 8 ) + '.png' )
    frame_counter += 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文