简体   繁体   English

循环后将颜色条添加到散点图

[英]Adding colorbar to scatterplot after loop

I am trying to add a continuous colorbar to a seaborn scatterplot (similar to the answers here and in here ).我正在尝试向 seaborn 散点图添加一个连续的颜色条(类似于此处此处的答案)。 For my purposes, I am building the scatterplot with a loop, and then trying to add the continuous colorbar, but I dont know what object to include as argument of fig.colorbar() .出于我的目的,我正在使用循环构建散点图,然后尝试添加连续的颜色条,但我不知道 object 包含什么作为fig.colorbar()的参数。 How would you do this?你会怎么做?

import pandas as pd
import seaborn as sb
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(42)

df=pd.DataFrame(np.random.rand(2, 100), index=['S1','S2']).T
tars=np.random.choice([0,0.3,0.5,0.8,1], 100)
df=pd.concat([df,pd.Series(tars, name='group')],1)
colors = matplotlib.cm.viridis(np.linspace(0,1,len(pd.unique(tars))))

fig = plt.figure(figsize = (10,8), dpi=300)
ax = fig.add_subplot(1,1,1) 
targets=pd.unique(tars)
for target, color in zip(targets,colors):
    ...
    g=ax.scatter(
        df.loc[df.group==target, 'S1'], 
        df.loc[df.group==target, 'S2'],
        color = [color]
    )
fig.colorbar(g)
plt.show()

在此处输入图像描述

If I add ax.legend(targets) instead of fig.colorbar(g) , the legend displays correctly but is categorical.如果我添加ax.legend(targets)而不是fig.colorbar(g) ,则图例会正确显示但是分类的。

df=pd.DataFrame(np.random.rand(2, 100), index=['S1','S2']).T
tars=np.random.choice([0,0.3,0.5,0.8,1], 100)
df=pd.concat([df,pd.Series(tars, name='group')],1)

cmap=matplotlib.cm.gnuplot2
colors = cmap(np.linspace(0,1,len(pd.unique(tars))))

fig = plt.figure(figsize = (10,8), dpi=300)
ax = fig.add_subplot(1,1,1) 
targets=pd.unique(tars)
for target, color in zip(targets,colors):
    ...
    g=ax.scatter(
        df.loc[df.group==target, 'S1'], 
        df.loc[df.group==target, 'S2'],
        color = [color]
    )
ax.legend(targets)
plt.show()

在此处输入图像描述

I am not entirely sure to understand what you are trying to achieve with your for-loop.我不完全确定你想用你的 for 循环来实现什么。

Is this the output that you are looking for?这是您要找的 output 吗?

fig = plt.figure()
ax = fig.add_subplot(1,1,1) 
g = ax.scatter(df['S1'],df['S2'],c=df['group'],cmap='viridis')
cbar = fig.colorbar(g)
plt.show()

在此处输入图像描述

Thanks to this answer I could edit my code to show the continuous colorbar.感谢这个答案,我可以编辑我的代码以显示连续的颜色条。

df=pd.DataFrame(np.random.rand(2, 100), index=['S1','S2']).T
tars=np.random.choice([0,0.3,0.5,0.8,1], 100)
df=pd.concat([df,pd.Series(tars, name='group')],1)

cmap=matplotlib.cm.viridis
colors = cmap(np.linspace(0,1,len(pd.unique(tars))))

fig = plt.figure(figsize = (10,8), dpi=300)
ax = fig.add_subplot(1,1,1) 
targets=pd.unique(tars)
for target, color in zip(targets,colors):
    g=ax.scatter(
        df.loc[df.group==target, 'S1'], 
        df.loc[df.group==target, 'S2'],
        color=[color]
    )

norm = plt.Normalize(np.min(tars), np.max(tars))
sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
sm.set_array([])
ax.figure.colorbar(sm)
plt.show()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM