Dynamically updating images on website - python

I am accessing the twitter streaming API. I generate a map using Basemap in python.
I want only certain parts of the map to change with time (for eg. every second). Is it hard to do?
Do I need to leave Basemap and look for something else? Please help!

You can send an ajax request and update the html contents dynamically.

A possible approach: divide the map into tiles, and treat each one separately; use Basemap to generate just the map-tile that contains new data, then update just that tile on your webpage using Ajax.
Of course, depending on the nature of changes to the data on your map, this approach may or may not work for you -- gerrymandering is not really possible.
You would need to write logic to understand which tile the new data belongs to, then use basemap to create a new image for that time, then intelligently update the tiled image. You will also have to play with margins and padding (both in matplotlib and in CSS) to cleanly piece the tiles together.
...
When the approach gets this complicated, one should re-evaluate whether better tools are available. Basemap doesn't sound like a good fit for what you need to do.

Related

Is it possible in Python to create a custom made polygon and use that as arrow to produce a stream plot?

i want to create a plot with 13 different arrow styles for a vector field showing the stream of a sea. I want to use very specific arrows which are not bult in FancyArrowPatch. Is ther a possibility to use a selfmade design as arrow in python ? Is it possible to somehow add custom arrow styles to the FancyArrowPatch function ?
I have attached the image I want to reproduce with Python (taken from bsh.de/DE/DATEN/Stroemungen/stroemungen_node.html):
My best try was to produce this one:
Which is not quite satisfying yet. I would like to have the same arrows as seen in the first image. If you have suggestions how to make the image fancier I am open for that too.

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.

Draw a map with gps markers and save this image to file

I have some GPS position recordings, which I want to draw into a map (e.g. OpenStreetMap) and save this as an image file.
I'm using python. I've found osm-gps-map as a candidate library to do the visualization, but apparantly the API lacks some functionality to satisfy all of my needs. What I need is a library that does:
draw stuff into a map (e.g. an OSM-based map), i.e. a list of GPS positions.
automatically adjust view (scale/position) to fit in all drawn positions
save this view to an image file
I seriously want to avoid being forced to manually make a screenshot from a widget or a browser window. And if possible I also want to avoid to implement the necessary projection functionality by myself (as in here). Does anyone know a library/toolchain that provides the desired functionality (if possible for use by Python)?
You may want to consider using Basemap for matplotlib. Here is a blog post describing how to use this package with OSM.
There are examples of how to draw custom GPS points on top of OSM background map using Matplotlib Basemap, Matplotlib or Cairo in GeoTiler project: https://github.com/wrobell/geotiler/tree/master/examples.

Right approach for speeding up presentation of large amount of data?

First question ever on stackoverflow. Been reading for some time now, while trying to learn Python and wxPython.
I'm writing a small app for presenting a large amount of data on the screen in a custom way. The data is stock information stored in objects in Python. Its about 100 stocks that should be presented at the same time on the screen. Every stockobject has 35 attributes, so it makes 3 500 attributes showing at the same time. And I want different fonts, size and colour depending on attribute. The background of each stockobject is changing depending on user (me) input.
So I tried making a interface with wxPython and a lot of StaticText controls. It took 5 seconds to load, timing it with timeit module.
Googling the net gave me an idea to draw the data on a device context instead. That took only 0.1 second. To make the app clickable I draw a second picture into memory with specific colours for each attribute. When clicking on the panel showing the picture I compare the coordinates with the DC in memory to calculating what was clicked. And now I am about to write a sizer routine so the user can change fontsize.
Well my question is quit simple: Do you think I chose the right approach?
Or is there a simpler more pythonic way to do this, without using StaticText that took forever to load?
Grids is not a solution for me, because I want the data to be presented in a very specific layout. To be able to do that with a grid, I would have to set the grid to 2px with and hight, and then merge cells all over the place...
edit:
link for downloading picture of the controll as it looked yesterday:
http://dl.dropbox.com/u/10606669/super.png
Ugly and not the exact way I want it to lok. This because of trying to write my own sizer routine.
You can try freezing the whole frame during the loading process, like this:
frame.Freeze()
try:
# load all data
finally:
frame.Thaw()
In general, though, having that many Window controls will hurt performance, so custom drawing is the only solution. You could simplify things a little by creating your own custom control for one stock (with its own EVT_PAINT handler, etc.) and then creating 100 of them. It should make your DC calculations easier. See Creating Custom Controls for more information.

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