Editing
Code Guide
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Plotting and Animating=== Plotting data is simple using matplotlib and the documentation is full of information and examples on how to fully customise your plots. The following examples shows how simple it is to get a quick plot: <code> 1 import matplotlib as mpl 2 mpl.use(’TkAgg’) # macOS only! 3 import matplotlib.pyplot as plt 4 5 data = [some_data] 6 7 plt.plot(data) 8 plt.show() </code> Animating is a bit trickier and I found the examples confusing at first so here is an example of how to animate some data: <code> 1 import ma tplot l ib as mpl 2 mpl.use(’TkAgg’) 3 import matplotlib.pyplot as plt 4 import matplotlib.animation as animation 5 6 data = [some_data] 7 fig = plt.figure() 8 axis = fig.add_axes([0.1, 0.1, 0.8, 0.7]) 9 10 def animate (frames, kwargs): 11 plt.plot(data[frames]) 12 13 ani = animation.FuncAnimation(fig, animate, interval = 1000, frames = len(data), kwargs = None) 14 ani.save(’filename.mp4’) 15 plt.show() </code> Basically, the FuncAnimation function creates the animation using the user written function "animate" to create all the frames of the animation. The frames parameter is a generator (similar to a list of a range, e.g. frames = 10 is similar to, but not the same as frames = range(0, 10)). The interval between frames in this example is 1000ms, or 1s. The animate function can be as complex as necessary and as long as the frames input changes the plot, there will be an animation video at the end. I found that the video file that is created works better if the frames are short in duration (20ms or so). This means that an animation will usually need a large number of frames.
Summary:
Please note that all contributions to Derek may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Derek:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information