python-3.x - 在seaborn的分组箱线图中突出显示一个点
问题描述
我需要在每个箱线图中突出显示一个特定点。例如,我想在每个物种的petal_length 的箱线图中突出显示petal_width 为0.8 的点。
这是示例:
iris = sns.load_dataset('iris')
##Create three points where petal_width is 0.8 for each species
iris_2 = pd.DataFrame({'sepal_length':Series([1,2,3],dtype='float32'), 'sepal_width':Series([1.1,2.1,3.1],dtype='float32'),
'petal_length':Series([1,2,3],dtype='float32'), 'petal_width':Series([0.8,0.8,0.8],dtype='float32'),
'species':Series(['setosa','versicolor','virginica'])})
iris_all = pd.concat([iris, iris_2]).reset_index(drop = True)
sns.boxplot(x='species', y = 'petal_length', data = iris_all)
sns.regplot(x= iris_all['species'][iris_all['petal_width'] == 0.8],
y= iris_all['petal_length'][iris_all['petal_width'] == 0.8], scatter=True, fit_reg=False, marker='o',
scatter_kws={"s": 100})
但是代码不起作用。我想知道我该如何纠正它。谢谢。
解决方案
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
iris = sns.load_dataset('iris')
# Create three points where petal_width is 0.8 for each species
iris_2 = pd.DataFrame(
{'sepal_length': pd.Series([1, 2, 3], dtype='float32'), 'sepal_width': pd.Series([1.1, 2.1, 3.1], dtype='float32'),
'petal_length': pd.Series([1, 2, 3], dtype='float32'), 'petal_width': pd.Series([0.8, 0.8, 0.8], dtype='float32'),
'species': pd.Series(['setosa', 'versicolor', 'virginica'])})
iris_all = pd.concat([iris, iris_2]).reset_index(drop=True)
sns.boxplot(x='species', y='petal_length', data=iris_all)
sns.regplot(x=iris_all['species'][(iris_all['petal_width'] > 0.79) & (iris_all['petal_width'] < 0.81)],
y=iris_all['petal_length'][(iris_all['petal_width'] > 0.79) & (iris_all['petal_width'] < 0.81)],
color='blue',
scatter=True, fit_reg=False,
marker='+',
scatter_kws={"s": 100})
plt.show()
推荐阅读
- flutter - Flutter:我想访问 setstate 的更改值,当我访问它时它没有显示新的状态值
- android - Android - 通过 Intent 启动另一个应用程序时保持演示文稿显示
- python - 输出: [
] - python - 重命名我的 pandas DataFrames 中的前 10 列
- sql - SQL Server 存储过程返回超过 50 行超时,但不是一直
- excel - 无法将formua注入excel单元格
- shell - 更改 Xamarin.Forms Shell 顶部 TabBar 的 BackgroundColor(不更改 NavigationbarColor)
- r - 为什么 lapply 带有闭包会出错?
- javascript - 递归搜索深度嵌套的对象/数组,并在不知道键的情况下收集包含指定字符串的值
- python - 使用 count_values() 时如何获取计数值的名称?