【问题标题】:How to plot heatmap colors in 3D in Matplotlib如何在 Matplotlib 中绘制 3D 热图颜色
【发布时间】:2013-07-19 09:05:25
【问题描述】:

我正在使用 Matplotlib 3D 绘制数据集的 3 个维度,如下所示:

但现在我还想将第 4 维(0 到 20 之间的标量值)可视化为热图。所以基本上,我希望每个点都根据这个第四维的值来取它的颜色。

Matplotlib 中是否存在这样的东西?如何将 [0-20] 之间的一堆数字转换为热图颜色?

我从这里获取代码:http://matplotlib.org/mpl_examples/mplot3d/scatter3d_demo.py

【问题讨论】:

    标签: python matplotlib plot heatmap


    【解决方案1】:

    是的,是这样的:

    更新这里是带有颜色条的版本。

    import numpy as np
    from pylab import *
    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib.pyplot as plt
    
    def randrange(n, vmin, vmax):
        return (vmax-vmin)*np.random.rand(n) + vmin
    
    fig = plt.figure(figsize=(8,6))
    
    ax = fig.add_subplot(111,projection='3d')
    n = 100
    
    xs = randrange(n, 23, 32)
    ys = randrange(n, 0, 100)
    zs = randrange(n, 0, 100)
    
    colmap = cm.ScalarMappable(cmap=cm.hsv)
    colmap.set_array(zs)
    
    yg = ax.scatter(xs, ys, zs, c=cm.hsv(zs/max(zs)), marker='o')
    cb = fig.colorbar(colmap)
    
    ax.set_xlabel('X Label')
    ax.set_ylabel('Y Label')
    ax.set_zlabel('Z Label')
    
    
    plt.show()
    

    看起来像:

    更新这是一个通过某些 4 维属性为数据点着色的明确示例。

    import numpy as np
    from pylab import *
    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib.pyplot as plt
    
    def randrange(n, vmin, vmax):
        return (vmax-vmin)*np.random.rand(n) + vmin
    
    fig = plt.figure(figsize=(8,6))
    
    ax = fig.add_subplot(111,projection='3d')
    n = 100
    
    xs = randrange(n, 0, 100)
    ys = randrange(n, 0, 100)
    zs = randrange(n, 0, 100)
    the_fourth_dimension = randrange(n,0,100)
    
    colors = cm.hsv(the_fourth_dimension/max(the_fourth_dimension))
    
    colmap = cm.ScalarMappable(cmap=cm.hsv)
    colmap.set_array(the_fourth_dimension)
    
    yg = ax.scatter(xs, ys, zs, c=colors, marker='o')
    cb = fig.colorbar(colmap)
    
    ax.set_xlabel('X Label')
    ax.set_ylabel('Y Label')
    ax.set_zlabel('Z Label')
    
    
    plt.show()
    

    【讨论】:

      【解决方案2】:

      我大多同意@seth,但我想更改 1 行,以便颜色值不标准化:

      yg = ax.scatter(xs, ys, zs, c=colmap.to_rgba(the_fourth_dimension)[:,0,0:3], marker='o')
      

      【讨论】:

        猜你喜欢
        • 2015-10-02
        • 1970-01-01
        • 1970-01-01
        • 2019-03-23
        • 1970-01-01
        • 1970-01-01
        • 2019-07-14
        • 1970-01-01
        • 2015-02-19
        相关资源
        最近更新 更多