简体   繁体   中英

How to draw a 3D bar plot showing in Z with a 2D array in python

I have a 2D array as 9*9

[[0.00000000e+00 0.00000000e+00 0.00000000e+00 5.50084172e-05
  5.46158438e-04 1.84137926e-03 0.00000000e+00 0.00000000e+00
  0.00000000e+00]
 [0.00000000e+00 0.00000000e+00 5.17114283e-03 7.64807584e-03
  8.65555579e-03 7.91309324e-03 5.13831183e-03 0.00000000e+00
  0.00000000e+00]
 [0.00000000e+00 2.15065134e-03 8.34556257e-03 1.05223953e-02
  1.18791211e-02 1.04581330e-02 7.91264590e-03 1.79794357e-04
  0.00000000e+00]
 [6.92726216e-04 1.26290451e-03 8.59721682e-03 1.33073631e-02
  1.47823709e-02 1.32878077e-02 8.59453513e-03 1.40982703e-03
  7.73497493e-04]
 [1.46863741e-03 2.21508449e-03 1.02876624e-02 1.50076521e-02
  1.69088403e-02 1.50112715e-02 1.03071764e-02 4.26901251e-03
  1.44401896e-03]
 [1.64847355e-03 6.21381192e-03 1.15551587e-02 1.62702578e-02
  1.75898006e-02 1.60109886e-02 1.11483390e-02 4.56305451e-03
  1.09996723e-03]
 [0.00000000e+00 4.95475788e-03 1.33011021e-02 1.53617997e-02
  1.66588986e-02 1.46342565e-02 1.22563977e-02 4.04366789e-03
  0.00000000e+00]
 [0.00000000e+00 0.00000000e+00 9.20872794e-03 1.36888024e-02
  1.45864141e-02 1.23971922e-02 7.98613338e-03 0.00000000e+00
  0.00000000e+00]
 [0.00000000e+00 0.00000000e+00 0.00000000e+00 4.52224197e-03
  5.47370708e-03 1.13043538e-03 0.00000000e+00 0.00000000e+00
  0.00000000e+00]]

I want to plot a 3D bar plot in python using this array as X and Y in 9 rows and 9 column. The z height will be according to the value.

I am trying the below code

import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x, y = data
hist, xedges, yedges = np.histogram2d(x,y, bins=9, range=[[0, 9], [0, 9]])


# Construct arrays for the anchor positions of the 16 bars.
xpos, ypos = np.meshgrid(xedges[:-1] + 0.25, yedges[:-1] + 0.25, indexing="ij")
xpos = xpos.ravel()
ypos = ypos.ravel()
zpos = 0

# Construct arrays with the dimensions for the 16 bars.
dx = dy = 0.5 * np.ones_like(zpos)
dz = hist.ravel()

ax.bar3d(xpos, ypos, zpos, dx, dy, dz, zsort='average')

plt.show()

my array is in "data". the above code showing me error as ValueError: too many values to unpack (expected 2)

Any assistance is highly appreciated. Thanks.

Try this which I adopted form the matplob3.1 documentation:

import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

fig = plt.figure()
ax = Axes3D(fig)

x, y = np.meshgrid(np.arange(0,len(data[0]),1) + 0.25, 
    np.arange(0,len(data[:,0]),1) + + 0.25)

x,y = xpos.flatten(),ypos.flatten()
bottom = np.zeros(len(data[0]) * len(data[:,0]))


width = .5 * np.ones_like(zpos)
depth = .5 * np.ones_like(zpos)

ax.bar3d(x,y,bottom,width,depth,data.flatten())
plt.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM