Python 2.7 用python绘制三维条形图

Python 2.7 用python绘制三维条形图,python-2.7,matplotlib,3d,histogram,Python 2.7,Matplotlib,3d,Histogram,我有一些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=-

我有一些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()
将numpy导入为np
导入numpy.random
将matplotlib.pyplot作为plt导入
#生成一些测试数据
x=np.random.randn(500)
y=np.random.randn(500)
XY=np.堆栈((x,y),轴=-1)
def选择(XY,限制XY=[[-2,+2],-2,+2]]):
XY_选择=[]
对于XY中的elt:
如果elt[0]>limitXY[0][0]和elt[0]limitXY[1][0]和elt[1]
并给出正确的结果:

现在,我想把它转换成一个3D直方图。不幸的是,我没有成功地用
bar3d
正确地绘制它,因为默认情况下,横坐标的长度是x和y


我非常确信,有一种非常简单的方法可以用imshow在3D中绘制。就像一个未知的选项…

您可以使用以下简单的方法生成相同的结果:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-2, 2, 7)
y = np.linspace(-2, 2, 7)

xx, yy = np.meshgrid(x, y)

z = xx*0+yy*0+ np.random.random(size=[7,7])

plt.imshow(z, interpolation='nearest', cmap=plt.cm.viridis, extent=[-2,2,2,2])
plt.show()

from mpl_toolkits.mplot3d import Axes3D
ax = Axes3D(plt.figure())

ax.plot_surface(xx, yy, z, cmap=plt.cm.viridis, cstride=1, rstride=1)
plt.show()
结果如下:


我终于成功了。我几乎可以肯定有更好的方法,但至少它是有效的:

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


xAmplitudes = np.array(XY_select)[:,0]#your data here
yAmplitudes = np.array(XY_select)[:,1]#your other data here


fig = plt.figure() #create a canvas, tell matplotlib it's 3d
ax = fig.add_subplot(111, projection='3d')


hist, xedges, yedges = np.histogram2d(x, y, bins=(7,7), range = [[-2,+2],[-2,+2]]) # you can change your bins, and the range on which to take data
# hist is a 7X7 matrix, with the populations for each of the subspace parts.
xpos, ypos = np.meshgrid(xedges[:-1]+xedges[1:], yedges[:-1]+yedges[1:]) -(xedges[1]-xedges[0])


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

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

cmap = cm.get_cmap('jet') # Get desired colormap - you can change this!
max_height = np.max(dz)   # get range of colorbars so we can normalize
min_height = np.min(dz)
# scale each z to [0,1], and get their rgb values
rgba = [cmap((k-min_height)/max_height) for k in dz] 

ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color=rgba, zsort='average')
plt.title("X vs. Y Amplitudes for ____ Data")
plt.xlabel("My X data source")
plt.ylabel("My Y data source")
plt.savefig("Your_title_goes_here")
plt.show()
将numpy导入为np
导入numpy.random
将matplotlib.pyplot作为plt导入
#生成一些测试数据
x=np.random.randn(500)
y=np.random.randn(500)
XY=np.堆栈((x,y),轴=-1)
def选择(XY,限制XY=[[-2,+2],-2,+2]]):
XY_选择=[]
对于XY中的elt:
如果elt[0]>limitXY[0][0]和elt[0]limitXY[1][0]和elt[1]
我使用它,但我修改了它,因为它引入了偏移。结果是:


除非您使用,否则matplotlib不具有三维打印功能。您是否查看了官方版本?这里有一个简单的例子,我不明白这个x,y数据是用来做什么的。我猜你有一个等间距的网格,网格上的每个正方形都应该有一些随机值。对norok2:是的,我已经看过了,但是例子(像你的)不符合我想要的…链接的例子似乎是直接适用的。如果这不是“我想要的”,你会想知道它离你有多远,否则你就不知道它到底出了什么问题。(如果你想通知某人,请使用@username,否则他们将看不到)对不起,但是如果你使用相同的x、y和heatmap.T数据会更好。你换了例外汉克!但是很抱歉,如果您使用相同的x、y和heatmap.T数据会更好。你稍微改变一下这个例子。另外,我们可以用一些平面来代替倾斜表面吗?还有一个我以前没有注意到的问题:2D彩色直方图有7x7个定界,另一个有6个定界!这使得观察数据分析变得很危险。。。最好是一个7x7的三维平面正方形水平仪@kanayamalakar@AgapeGal“瞧,你能给我看一张你想要的3D情节的照片吗?也许只是一张有代表性的照片?因为我似乎不明白你所说的三维平面正方形的意思。你可以好好看看这个链接!我在应用配色方案时遇到了麻烦。谢谢你sharing@kanayamalakar但是颜色的重新规范化有一个问题。。。如果在这行'hist,xedges,yedges=np.histogram2d(x,y,bins=(7,7),range=[-2,+2],-2,+2]])#您可以更改您的bins和获取数据的范围'您可以用另一个7X7矩阵替换hist,颜色不再具有良好的比例。我不明白为什么!应该有更好的方法来获取此颜色渐变。也许您可以添加“import matplotlib.cm as cm”