在图形的顶部绘制一个色块,用python创建轨迹

2024-09-30 04:31:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一组数据,我正在绘制使用pylab,随着时间的推移而变化。你知道吗

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

我认为可以这样做的一个方法是plt.保持(True)在图上,然后在每个新时间点的数据顶部绘制一个轴大小的白色块(透明度值)alpha<;1。你知道吗

有人知道我怎么做吗?axisbg似乎不起作用。你知道吗

非常感谢

汉娜


Tags: 数据方法alphatrue绘图png轨迹格式
1条回答
网友
1楼 · 发布于 2024-09-30 04:31:39

在一系列绘图上实现淡入淡出轨迹的另一种方法是使用.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

相关问题 更多 >

    热门问题