plotting 3d histogram/barplot in python matplotlib

笑着哭i 提交于 2019-12-07 04:23:11

问题


I have an Nx3 matrix in scipy/numpy and I'd like to make a 3 dimensional bar graph out of it, where the X and Y axes are determined by the values of first and second columns of the matrix, the height of each bar is the third column in the matrix, and the number of bars is determined by N.

In other words, if "data" is the matrix then:

data[:, 0] # values of X-axis
data[:, 1] # values of Y-axis
data[:, 2] # values of each Z-axis bar

and there should be one bar for each len(data)

How can I do this in Matplotlib?

Secondly, as a variant of this, how can I do the same thing, but this time histogram the bars into N bins in each X, Y, Z dimension? I.e. instead of a bar for each point, just histogram the data into those bins in every dimension, and plot a bar for each bin.

thanks very much for your help.


回答1:


Here is one example of a 3D bar plot. Here is another.

Numpy has a function called histogram2d to do the rectangular binning you want.




回答2:


I had a little of a hard time shaping the 'heights' of my data properly from the examples, but finally got it to work with the following code. Here, Z is a 3 dimensional array with all of my data, and x and rval are basically the 2-d indices corresponding to the datapoints.

xs = np.arange(biopsy_num)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for y in (np.arange(r_sweep)):
    z = Z[:,y]
    ax.bar(xs, z, zs=y, zdir='y', alpha=0.8)

ax.set_xlabel('biopsies')
ax.set_ylabel('radius of biopsy')
ax.set_zlabel('Shannon Index')

plt.show()


来源:https://stackoverflow.com/questions/5488485/plotting-3d-histogram-barplot-in-python-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!