带条件的散点图

数据挖掘 熊猫 matplotlib 海运
2022-02-19 21:55:28

假设我有一个数据框

name = ['A', 'B', 'C'] 
score = [2,4,6] 

我想创建一个具有以下条件的散点图,如果分数大于 3,则将气泡着色为绿色,否则将其着色为红色。我还想用其各自的名称标记气泡。

我只能使用具有相应名称的气泡创建散点图。

1个回答

这种类型的散点图最好在seaborn for Python 中执行。

# example from: https://python-graph-gallery.com/?s=scatter
# library & dataset
import seaborn as sns
df = sns.load_dataset('iris')

# Use the 'hue' argument to provide a factor variable
sns.lmplot( x="sepal_length", y="sepal_width", data=df, fit_reg=False, hue='species', legend=False)

# Move the legend to an empty part of the plot
plt.legend(loc='lower right')

关键字hue将指定哪个变量是用于标记数据点的变量。此外,请查看提供的链接以获取更多示例。