开发者

Draw a block of colour on top of a figure to create trails in python

I have a set of data which I am plotting using pylab 开发者_如何学Gowhich 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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜