Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python matplotlib中的三维离散热图_Python_Matplotlib_Heatmap - Fatal编程技术网

Python matplotlib中的三维离散热图

Python matplotlib中的三维离散热图,python,matplotlib,heatmap,Python,Matplotlib,Heatmap,在python中,我有一个包含三维数据的元组列表,其中每个元组的形式为:(x,y,z,data_value),也就是说,我在每个(x,y,z)坐标上都有数据值。我想制作一个3D离散热图图,其中颜色表示元组列表中数据值的值。这里,我给出了一个2D数据集的热图示例,其中我有一个(x,y,data_值)元组列表: 这将生成如下图: 如果我的数据是三维的,我如何在三维空间中绘制类似的图(即,有一个z轴)。例如,如果 # x and y and z coordinates x = np.array(ra

在python中,我有一个包含三维数据的元组列表,其中每个元组的形式为:(x,y,z,data_value),也就是说,我在每个(x,y,z)坐标上都有数据值。我想制作一个3D离散热图图,其中颜色表示元组列表中数据值的值。这里,我给出了一个2D数据集的热图示例,其中我有一个(x,y,data_值)元组列表:

这将生成如下图:

如果我的数据是三维的,我如何在三维空间中绘制类似的图(即,有一个z轴)。例如,如果

# x and y and z coordinates
x = np.array(range(10))
y = np.array(range(10,15))
z = np.array(range(15,20))
data = np.zeros((len(y),len(x), len(y)))

# Generate some random discrete data (1, 2 or 3) for each (x, y, z) triplet. 
# Am I defining i, j and k correctly here?
for i,yy in enumerate(y):
    for j, xx in enumerate(x):
        for k, zz in enumerate(z):
            data[i,j, k] = randint(1,3)
听起来我应该能够做到这一点,但是这个函数的输入中的z基本上是(x,y)坐标处的数据值,即(x,y,z=数据值),这与我的不同,即(x,y,z,数据值)

新答案: 看来我们真的很想在这里玩一个3D俄罗斯方块游戏;-)

因此,这里有一种方法可以绘制不同颜色的立方体,以填充数组
(x,y,z)
给出的空间

我发现这里真的很难看到任何东西,但这可能是一个品味的问题,现在希望也能回答这个问题


原始答复: 我似乎误解了这个问题。因此,以下内容不能回答这个问题。现在我把它留在这里,让下面的评论对其他人有用

我认为对于指定的任务来说是可以的

基本上,您可以使用3D中的点
X、Y、Z
给出的形状绘制曲面,并使用
data\u values
中的值对其进行着色,如下面的代码所示

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')

# as plot_surface needs 2D arrays as input
x = np.arange(10)
y = np.array(range(10,15))
# we make a meshgrid from the x,y data
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X**2 + Y**2))

# data_value shall be represented by color
data_value = np.random.rand(len(y), len(x))
# map the data to rgba values from a colormap
colors = cm.ScalarMappable(cmap = "viridis").to_rgba(data_value)


# plot_surface with points X,Y,Z and data_value as colors
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=colors,
                       linewidth=0, antialiased=True)

plt.show()

您想要一个3d曲面,其中绘图的颜色是x、y和z的函数?正确!使用离散色条。您可能想查看
mayavi
,它允许您在3d中绘制标量场的iso曲面。如果您不介意语言转换和许可证问题,Mathematica内置了
Image3D
,Mathematica中的
ContourPlot3D
Image3D听起来非常像我想要的。我只是希望python/matplotlib中有一个等价物。这并不是我想要的。在您的示例中,对于每个(x,y)对,z只有一个值。在我的例子中,z是一个独立的坐标,也就是说,对于每个(x,y)对,我将有z的所有可能值(而不仅仅是一个值)。例如,我遗漏了一些东西,如果您使用我在示例代码中提供的3D数据修改您的示例,这将非常有帮助。@user3076813我可能完全误解了您的问题。但在这种情况下,我认为首先需要定义映射。我确实是从RxR->R(2D->1D映射)映射的。如果你真的想要一个RxRxR->R映射,结果图应该是什么样的?一大堆半透明的立方体?定义好后,我们可以看看matplotlib是否可行。我实际上想要一个R x R x R-->Z+映射(其中Z+是非负整数集)。对于上面给出的2D示例,每个(x,y)点都有一个彩色正方形。对于3D的情况,我希望每个(x,y,z)点都有一个(半透明的,如果可能的话)彩色立方体。好吧,那么让人困惑的是,你在评论中用“正确”回答了这个问题“你想要一个3D曲面[…]”。从某种程度上说,我非常怀疑你想要的是一个信息丰富的情节。很抱歉造成混乱。我想我误解了那个问题。那么,在python中就没有这样的方法了?有什么建议可以让情节更丰富?
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm
import matplotlib.colorbar
import matplotlib.colors

def cuboid_data(center, size=(1,1,1)):
    # code taken from
    # http://stackoverflow.com/questions/30715083/python-plotting-a-wireframe-3d-cuboid?noredirect=1&lq=1
    # suppose axis direction: x: to left; y: to inside; z: to upper
    # get the (left, outside, bottom) point
    o = [a - b / 2 for a, b in zip(center, size)]
    # get the length, width, and height
    l, w, h = size
    x = [[o[0], o[0] + l, o[0] + l, o[0], o[0]],  # x coordinate of points in bottom surface
         [o[0], o[0] + l, o[0] + l, o[0], o[0]],  # x coordinate of points in upper surface
         [o[0], o[0] + l, o[0] + l, o[0], o[0]],  # x coordinate of points in outside surface
         [o[0], o[0] + l, o[0] + l, o[0], o[0]]]  # x coordinate of points in inside surface
    y = [[o[1], o[1], o[1] + w, o[1] + w, o[1]],  # y coordinate of points in bottom surface
         [o[1], o[1], o[1] + w, o[1] + w, o[1]],  # y coordinate of points in upper surface
         [o[1], o[1], o[1], o[1], o[1]],          # y coordinate of points in outside surface
         [o[1] + w, o[1] + w, o[1] + w, o[1] + w, o[1] + w]]    # y coordinate of points in inside surface
    z = [[o[2], o[2], o[2], o[2], o[2]],                        # z coordinate of points in bottom surface
         [o[2] + h, o[2] + h, o[2] + h, o[2] + h, o[2] + h],    # z coordinate of points in upper surface
         [o[2], o[2], o[2] + h, o[2] + h, o[2]],                # z coordinate of points in outside surface
         [o[2], o[2], o[2] + h, o[2] + h, o[2]]]                # z coordinate of points in inside surface
    return x, y, z

def plotCubeAt(pos=(0,0,0), c="b", alpha=0.1, ax=None):
    # Plotting N cube elements at position pos
    if ax !=None:
        X, Y, Z = cuboid_data( (pos[0],pos[1],pos[2]) )
        ax.plot_surface(X, Y, Z, color=c, rstride=1, cstride=1, alpha=0.1)

def plotMatrix(ax, x, y, z, data, cmap="jet", cax=None, alpha=0.1):
    # plot a Matrix 
    norm = matplotlib.colors.Normalize(vmin=data.min(), vmax=data.max())
    colors = lambda i,j,k : matplotlib.cm.ScalarMappable(norm=norm,cmap = cmap).to_rgba(data[i,j,k]) 
    for i, xi in enumerate(x):
            for j, yi in enumerate(y):
                for k, zi, in enumerate(z):
                    plotCubeAt(pos=(xi, yi, zi), c=colors(i,j,k), alpha=alpha,  ax=ax)



    if cax !=None:
        cbar = matplotlib.colorbar.ColorbarBase(cax, cmap=cmap,
                                norm=norm,
                                orientation='vertical')  
        cbar.set_ticks(np.unique(data))
        # set the colorbar transparent as well
        cbar.solids.set(alpha=alpha)              



if __name__ == '__main__':

    # x and y and z coordinates
    x = np.array(range(10))
    y = np.array(range(10,15))
    z = np.array(range(15,20))
    data_value = np.random.randint(1,4, size=(len(x), len(y), len(z)) )
    print data_value.shape

    fig = plt.figure(figsize=(10,4))
    ax = fig.add_axes([0.1, 0.1, 0.7, 0.8], projection='3d')
    ax_cb = fig.add_axes([0.8, 0.3, 0.05, 0.45])
    ax.set_aspect('equal')

    plotMatrix(ax, x, y, z, data_value, cmap="jet", cax = ax_cb)

    plt.savefig(__file__+".png")
    plt.show()
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')

# as plot_surface needs 2D arrays as input
x = np.arange(10)
y = np.array(range(10,15))
# we make a meshgrid from the x,y data
X, Y = np.meshgrid(x, y)
Z = np.sin(np.sqrt(X**2 + Y**2))

# data_value shall be represented by color
data_value = np.random.rand(len(y), len(x))
# map the data to rgba values from a colormap
colors = cm.ScalarMappable(cmap = "viridis").to_rgba(data_value)


# plot_surface with points X,Y,Z and data_value as colors
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=colors,
                       linewidth=0, antialiased=True)

plt.show()