【发布时间】:2019-02-22 09:57:13
【问题描述】:
我有一些 x 和 y 数据,我想用它们生成一个带有颜色渐变(bwr 或其他)的 3D 直方图。
我编写了一个脚本,它绘制了 x 和 y 脓肿的有趣值,介于 -2 和 2 之间:
import numpy as np
import numpy.random
import matplotlib.pyplot as plt
# To generate some test data
x = np.random.randn(500)
y = np.random.randn(500)
XY = np.stack((x,y),axis=-1)
def selection(XY, limitXY=[[-2,+2],[-2,+2]]):
XY_select = []
for elt in XY:
if elt[0] > limitXY[0][0] and elt[0] < limitXY[0][1] and elt[1] > limitXY[1][0] and elt[1] < limitXY[1][1]:
XY_select.append(elt)
return np.array(XY_select)
XY_select = selection(XY, limitXY=[[-2,+2],[-2,+2]])
heatmap, xedges, yedges = np.histogram2d(XY_select[:,0], XY_select[:,1], bins = 7, range = [[-2,2],[-2,2]])
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
plt.figure("Histogram")
#plt.clf()
plt.imshow(heatmap.T, extent=extent, origin='lower')
plt.show()
并给出正确的结果:
现在,我想将其转换为 3D 直方图。不幸的是,我没有成功地用bar3d 正确绘制它,因为默认情况下它采用横坐标的 x 和 y 的长度。
我很确定有一种非常简单的方法可以使用 imshow 以 3D 形式进行绘制。就像一个未知的选项...
【问题讨论】:
标签: python-2.7 matplotlib 3d histogram