How to color `matplotlib` scatterplot using a continuous value [`seaborn` color palettes?]

后端 未结 2 1542
有刺的猬
有刺的猬 2021-02-19 02:05

I have a scatterplot and I want to color it based on another value (naively assigned to np.random.random() in this case).

Is there a way to use

相关标签:
2条回答
  • 2021-02-19 02:22
    import numpy as np
    import seaborn as sns
    import matplotlib.pyplot as plt
    
    x, y, z = np.random.rand(3, 100)
    cmap = sns.cubehelix_palette(as_cmap=True)
    
    f, ax = plt.subplots()
    points = ax.scatter(x, y, c=z, s=50, cmap=cmap)
    f.colorbar(points)
    

    0 讨论(0)
  • 2021-02-19 02:38
    from matplotlib.cm import ScalarMappable
    from matplotlib.colors import Normalize
    
    
    cmap = {obsv_id:np.random.random() for obsv_id in DF_PCA.index}
    sm = ScalarMappable(norm=Normalize(vmin=min(list(cmap.values())), vmax=max(list(cmap.values()))), cmap=sns.cubehelix_palette(as_cmap=True))
    
    # Plot
    fig, ax = plt.subplots()
    ax.scatter(x=DF_PCA["PC1"], y=DF_PCA["PC2"], color=[sm.to_rgba(cmap[obsv_id]) for obsv_id in DF_PCA.index])
    ax.set_title("With Coloring")
    

    0 讨论(0)
提交回复
热议问题