Physicist
Physicist

Reputation: 3048

how to plot 2D intensity plot in matplotlib?

I have an Nx3 array which stores the values in N coordinates. The first and second column correspond to x and y coordinate respectively, and the third column represents the value at that coordinates. I want to plot a 2D intensity plot, what's the best way to do it?

If the coordinates are evenly spaced, then I can use meshgrid and then use imshow, but in my data the coordinates are not evenly spaced. Besides, the array is very large N~100000, and the values (third column) span several orders of magnitude (so I should be using a logplot?). What's the best way to plot such a graph?

Upvotes: 2

Views: 4467

Answers (1)

Ed Smith
Ed Smith

Reputation: 13206

You can use griddata to interpolate your data at all 100000 points to a uniform grid (say 100 x 100) and then plot everything with a Log scaling of the colours,

x = data[:,0]
y = data[:,1]
z = data[:,2]

# define grid.
xi = np.linspace(np.min(x),np.max(x),100)
yi = np.linspace(np.min(y),np.max(y),100)

# grid the data.
zi = griddata(x,y,z,xi,yi,interp='linear')

#pcolormesh of interpolated uniform grid with log colormap
plt.pcolormesh(xi,yi,zi,norm=matplotlib.colors.LogNorm())

plt.colormap()
plt.show()

I've not tested this but basic idea should be correct. This has the advantage that you don't need to know your original (large) dataset and can work simply with the grid data xi, yi and zi.

The alternative is to colour a scatterplot,

plt.scatter(x, y, c=z,edgecolors='none', norm=matplotlib.colors.LogNorm())

and turn off the outer edges of the points so they make up a continuous picture.

Upvotes: 2

Related Questions