 # Smooth contourplots

It sounds like you’re wanting a gaussian kernel density estimate (KDE) (not the desktop!). The other options you mentioned are for interpolation, and are not at all what you’re wanting to do.

You can use scipy.stats.kde.gaussian_kde(). However, it currently doesn’t take a weights array, so you’ll need to modify it for your use case.

If you prefer, I have faster version of a gaussian KDE that can take a weights array. It’s actually slower than the scipy’s gaussian kde for a low number of points, but for hundreds, thousands, or millions of points, it’s several orders of magnitude faster. (Though the speedup depends on the covariance of the points… higher covariance = slower, generally speaking)

Here’s a quick pastebin of the code. http://pastebin.com/LNdYCZgw

To use it, you do something like the below… (assuming the code in the pastebin is saved in a file called fast_kde.py)

import numpy as np
import matplotlib.pyplot as plt
from fast_kde import fast_kde

weights, x, y = np.loadtxt(‘chain.txt’, usecols=(0,4,6)).T

kde_grid = fast_kde(x, y, gridsize=(200,200), weights=weights)

# Plot the grid

plt.figure()
plt.imshow(kde_grid, extent=(x.min(), x.max(), y.max(), y.min())

# Reverse the y-axis

plt.gca().invert_yaxis()

plt.show()

Hope that helps a bit,
-Joe

···

On Sat, Jul 24, 2010 at 3:56 AM, montefra <franz.bergesund@…982…> wrote:

Hi,

I am writing a program that reads three columns (one column containing the

weights, the other two containing the values I want to plot) from a file

containing the results from a MonteCarlo Markov Chain. The file contains

thousends of lines. Then create the 2D histogram and make contourplots. Here

is a sample of the code (I don’t know if is correct, it’s just to show what

I do)

import numpy as np

import matplotlib.pyplot as mplp

weights), 4 and 6 (the data), from the file “chain.txt”

h2D, xe, ye = np.histogram2D(chain[:,1],chain[:,2], weights=chain[:,0])

#create the 2D histogram

x = (xe[:-1] + xe[1:])/2. #x and y values for the plot (I use the mean

of each bin)

y = (ye[:-1] + ye[1:])/2.

mplp.figure() #open the figure

mplp.contourf(x, y, h2D.T, origin=‘lower’) #contour plot

As it is the contours are not smooth and they look not that nice. After days

of searches I’ve found three methods and tried, unsuccesfully, to apply them

1. 2d interpolation: I got “segmentation fault” (on a quadcore machine with

8Gb of RAM)

1. Rbf (radial basis functions): I got wrong contours

2. ndimage: it creates spurious features (like secondary peaks parallel to

the direction of the main one)

Before beginning with Python, I used to use IDL to plot, and there is a

function ‘smooth’ that smooth for you 2D histograms. I haven’t found

anything similar for Python.

Does anyone have an idea or suggestion on how to do it?

Francesco

View this message in context: http://old.nabble.com/Smooth-contourplots-tp29253884p29253884.html

Sent from the matplotlib - users mailing list archive at Nabble.com.

The Palm PDK Hot Apps Program offers developers who use the

Plug-In Development Kit to bring their C/C++ apps to Palm for a share

of \$1 Million in cash or HP Products. Visit us here for more details:

http://clk.atdmt.com/CRS/go/247765532/direct/01/

Matplotlib-users mailing list

Matplotlib-users@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Dear Joe,

finally I had time to come back to my python scritp for the contour plots.
You're code works very nicelly and does exactly what I need.

Thank you for the help

Francesco

2010/7/26 Joe Kington <jkington@...150...>:

···

It sounds like you're wanting a gaussian kernel density estimate (KDE) (not
the desktop!). The other options you mentioned are for interpolation, and
are not at all what you're wanting to do.

You can use scipy.stats.kde.gaussian_kde(). However, it currently doesn't
take a weights array, so you'll need to modify it for your use case.

If you prefer, I have faster version of a gaussian KDE that can take a
weights array. It's actually slower than the scipy's gaussian kde for a low
number of points, but for hundreds, thousands, or millions of points, it's
several orders of magnitude faster. (Though the speedup depends on the
covariance of the points... higher covariance = slower, generally speaking)

Here's a quick pastebin of the code. http://pastebin.com/LNdYCZgw

To use it, you do something like the below... (assuming the code in the
pastebin is saved in a file called fast_kde.py)

import numpy as np
import matplotlib.pyplot as plt
from fast_kde import fast_kde

weights, x, y = np.loadtxt('chain.txt', usecols=(0,4,6)).T

kde_grid = fast_kde(x, y, gridsize=(200,200), weights=weights)

# Plot the grid
plt.figure()
plt.imshow(kde_grid, extent=(x.min(), x.max(), y.max(), y.min())

# Reverse the y-axis
plt.gca().invert_yaxis()

plt.show()

Hope that helps a bit,
-Joe

On Sat, Jul 24, 2010 at 3:56 AM, montefra <franz.bergesund@...982...> > wrote:

Hi,

I am writing a program that reads three columns (one column containing the
weights, the other two containing the values I want to plot) from a file
containing the results from a MonteCarlo Markov Chain. The file contains
thousends of lines. Then create the 2D histogram and make contourplots.
Here
is a sample of the code (I don't know if is correct, it's just to show
what
I do)

>>> import numpy as np
>>> import matplotlib.pyplot as mplp
>>> (the
>>> weights), 4 and 6 (the data), from the file "chain.txt"
>>> h2D, xe, ye = np.histogram2D(chain[:,1],chain[:,2],
>>> weights=chain[:,0])
>>> #create the 2D histogram
>>> x = (xe[:-1] + xe[1:])/2. #x and y values for the plot (I use the mean
>>> of each bin)
>>> y = (ye[:-1] + ye[1:])/2.
>>> mplp.figure() #open the figure
>>> mplp.contourf(x, y, h2D.T, origin='lower') #contour plot

As it is the contours are not smooth and they look not that nice. After
days
of searches I've found three methods and tried, unsuccesfully, to apply
them
1) 2d interpolation: I got "segmentation fault" (on a quadcore machine
with
8Gb of RAM)
2) Rbf (radial basis functions): I got wrong contours
3) ndimage: it creates spurious features (like secondary peaks parallel to
the direction of the main one)

Before beginning with Python, I used to use IDL to plot, and there is a
function 'smooth' that smooth for you 2D histograms. I haven't found
anything similar for Python.
Does anyone have an idea or suggestion on how to do it?

Francesco

--
View this message in context:
http://old.nabble.com/Smooth-contourplots-tp29253884p29253884.html
Sent from the matplotlib - users mailing list archive at Nabble.com.

------------------------------------------------------------------------------
The Palm PDK Hot Apps Program offers developers who use the
Plug-In Development Kit to bring their C/C++ apps to Palm for a share
of \$1 Million in cash or HP Products. Visit us here for more details: