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

后端 未结 2 1554
有刺的猬
有刺的猬 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: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")
    

提交回复
热议问题