Exporting matplotlib plot to holoviews - python

I'm using a library called GPy to fit a Gaussian process model and plot the output. The library has it's own plotting functionality, and returns a matplotlib figure.
I'd like to use this output in a holoviews element, as part of a dynamic map. This feels like it should be possible, but I can't find a good way to do it.
I had wondered about reading the matplotlib figure into a numpy image array and sending this to a holoviews Raster element - but the only way to do this seems to be saving the figure to a file, which does not seem a good option.

Great question! At the point where you define holoviews elements, they are still backend-agnostic. Matplotlib etc. only come into play when they are actually rendered. Hence no, you cannot take a matplotlib figure as such and pipe that into a holoviews element.
Hence you have two options:
Extract the data from the matplotlib figure in some way, or get hold of those data from GPy in some other way, and create a holoviews element from that, or
Use the code that generates the matplotlib figure in a panel (https://panel.pyviz.org) app.
Number 2 is closer to what you are probably imagining but without a minimal working example I cannot say much more.

Related

Simplest way to publish matplotlib-like interactive animation to html?

I'm using matplotlib and python to make an animated scatter plot with points as 'balls' that bounce around, just like this:
https://jakevdp.github.io/downloads/videos/particle_box.mp4
It is interactive at runtime (with sliders to change velocity, attraction, etc), simulating on the go.
I would like to then publish it with the interactivity and all in html.
Problem: I don't know of a way to publish matplotlib interactivity to html directly.
So can I do it in python? Or is there a better way?
Which library (or program, if different from python) would you recommend as the simplest and fastest for this kind of project? I don't need "pretty" customizations and all that, I just need easy calculations and simulations (python) to then plot in a few easy lines of code (matplotlib). I do need to change the dots (balls) colors though.
I have looked at mpld3 that should wrap matplotlib around D3js. But I don't know anything about js and it is not very straightforward, so it would take me a bit to learn.
I have also looked at plotly, but it doesn't seem to have the same customization of the animation I need.
I have looked at Dash with plotly, but it would be a whole new environment to learn, and definitely overkill.
So the questions are: is there a way to output matplotlib interactive animations to html that I haven't found?
If not, what tools would you use to accomplish this project within a fast timeframe and shallow learning curve, based on my beginner/low intermediate python and matplotlib skill level?
Thank you!

difference between datashader and other plotting libraries

I want to understand the clear difference between Datashader and other graphing libraries eg plotly/matplotlib etc.
I understand that in order to plot millions/billions of data points, we need datashader as other plotting libraries will hung up the browser.
But what exactly is the reason which makes datashader fast and does not hung up the browser and how exactly the plotting is done which doesnt put any load on the browser ????
Also, datashader doesnt put any load on browser because in the backend datashader will create a graph on the basis of my dataframe and send only the image to the browser which is why its fast??
Plz explain i am unable to understand the in and out clearly.
It may be helpful to first think of Datashader not in comparison to Matplotlib or Plotly, but in comparison to numpy.histogram2d. By default, Datashader will turn a long list of (x,y) points into a 2D histogram, just like histogram2d. Doing so only requires a simple increment of a grid cell for each new point, which is easily accellerated to machine-code speeds with Numba and is trivial to parallelize with Dask. The resulting array is then at most the size of your display screen, no matter how big your dataset is. So it's cheap to process in a separate program that adds axes, labels, etc., and it will never crash your browser.
By contrast, a plotting program like Plotly will need to convert each data point into a JSON or other serialized representation, pass that to JavaScript in the browser, have JavaScript draw a shape into a graphics buffer, and make each such shape support hover and other interactive features. Those interactive features are great, but it means Plotly is doing vastly more work per data point than Datashader is, and requires that the browser can hold all those data points. The only computation Datashader needs to do with your full data is to linearly scale the x and y locations of each point to fit the grid, then increment the grid value, which is much easier than what Plotly does.
The comparison to Matplotlib is slightly more complicated, because with an Agg backend, Matplotlib is also pre-rendering to a fixed-size graphics buffer before display (somewhat like Datashader). But Matplotlib was written before Numba and Dask (making it more difficult to speed up), it still has to draw shapes for each point (not just a simple increment), it can't fully parallelize the operations (because later points overwrite earlier ones in Matplotlib), and it provides anti-aliasing and other nice features not available in Datashader. So again Matplotlib is doing a lot more work than Datashader.
But if what you really want to do is see the faithful 2D distribution of billions of data points, Datashader is the way to go, because that's really all it is doing. :-)
From the datashader docs,
datashader is designed to "rasterize" or "aggregate" datasets into regular grids that can be viewed as images, making it simple and quick to see the properties and patterns of your data. Datashader can plot a billion points in a second or so on a 16GB laptop, and scales up easily to out-of-core or distributed processing for even larger datasets.
There aren't any tricks going on in any of these libraries - rendering a huge number of points takes a long time. What datashader does is to shift the burden of visualization from rendering to computing. There's a very good reason you have to create a canvas before plotting instructions in datashader. The first step in a datashader pipeline is to rasterize a dataset, in other words, it approximates the position of each piece of data and then uses aggregation functions to determine the intensity or color of each pixel. This allows datashader to plot enormous numbers of points; even more points than can be held in memory.
Matplotlib, on the other hand, renders every single point you instruct it to plot, making plotting large datasets time consuming or even impossible.

Is there a way to add a title or a caption to the drawing using graph_tool

At least some workaround would be appreciated. I am trying to save the figures as pdfs and then use it with latex as an animation. If there is a way to automatically add captions that will be really helpful as I am going to have about 50-60 figures.
I don't know if it is possible natively, but you can put the graph drawn by graph-tool in a matplotlib Figure() using the mplfig option in graph_draw(). With that you have access to all the flexibility of matplotlib around your graph.

Can I get imshow() like behaviour in a wx.lib.plot.PlotCanvas?

I'm new to wxpython but have been a matplotlib user for some time now. I am developing a wxpython app in which I would like to have a simple frame with a single panel (actually, a wx.lib.plot.PlotCanvas instance). In the panel I want to show some data, which is in the form of a binary array (called imarr), like this one:
The array will always be of shape (64,N), and N can vary between about 400 and 1200 (I can sort out the panel sizing myself).
To do this in matplotlib I would use something like:
fig=figure(figsize=(12,2))
ax=fig.add_subplot(111)
ax.set_ylim(0,63)
ax.set_xlim(0,imarr.shape[0])
ax.set_aspect('equal')
matplotlib.imshow(imarr)
But I'm completely lost in wxpython. I have been looking into wx.lib.plot, but apart from the nice line and marker plotting capabilities, there doesn't seem to be an equivalent to imshow (or pcolor).
One (ugly) solution I have is to use matplotlib as above to create a png image and then use some wx magic to display the image on a panel (please set me straight if this won't be easy).
I'd much prefer to do it from within wxpython, for portability and general tidiness. Any advice on how to display a binary array on a wx.lib.plot.PlotCanvas (or a more generic wx.Panel) would be very useful!
Why don't you combine wxPython and matplotlib together? wx.lib.plot.PlotCanvas has limited functionality comparing to matplotlib. Have a look at an example in this answer and an example from the matplotlib documentation. Sorry that I didn't answer exactly what you are asking for but I think this is the right way to go.

Best way to create a 2D Contour Map with Python

I am trying to create a 2D Contour Map in Python that looks like this:
In this case, it is a map of chemical concentration for a number of points on the map. But for the sake of simplicity, we could just say it's elevation.
I am given the map, in this case 562 by 404px. I am given a number of X & Y coordinates with the given value at that point. I am not given enough points to smoothly connect the line, and sometimes very few data points to draw from. It's my understanding that Spline plots should be used to smoothly connect the points.
I see that there are a number of libraries out there for Python which assist in creation of the contour maps similar to this.
Matplotlib's Pyplot Contour looks promising.
Numpy also looks to have some potential
But to me, I don't see a clear winner. I'm not really sure where to start, being new to this programming graphical data such as this.
So my question really is, what's the best library to use? Simpler would be preferred. Any insight you could provide that would help get me started the proper way would be fantastic.
Thank you.
In the numpy example that you show, the author is actually using Matplotlib. While there are several plotting libraries, Matplotlib is the most popular for simple 2D plots like this. I'd probably use that unless there is a compelling reason not to.
A general strategy would be to try to find something that looks like what you want in the Matplotlib example gallery and then modify the source code. Another good source of high quality Matplotlib examples that I like is:
http://astroml.github.com/book_figures/
Numpy is actually a N-dimensional array object, not a plotting package.
You don't need every pixel with data. Simply mask your data array. Matplotlib will automatically plot the area that it can and leave other area blank.
I was having this same question. I found that matplotlib has interpolation which can be used to smoothly connect discrete X-Y points.
See the following docs for what helped me through:
Matplotlib's matplotlib.tri.LinearTriInterpolator docs.
Matplotlib's Contour Plot of Irregularly Spaced Data example
How I used the above resources loading x, y, z points in from a CSV to make a topomap end-to-end

Categories