Simple animation in Python using wxPython objects - python

I'm writing a gravity simulator in Python 2.7 and at the moment I finished the mathematical part. I'm trying to find a way to display the results while the simulation is running, it should consist of some colored circles representing the various bodies and optionally some curved lines to represent orbits, that can be shown or hidden while the simulation is running.
I pictured a way to obtain this result, but I can't seem to find a way to even start.
My idea is to use wxPython. The window should be divided into four sectors (2x2), the first three contain the simulation viewed in the XY, XZ and YZ planes, while the last contains the controls (start/stop simulation, show/hide orbits, ...).
The controls should not be a problem, I just need a way to display the animation. So how can I display moving circles and curved lines using wxPython objects? Which objects should I use? I don't need much more than a couple names, the rest should follow easily.
I know that an animation purely with wxPython will probably require some multithreading, I'm already prepared for that. I also want to stress that I need the animation to be shown while the simulation is running, not after, because the simulation has no definite end at the moment: I don't know when to stop it if I don't see the results first.
If it's somehow useful, I'm using Ubuntu Linux 17.10.
Edit: Since I was asked to choose one approach, I discarded Matplotlib because it requires two different windows. Hope this helps.

Related

WxPython zooming technique

I am developing a wxpython project where I am drawing a diagram on to a panel that I need to be able to zoom in/out to this diagram(a directed acyclic graph in my case). I will achieve this by mouse scroll when the cursor is on the panel, however that is not a part of my question. I need an advice from an experienced person about the method I am using for zooming. So far I thought as doing,
There are lines, rectangles and texts inside rectangles within this diagram. So maybe I could increase/decrease their length/size with the chosen mouse event. But it is hard to keep it balanced because rectangles are connected with lines their angles should not change, and texts inside the rectanges should stay in the middle of them.
Other method I thought of doing is to search for a built-in zoom method. Which I heard about something like Scale. However I have some questions about this method. Will this work on vector drawings(like mine) rather than images. And will it be scaling only the panel I chose and not the whole screen ? After I hear your advice about this, I will look deeper into this, but now I am a bit clueless.
Sorry if my question is too theoretical. But I felt I needed help in the area. Thanks in advance.
Note: Zooming not necessarily applied by scrolling.
Note2: My research also led me to FloatCanvas. Is this suitable to my needs ?
Yes, from your description FloatCanvas would certainly meet your needs.
Another possibility to consider would be the wx.GraphicsContext and related classes. It is vector-based (instead of raster) and supports the use of a transformation matrix which would make zooming, rotating, etc. very easy. However, the actual drawing and management of the shapes and such would probably require more work for you than using FloatCanvas.

Simple animations in Python 2.7 [duplicate]

This question already has answers here:
simple animation using tkinter
(2 answers)
Closed 6 years ago.
In Python 2.7, I want to display some simple graphics, such as a red square moving around on a blue window. On every frame, I want to update the position of the square, and render the new window. I am looking for something simple and lightweight.
I have seen people use matplotlib for drawing shapes, but I don't want to have to deal with axes and data points. I have also seen pygame suggested, but this seems to heavyweight for what I want, as I do not want to create a game, just a simple animation.
So what I really want is something where on every frame, I just indicate the colour of every pixel on an image, and then display that image. What are some good suggestions?
Tkinter is not good for setting individual pixels. If you want to move rectangles or ovals though (a small oval will look like a pixel, but it doesn't scale for updating a whole image).
def update(x,y):
canvas.delete('all')
canvas.create_rectangle(x-1,y-1,x+1,y+1)
You can, of course, be more judicious, saving the return value of the rectangle and then only clear the appropriate elements. Or you can move existing elements directly as Bryan points out. As he explains elsewhere Tkinter of course, supports drawing images, ovals, and a slew of other things. Here's a canonical source edit: that is old and not canoncial This one's slightly better For a general source on animating with a timer loop, here's Bryan agian
Bryan also noted that you can work with pixels directly You can do that with PhotoImage.
Array-Like Pixel Access Without Graphical Extensions
A robust module like pygame will be the most scalable option. However, I've had success (in educational settings only) writing graphics engines by modifying the elements of a numpy array and then displaying it as an image (you also need this link to display the images).
This lets you do pixel level modifications; since it's relatively trivial to write C-extensions that modify numpy arrays, you can prototype fast image processing doing custom manipulations. While I've written whole graphics engines using just tkinter this way, again I can only reccomend it for educational purposes.
Otherwise, just bite the bullet and pull in openGl or pygame. You'll save yourself a ton of time in the long run.
Summary
Very simple animations can be done right in tkinter
For educational purposes, you can do arbitrary graphics with numpy and tkinter
For rhobust animations, check out a full library (openGl, matplotlib, pygame) that suits your needs (graphical rendering, statistical graphing, game development, etc.)

How to draw a graph that can indicate the values when the mouse moves to some part of the graph in python and put it on the web page?

I'm writing a web interface for a database of genes values of some experiments with CGI in Python and I want to draw a graph for the data queried. I'm using matplotlib.pyplot, draw a graph, save it, and perform it on the web page. But usually there are many experiments queried hence there are a lot of values. Sometimes I want to know which experiment does one value belong to because it's a big value, whereas it's hard to identify because the picture is small in size. The names of the experiments are long strings so that it will mess the x axis if I put all the experiment names on the x axis.
So I wonder if there is a way to draw a graph that can interact with users, i.e. if I point my mouse to some part on the graph, there would be one small window appears and tells me the exact value and what is the experiment name here. And the most important is, I can use this function when I put the graph on the web page.
Thank you.
What you want is basically D3.js rendering of your plots. As far as I know, there are currently three great ways of achieving this, all under rapid development:
MPLD3 for creating graphs with Matplotlib and serving them as interactive web graphics (see examples in Jake's blog post).
Plotly where you can either generate the plots directly via Plotly or from Matplotlib figures (e.g. using matplotlylib) and have them served by Plotly.
Bokeh if you do not mind moving away from Matplotlib.

What would you use to create an interactive geometry program with python?

What libraries/modules would you recommend for creating an interactive geometry program?
What I have found includes: Pyglet, Pygame, Pycairo, Sympy
I'll illustrate the basic requirements with an example:
Create two Point objects by clicking two locations on a canvas.
Create two intersecting Circle objects based on those two Points.
Detect the two circles and calculate their intersections.
I would like to work with a canvas that supports anti-aliasing.
I would also like the ability to produce(export) an image with a transparent background.
If you have a bit of experience with OpenGL I would seriously recommend pyglet. Very customizable. If you don't, get it, because in Python there doesn't really exist a game/rendering library that has a "canvas-style" interface, except, to a certain point, pygame. But pygame...
Stay away from pygame. Stay away. pygame has horrendous design, documentation, code written with it and performance.
I have no experience with the two others (or any others, for that fact). There aren't many options for this domain in Python sadly (this also is the reason why I started a project like this, but it's still too unstable and WIP). Most notably the built-in support for things like anti-aliasing, primitives and intersection calculating is non-existent.
I have created a library for interactive geometry board using tkinter.
http://bitbucket.org/zambu/pygraph

Scientific Plotting in Python

I have a large data set of tuples containing (time of event, latitude, longitude) that I need to visualize. I was hoping to generate a 'movie'-like xy-plot, but was wondering if anyone has a better idea or if there is an easy way to do this in Python?
Thanks in advance for the help,
--Leo
get matplotlib
The easiest option is matplotlib. Two particular solutions that might work for you are:
1) You can generate a series of plots, each a snapshot at a given time. These can either be displayed as a dynamic plot in matplotlib, where the axes stay the same and the data moves around; or you can save the series of plots to separate files and later combine them to make a movie (using a separate application). There a number of examples in the official examples for doing these things.
2) A simple scatter plot, where the colors of the circles changes with time might work well for your data. This is super easy. See this, for example, which produces this figure
alt text http://matplotlib.sourceforge.net/plot_directive/mpl_examples/pylab_examples/ellipse_collection.hires.png
I'd try rpy. All the power of R, from within python.
http://rpy.sourceforge.net/
rpy is awesome.
Check out the CRAN library for animations,
http://cran.r-project.org/web/packages/animation/index.html
Of course, you have to learn a bit about R to do this, but if you're planning to do this kind of thing routinely in future it will be well worth your while to learn.
If you are interested in scientific plotting using Python then have a look at Mlab: http://code.enthought.com/projects/mayavi/docs/development/html/mayavi/mlab.html
It allows you to plot 2d / 3d and animate your data and the quality of the charts is really high.
Enthought's Chaco is designed for interactive/updating plots. the api and such takes a little while to get use to, but once you're there it's a fantastic framework to work with.
I have had reasonable success with Python applications generating SVG with animation features embedded, but this was with a smaller set of elements than what you probably have. For example, if your data is about a seismic event, show a circle that shows up when the event happened and grows in size matching the magnitude of the event. A moving indicator over a timeline is really simple to add.
Kaleidoscope (Opera, others maybe, Safari not) shows lots of pieces moving around and I found inspirational. Lots of other good SVG tutorial content on the site too.
You might want to look at PyQwt. It's a plotting library which works with Qt/PyQt.
Several of the PyQwt examples (in the qt4examples directory) show how to create "moving" / dynamically changing plots -- look at CPUplot.py, MapDemo.py, DataDemo.py.

Categories

Resources