Plotting distribution from sampled data in python - python

I have two sets of sampled points in 2d space[x ,y], each set represents one class. When I plot all points, it's mess and one can't see anything on it. I need somehow plot distribution of each set (if it's possible on same canvas with different colours, then better). Does anybody know about some good library for it?

Matplotlib is a very good library for that task. You can plot histograms, scatter plots and lot of other things. You just have to know what you want and then you can probably do it with that. I use that for similar tasks a lot.
[UPDATE]
As I said, you can do that with matplotlib. Here is an example from their gallery: http://matplotlib.org/examples/pylab_examples/scatter_hist.html
It's not so pretty as with the answer in the comment of #lejlot, but still correct.

Related

How to draw a plot by using python

I am implementing the Fastdtw algorithm to find the optimal path to align two time-series data. I hope to output a plot like this:
However, I've never tried such kind of plot before. I guess maybe I need to use the imshow() function in matplotlib, but I don't know how to draw the extra trajectory in the plot.
I wish somebody coould give a similar example about drawing like such style. I will modify the parameters by myself.

Using matplotlib when one should use `axes`?

Let me first say I've never studied matplotlib and pyplot organically. I've been able to employ these fantastic tools to some success however without developing a big picture of how things are supposed to be used. One of the thing that confuse me the most is the use of axes instances.
In many occasions I have found solutions to a particular problem which make use of axes or do not.
Example: one can set ticks to the x-axis through matplotlib.pyplot.xticks without using axes or through matplotlib.axes.Axes.set_xticks.
I usually try to avoid the use of axes. However there are times at which this seems to not be possible. For example when trying to use subplots with shared axis.
Can someone help me to shed some light on this subject? When am i supposed to use axes? When should I not?
Thanks.

What is the best way to create a figure with distributions as insets in python?

I created a graph in MATLAB (see figure below) such that around every data point there is a data distribution plotted (grey area plots). The way I did it in MATLAB was to create a set of axes for every distribution curve and then plot the curves without showing those axes at every point of the data curve. I also used a command 'linkaxes' to set figure limits for all the curves at once.
I must say that this is far from an elegant solution and I had many troubles with saving this figure in the correct aspect ratio settings. All in all I couldn't find any other useful option in MATLAB.
Is there a more elegant solution for such types of graphs in Python? I am not that much interested in how to do the areas highlighted, but how to place a set of curves(distributions) exactly at the positions of the main data curve points.
Thank you!

Barchart (o plot) 3D in Python

I need to plot some data in various forms. Currently I'm using Matplotlib and I'm fairly happy with the plots I'm able to produce.
This question is on how to plot the last one. The data is similar to the "distance table", like this (just bigger, my table is 128x128 and still have 3 or more number per element).
Now, my data is much better "structured" than a distance table (my data doesn't varies "randomly" like in a alphabetically sorted distance table), thus a 3D barchart, or maybe 3 of them, would be perfect. My understanding is that such a chart is missing in Matplotlib.
I could use a (colored) Countor3d like these or something in 2D like imshow, but it isn't really well representative of what the data is (the data has meaning just in my 128 points, there isn't anything between two points). And the height of bars is more readable than color, IMO.
Thus the questions:
is it possible to create 3D barchart in Matplotlib? It should be clear that I mean with a 2D domain, not just a 2D barchart with a "fake" 3D rendering for aesthetics purposes
if the answer to the previous question is no, then is there some other library able to do that? I strongly prefer something Python-based, but I'm OK with other Linux-friendly possibilities
if the answer to the previous question is no, then do you have any suggestions on how to show that data? E.g. create a table with the values, superimposed to the imshow or other colored way?
For some time now, matplotlib had no 3D support, but it has been added back recently. You will need to use the svn version, since no release has been made since, and the documentation is a little sparse (see examples/mplot3d/demo.py). I don't know if mplot3d supports real 3D bar charts, but one of the demos looks a little like it could be extended to something like that.
Edit: The source code for the demo is in the examples but for some reason the result is not. I mean the test_polys function, and here's how it looks like:
example figure http://www.iki.fi/jks/tmp/poly3d.png
The test_bar2D function would be even better, but it's commented out in the demo as it causes an error with the current svn version. Might be some trivial problem, or something that's harder to fix.
MyavaVi2 can make 3D barcharts (scroll down a bit). Once you have MayaVi/VTK/ETS/etc. installed it all works beautifully, but it can be some work getting it all installed. Ubuntu has all of it packaged, but they're the only Linux distribution I know that does.
One more possibility is Gnuplot, which can draw some kind of pseudo 3D bar charts, and gnuplot.py allows interfacing to Gnuplot from Python. I have not tried it myself, though.
This is my code for a simple Bar-3d using matplotlib.
import mpl_toolkits
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
%matplotlib inline
## The value you want to plot
zval=[0.020752244,0.078514652,0.170302899,0.29543857,0.45358061,0.021255922,0.079022499,\
0.171294169,0.29749654,0.457114286,0.020009631,0.073154019,0.158043498,0.273889264,0.419618287]
fig = plt.figure(figsize=(12,9))
ax = fig.add_subplot(111,projection='3d')
col=["#ccebc5","#b3cde3","#fbb4ae"]*5
xpos=[1,2,3]*5
ypos=range(1,6,1)*5
zpos=[0]*15
dx=[0.4]*15
dy=[0.5]*15
dz=zval
for i in range(0,15,1):
ax.bar3d(ypos[i], xpos[i], zpos[i], dx[i], dy[i], dz[i],
color=col[i],alpha=0.75)
ax.view_init(azim=120)
plt.show()
http://i8.tietuku.com/ea79b55837914ab2.png
You might check out Chart Director:
http://www.advsofteng.com
It has a pretty wide variety of charts and graphs and has a nice Python (and several other languages) API.
There are two editions: The free version puts a blurb on the generated image, and the
pay version is pretty reasonably priced.
Here's one of the more interesting looking 3d stacked bar charts:
(source: advsofteng.com)

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