【问题标题】:colormap for 3d bar plot in matplotlib applied to every barmatplotlib 中 3d 条形图的颜色图应用于每个条形图
【发布时间】:2018-08-01 10:17:28
【问题描述】:

有谁知道如何在 matplotlib 中轻松实现 3d 条形图的颜色映射?

this 为例,如何根据颜色图更改每个条形?例如,短条应该主要是蓝色的,而较高的条则将它们的颜色从蓝色渐变到红色......

【问题讨论】:

    标签: matplotlib


    【解决方案1】:

    在物理科学中,想要一个所谓的 LEGO 情节是很常见的,我认为这就是原始用户想要的。 Kevin G 的回答很好,让我得到了最终结果。这是一个更高级的直方图,用于 x-y 散点数据,按高度着色:

    xAmplitudes = np.random.exponential(10,10000) #your data here
    yAmplitudes = np.random.normal(50,10,10000) #your other data here - must be same array length
    
    x = np.array(xAmplitudes)   #turn x,y data into numpy arrays
    y = np.array(yAmplitudes)   #useful for regular matplotlib arrays
    
    fig = plt.figure()          #create a canvas, tell matplotlib it's 3d
    ax = fig.add_subplot(111, projection='3d')
    
    #make histogram stuff - set bins - I choose 20x20 because I have a lot of data
    hist, xedges, yedges = np.histogram2d(x, y, bins=(20,20))
    xpos, ypos = np.meshgrid(xedges[:-1]+xedges[1:], yedges[:-1]+yedges[1:])
    
    xpos = xpos.flatten()/2.
    ypos = ypos.flatten()/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()
    

    注意:结果会因您选择的 bin 数量和使用的数据量而异。此代码需要您插入一些数据或生成随机线性数组。结果图如下,有两种不同的视角:

    【讨论】:

    • 太棒了!这正是我想要的。
    • 很好的答案!确保从 matplotlib 导入颜色图:import matplotlib.cm as cm
    【解决方案2】:

    所以也许不是你正在寻找的东西(也许对你来说是一个很好的起点),但使用

    Getting individual colors from a color map in matplotlib

    可以为条形赋予不同的纯色:

    from mpl_toolkits.mplot3d import Axes3D
    import matplotlib.pyplot as plt
    import matplotlib.cm as cm           # import colormap stuff!
    import numpy as np
    
    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    x, y = np.random.rand(2, 100) * 4
    hist, xedges, yedges = np.histogram2d(x, y, bins=4, range=[[0, 4], [0, 4]])
    
    # Construct arrays for the anchor positions of the 16 bars.
    # Note: np.meshgrid gives arrays in (ny, nx) so we use 'F' to flatten xpos,
    # ypos in column-major order. For numpy >= 1.7, we could instead call meshgrid
    # with indexing='ij'.
    xpos, ypos = np.meshgrid(xedges[:-1] + 0.25, yedges[:-1] + 0.25)
    xpos = xpos.flatten('F')
    ypos = ypos.flatten('F')
    zpos = np.zeros_like(xpos)
    
    # Construct arrays with the dimensions for the 16 bars.
    dx = 0.5 * np.ones_like(zpos)
    dy = dx.copy()
    dz = hist.flatten()
    
    cmap = cm.get_cmap('jet') # Get desired colormap
    max_height = np.max(dz)   # get range of colorbars
    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.show()
    

    就我个人而言,我觉得这很丑陋!但是使用顺序颜色图可能看起来不会太糟糕 - https://matplotlib.org/examples/color/colormaps_reference.html

    【讨论】:

    • 缩放到 [0,1] 应该是 (k-min_height)/(max_height-min_height)
    猜你喜欢
    • 2016-11-15
    • 1970-01-01
    • 2020-10-07
    • 1970-01-01
    • 2023-04-09
    • 2019-06-11
    • 1970-01-01
    • 1970-01-01
    • 2020-09-22
    相关资源
    最近更新 更多