我有两个数据列表:
x=[2006, 2007, 2008, 2009, 2010]
y=[700, 560, 450, 500, 570]
我需要建立一个规则的直方图(这并不困难)。但是我还需要一个三维图,如图所示。我尝试了各种选项(例如bar3d),但事实并非如此。请帮助。
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()