Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

3D scatter plot with 2D histogram at sides

Tags:

I would like to make a 3D scatterplot with with the 2D projections at each side. Something like the following:

3D scatter

The scatterplot has been created with:

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z, c='r', marker='.')

And the projections with:

 h = np.np.histogram2d(x, y)
 plt.imshow(h, cmap='cubehelix_r', interpolation='none')

And they have been brought together with inkscape. How would I do this completely with matplotlib?

like image 532
magu_ Avatar asked Jul 06 '16 17:07

magu_


People also ask

How do you make a 3 dimensional scatter plot?

Generally 3D scatter plot is created by using ax. scatter3D() the function of the matplotlib library which accepts a data sets of X, Y and Z to create the plot while the rest of the attributes of the function are the same as that of two dimensional scatter plot.

What does a 2D histogram tell you?

In image processing a 2D histogram shows the relationship of intensities at the exact position between two images. The 2D histogram is mostly used to compare 2 channels in a multi-channel images, where the x-axis represent the intensities of the first channel and the y-axis the intensities of the second channel.

Can a histogram be a scatter plot?

Scatter plots with marginal histograms are those which have plotted histograms on the top and side, representing the distribution of the points for the features along the x- and y- axes. It's a small addition but great for seeing the exact distribution of our points and more accurately identify our outliers.


1 Answers

Use plot_surface():

ax.scatter(x,y,z, marker='.', c='r')

h, yedges, zedges = np.histogram2d(y, z, bins=50)
h = h.transpose()
normalized_map = plt.cm.Blues(h/h.max())

yy, zz = np.meshgrid(yedges, zedges)
xpos = min(x)-2 # Plane of histogram
xflat = np.full_like(yy, xpos) 

p = ax.plot_surface(xflat, yy, zz, facecolors=normalized_map, rstride=1, cstride=1, shade=False)

Repeat for the other 2 histograms. Plot with histograms

A simpler method if you just want projections (but not histograms) is to add scatter plots with flattened data:

ax.scatter(x, y, z, c='r', marker='.')

xflat = np.full_like(x, min(ax.get_xlim()))
yflat = np.full_like(y, max(ax.get_ylim()))
zflat = np.full_like(z, min(ax.get_zlim()))

ax.scatter(xflat, y, z)
ax.scatter(x, yflat, z)
ax.scatter(x, y, zflat)

(Normally I'd just type x*0 + n to make a matching array filled with constant value, but np.full_like is more explicit)

Plot with projections

like image 162
Lack Avatar answered Sep 28 '22 04:09

Lack