开发者

plotting 3d histogram/barplot in python matplotlib

开发者 https://www.devze.com 2023-02-21 00:16 出处:网络
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

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 thi开发者_StackOverflow中文版ng, 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.


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.


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()
0

精彩评论

暂无评论...
验证码 换一张
取 消