3D гистограмма из матрицы z-значений - PullRequest
0 голосов
/ 03 декабря 2018

Я искал на форуме, но нашел только похожие, но разные ответы и вопросы.У меня проблема с построением трехмерной гистограммы, начиная с матрицы значений z.

Это матрица, которую я получил из предыдущих операций:

[[  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')

Но у меня проблемы с пониманием, как положить в хзначения у.Кто-нибудь может мне помочь?

ОБНОВЛЕНИЕ:

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')

1 Ответ

0 голосов
/ 03 декабря 2018

Не уверен, что ты хочешь получить.Попробуйте следующий код в качестве отправной точки:

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

Результат:

enter image description here

Код является модификацией этот пример с сайта matplotlib.org.

...