Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 基于z值矩阵的三维直方图_Python_Matplotlib_3d_Histogram - Fatal编程技术网

Python 基于z值矩阵的三维直方图

Python 基于z值矩阵的三维直方图,python,matplotlib,3d,histogram,Python,Matplotlib,3d,Histogram,我在论坛上搜索过,但只找到了相似但不同的答案和问题。我在从z值矩阵开始绘制3D直方图时遇到问题 这是我从以前的操作中得到的矩阵: [[ 84. 80.76923077 68.05555556 56.57894737 60. 44.7761194 55.2238806 39.0625 27.41935484 29.8245614 ] [ 82.44274809 67.70833333 63.75 44

我在论坛上搜索过,但只找到了相似但不同的答案和问题。我在从z值矩阵开始绘制3D直方图时遇到问题

这是我从以前的操作中得到的矩阵:

[[  84.           80.76923077   68.05555556   56.57894737   60.
    44.7761194    55.2238806    39.0625       27.41935484   29.8245614 ]
 [  82.44274809   67.70833333   63.75         44.44444444   47.76119403
    33.33333333   22.78481013   19.23076923    9.21052632    2.63157895]
 [  53.33333333   61.76470588   48.64864865   34.61538462    0.
    16.66666667   0.            0.            0.            0.        ]
 [  48.           25.            0.            0.            0.         
    0.            0.             0.            0.            0. ]]
这些都是z值。x和y值只是它们在矩阵上的位置。 我刚刚查看了matplotlib页面,但所有示例都是从x,y值开始的。我也在论坛上看过,但这个问题略有不同

我试着做一些类似的事情:

hist, xedges, yedges = np.histogram2d(x, y, bins=(20,20))
xpos, ypos = np.meshgrid(xedges[:-1]+xedges[1:], yedges[:-1]+yedges[1:])
dx = xedges [1] - xedges [0]
dy = yedges [1] - yedges [0]
dz = hist.flatten()

ax.bar3d(xpos, ypos, zpos, dx, dy, dz, zsort='average')
但我在理解如何输入x,y值方面遇到了麻烦。 有人能帮我吗

更新:

len_x, len_y = matrix.shape
x = np.linspace(0,len_x-1,len_x)
y = np.linspace(0,len_y-1,len_y)


# 3D PLOT:

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')    


hist, xedges, yedges = np.histogram2d(x,y)
xpos, ypos = np.meshgrid(xedges[:-1]+xedges[1:], yedges[:-1]+yedges[1:])

xpos = xpos.flatten()/2.
ypos = ypos.flatten()/2.
zpos = np.zeros_like(xpos)

dx = xedges [1] - xedges [0]
dy = yedges [1] - yedges [0]
dz = hist.flatten()

max_height = np.max(dz)
min_height = np.min(dz) 

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

不知道你想要什么。尝试以下代码作为起点:

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

matrix = np.array([
    [84., 80.76923077, 68.05555556, 56.57894737, 60.,
     44.7761194, 55.2238806, 39.0625, 27.41935484, 29.8245614],
    [82.44274809, 67.70833333, 63.75, 44.44444444, 47.76119403,
     33.33333333, 22.78481013, 19.23076923, 9.21052632, 2.63157895],
    [53.33333333, 61.76470588, 48.64864865, 34.61538462, 0.,
     16.66666667, 0., 0., 0., 0.],
    [48., 25., 0., 0., 0., 0., 0., 0., 0., 0. ]])

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

xpos = [range(matrix.shape[0])]
ypos = [range(matrix.shape[1])]
xpos, ypos = np.meshgrid(xpos, ypos)
xpos = xpos.flatten('F')
ypos = ypos.flatten('F')
zpos = np.zeros_like(xpos)

dx = 0.5 * np.ones_like(zpos)
dy = dx.copy()
dz = matrix.flatten()

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

plt.show()
结果是:


该代码是对matplotlib.org的修改。

显示的代码与数据的关系如何?代码会生成一个20乘20的矩阵,你得到的是一个4乘10的矩阵。到底是什么问题?也许你会确切地展示你所尝试过的,明确地写x=…,y=。。。在你的代码中,你可以理解你面临的问题,问题是如何从第一个矩阵中得到x和y。我展示的代码是一个不起作用的试用版。。。我不明白如何从初始矩阵中选择x和y值。在您的情况下,x将是[0,1,2,…9],y将是[0,1,2,3]。然后做x,y=meshgridx,y来得到x和y矩阵。谢谢,我已经更新了代码,但仍然不起作用…嗯,你想绘制直方图吗?或者你想在问题的顶部绘制你拥有的数组?