【问题标题】:Scatterplot with 'continuous bivariate' color palette in python [duplicate]python中带有“连续双变量”调色板的散点图[重复]
【发布时间】:2018-04-17 06:40:23
【问题描述】:

我已经能够使用以下脚本使用表示连续变量的调色板绘制散点图:

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

x, y, z = np.random.rand(3, 50)
cmap = sns.cubehelix_palette(start=5, light=1, as_cmap=True)

fig, ax = plt.subplots()
points = ax.scatter(x, y, c=z, s=20, cmap=cmap)
fig.colorbar(points)

Output univariate palette

但是,我需要使用“双变量调色板”创建地图。我最终将创建这样的地图,但目前我正在寻找以这种方式绘制散点图。 (来源:https://www.linkedin.com/feed/update/urn:li:activity:6378928737907466240Bivariate palette 我希望绘制散点图,以便颜色代表地图中的变量 RMSE 和 R。

【问题讨论】:

    标签: python matplotlib plot gradient seaborn


    【解决方案1】:

    您可以通过取两个不同颜色图的平均值来创建二元颜色图。使用不同的配色方案很好,您可以尝试here显示的范围。

    import numpy as np
    import matplotlib.pyplot as plt
    
    def colorFromBivariateData(Z1,Z2,cmap1 = plt.cm.YlOrRd, cmap2 = plt.cm.PuBuGn):
        # Rescale values to fit into colormap range (0->255)
        Z1_plot = np.array(255*(Z1-Z1.min())/(Z1.max()-Z1.min()), dtype=np.int)
        Z2_plot = np.array(255*(Z2-Z2.min())/(Z2.max()-Z2.min()), dtype=np.int)
    
        Z1_color = cmap1(Z1_plot)
        Z2_color = cmap2(Z2_plot)
    
        # Color for each point
        Z_color = np.sum([Z1_color, Z2_color], axis=0)/2.0
    
        return Z_color
    
    z1 = np.random.random((50,100))
    z2 = np.random.random((50,100))
    Z_color = colorFromBivariateData(z1,z2)
    
    xx, yy = np.mgrid[0:100,0:100]
    C_map = colorFromBivariateData(xx,yy)
    
    fig = plt.figure(figsize=(10,5))
    
    ax1 = fig.add_subplot(1,2,1)
    ax1.imshow(Z_color)
    ax1.set_title('Data')
    
    ax2 = fig.add_subplot(1,2,2)
    ax2.imshow(C_map)
    ax2.set_title('Bivariate Color Map')
    ax2.set_xlabel('Variable 1')
    ax2.set_ylabel('Variable 2')
    
    fig.tight_layout()
    fig.show()
    

    输出是:

    有很多关于在地图上绘图以及将较小的轴嵌入到较大的轴上的信息,因此扩展这个想法以创建您在问题中链接到的图像应该不难。

    如果这回答了你的问题,请告诉我!

    【讨论】:

    • 我已编辑脚本以处理散点图数据并反转颜色图中的 y 轴。这显示了这些如何以更好的方式工作。 gist.github.com/wolfiex/…
    猜你喜欢
    • 2018-04-04
    • 2017-11-22
    • 2023-03-08
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    • 2018-11-04
    • 2019-01-24
    • 2023-03-18
    相关资源
    最近更新 更多