Ploting 2D Histogram in 3D Axes Matplotlib/Pyplot

Viewed 35

I'm kinda new to python..

I have a dataframe on which if I call df.hist() it would result in this following pictures. Result of

I would like to plot these histogram in 3d.Does anyone has a suggestion on plotting several histogram in jupyter notebook on 3d Axes?

I've tried this code but it does not generate the bar graph as I would like it to be.. My poor result

import matplotlib.pyplot as plt


fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
nbins = 5
for c in MCC_Dataframe.columns:

    ax.hist( x=c, alpha=0.8, density=True)

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

plt.show()

My dataframe look like this Dataframe I would like to plot the distribution for each column..

Anyone has a suggestion? Thank you! Thank you in advance!

1 Answers

I think you want to plot the histograms as bars:

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
nbins = 5
for y, c in enumerate(df.columns):
    hist, bins = np.histogram(df[c])
    print(hist, bins)
    ax.bar3d(bins[1:], y-.2, 0, .8, .4, hist)

ax.set_yticks(np.arange(len(df.columns)))
ax.set_yticklabels(df.columns)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

Output:

enter image description here

Note This is just a sample, you'll have to deal with the different bins across the columns.

Related