Ask Your Question

List Density Plot?

asked 2015-03-09 18:36:55 -0500

Geremia gravatar image

updated 2015-03-09 20:07:12 -0500

How can I make a 2-D density plot of a list of values in Sage?

I'd like to make something like this: Density map (image source)

edit retag flag offensive close merge delete

3 answers

Sort by » oldest newest most voted

answered 2015-03-09 18:52:57 -0500

tmonteil gravatar image

updated 2015-03-09 18:55:20 -0500

You can plot various points on a picture by adding together point plots:

sage: point((1,2)) + point((3,4))

You can also do:

sage: points([(1,2), (3,4)])

You can also have a look at:

sage: bar_chart?

Now, if you have a list of values, you can make boxes (representing intervals), and count how many points fall within each box, and then draw the result. Please do not hesitate to ask for more details if you are locked somewhere.

edit flag offensive delete link more


I would like to color-code each point based upon a z-value.

Geremia gravatar imageGeremia ( 2015-03-09 19:17:29 -0500 )edit

I just discovered the hue() function! That's exactly what I need! (I suppose I don't want to do a density plot as much a 3-D plot on a 2-D surface, where the third dimension is the color of the point.)

Geremia gravatar imageGeremia ( 2015-03-09 19:19:33 -0500 )edit

I'm dealing with over 300,000 points, and

p = Graphics()

for n in range(len(mylist)):
    p += point((mylist[n][0], mylist[n][1]),color=hue(n/len(mylist)))

is somewhat slow…

Any tips to speed this up?


Geremia gravatar imageGeremia ( 2015-03-09 19:42:05 -0500 )edit

You might be able to use a mapcommand. Or you might want to try using Cython. See:

calc314 gravatar imagecalc314 ( 2015-03-10 07:54:39 -0500 )edit

answered 2015-03-13 20:10:57 -0500

Geremia gravatar image

updated 2015-03-13 20:15:37 -0500

They key is to use hist2d:

import matplotlib as mpl
import matplotlib.pyplot as plt

px,py = x,y

nx = ny = 100 #number of bins in x,y directions
x_bins = np.linspace(px.min(), px.max(), nx+1)
y_bins = np.linspace(py.min(), py.max(), ny+1)

plt.ylabel('Hash ($\log_{10}$)');
plt.title(r'2-D Histogram of First '+str(len(x))+' Valid Nonces and Hashes');

plt.hist2d(px, py, [x_bins,y_bins], norm=mpl.colors.LogNorm());

plt.savefig('Density Plot (log colorscale).png')

This produces a nice plot: hist2d plot

edit flag offensive delete link more

answered 2015-03-10 17:43:13 -0500

Eugene gravatar image

Consider this example borrowed from matplotlib gallery (note the change in the last row, it is needed for use within sage's notebook):

import matplotlib.pyplot as plt
from matplotlib.colors import BoundaryNorm
from matplotlib.ticker import MaxNLocator
import numpy as np

# make these smaller to increase the resolution
dx, dy = 0.01, 0.01

# generate 2 2d grids for the x & y bounds
y, x = np.mgrid[slice(1, 5 + dy, dy),
                slice(1, 5 + dx, dx)]

z = np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x)

# x and y are bounds, so z should be the value *inside* those bounds.
# Therefore, remove the last value from the z array.
z = z[:-1, :-1]
levels = MaxNLocator(nbins=15).tick_values(z.min(), z.max())

# pick the desired colormap, sensible levels, and define a normalization
# instance which takes data values and translates those into levels.
cmap = plt.get_cmap('PiYG')
norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True)

plt.subplot(2, 1, 1)
im = plt.pcolormesh(x, y, z, cmap=cmap, norm=norm)
# set the limits of the plot to the limits of the data
plt.axis([x.min(), x.max(), y.min(), y.max()])
plt.title('pcolormesh with levels')

plt.subplot(2, 1, 2)
# contours are *point* based plots, so convert our bound into point
# centers
plt.contourf(x[:-1, :-1] + dx / 2.,
             y[:-1, :-1] + dy / 2., z, levels=levels,
plt.title('contourf with levels')

edit flag offensive delete link more


Actually, I want to do this for discrete values, not functions, so a need to use histogram2d, as here.

Geremia gravatar imageGeremia ( 2015-03-13 14:33:09 -0500 )edit

Actually, hist2d is what I was looking for.

Geremia gravatar imageGeremia ( 2015-03-13 20:05:00 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2015-03-09 18:36:55 -0500

Seen: 393 times

Last updated: Mar 13 '15