0

Using matplotlib, I am attempting to display the histograms of 2 sets of data simultaneously on the side walls of a 3D plot, using this Matlab code and plot from wikipedia as my guide: https://commons.wikimedia.org/wiki/File:MultivariateNormal.png I am able to plot my raw data on the base plane and have created and plotted my Gaussian fits on the side walls using the 'zdir' kwarg.

This example is able to leverage the 'zdir' kwarg to force where the curves are plotted, Matplotlib 2d Plot on Faces of 3d Plot but the matplotlib documentation confirms my AttributeErrors: Unknown property zdir; hist and hist2d don't support this argument.

This example seems to be plotting bars manually on the figure plotting 3d histogram/barplot in python matplotlib as a way around the problem.

I've tried both .hist and .hist2d with and without zdir=''.

# data is a 2D np.array defined elsewhere

# define plot limits
X = np.linspace(0, np.amax(data), 100)
Y = np.linspace(0, np.amax(data), 100)

# initialize data into x and y sets
x_data = data[:, 0]
y_data = data[:, 1]

# fit a gaussian to both sets
x_mean, x_std = norm.fit(x_data)
x_gauss = norm.pdf(X, x_mean, x_std)
y_mean, y_std = norm.fit(y_data)
y_gauss = norm.pdf(Y, y_mean, y_std)

# initialize plot
figure = plt.figure()
ax = figure.add_subplot(111, projection='3d')

# label axes
ax.set_xlabel('Delta X (um)')
ax.set_ylabel('Delta Y (um)')
ax.set_zlabel('P (X,Y)')

# plot data on base plane
ax.scatter3D(x_data, y_data, zdir='z', zs=0.0, c='k', marker='.')

# plot histograms on walls
ax.hist((x_data, x_gauss), bins=30) #these 2 lines
ax.hist((y_data, y_gauss), bins=30) #are where I'm looking for help

# plot gaussians on walls
ax.plot3D(X, x_gauss, zdir='y', zs=np.amax(data), c='b')
ax.plot3D(Y, y_gauss, zdir='x', zs=np.amax(data), c='g')

# show plot
plt.show()

Is there a direct match in matplotlib for the method Matlab that draws histograms on a specific plane of a 3D plot? Thank you for your help! I am very new to plotting and welcome any other idiomatic or depreciated changes you can see. I always like to see how other coders think.

E Rind
  • 53
  • 5
  • Please provide data so that we can run your code – Sheldore Apr 29 '19 at 22:08
  • 1
    Try the way [this](https://stackoverflow.com/questions/48038422/3d-plot-of-list-of-hist-bin-edges-where-histogram-bar-chart-or-lines-are-in-t/48042170) answer uses `ax.bar` – Sheldore Apr 29 '19 at 22:10
  • @Sheldore Thank you for the tip! `ax.bar` is what I needed. I was unable to get the `density=True` flag to work properly (it wouldn't normalize the histogram), so I got around it by dividing each histogram's value by the total sum. – E Rind May 01 '19 at 20:52

0 Answers0