本文介绍了如何从二维数据中获得三维直方图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个数据列表:
x=[2006, 2007, 2008, 2009, 2010]
y=[700, 560, 450, 500, 570]
我需要建立一个规则的直方图(这并不困难).但我还需要一个三维图,如图所示.我尝试了各种选项(例如bar3d),但事实并非如此.请帮助.
I needed to build a regular histogram (it was not difficult). But I also need a three-dimensional diagram, as in the figure . I tried various options (for example, bar3d), but it turns out not that. Help, please.
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x= [float(i) for i in input('Input x ').split()]
y= [float(i) for i in input('Input y ').split()]
hist, xedges, yedges = np.histogram2d(x, y, bins=(4,4))
xpos, ypos = np.meshgrid(xedges[:-1]+xedges[1:], yedges[:-1]+yedges[1:])
xpos = xpos.flatten()/2.
ypos = ypos.flatten()/2.
zpos = np.ones_like (xpos)
dx = xedges [1] - xedges [0]
dy = yedges [1] - yedges [0]
dz = hist.flatten()
ax.bar3d(xpos, ypos,zpos , dx, 1, dz, color='b')
plt.xlabel ("X")
plt.ylabel ("Y")
plt.show()
推荐答案
将此行添加到代码的第一行:
add this line to the first line of your code:
from mpl_toolkits.mplot3d import Axes3D
已更新希望这会帮助你.
UpdatedHope this will help you.
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x=[2006, 2007, 2008, 2009, 2010]
z=[700, 560, 450, 500, 570]
ax.bar3d(x, 0, 0, 0.5, 0.5, z)
plt.show()
这篇关于如何从二维数据中获得三维直方图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!