简体   繁体   English

如何将来自不同列的标记添加到 seaborn pandas 条形图

[英]How to add a marker from a different column to a seaborn pandas barplot

I have the following dataset, code and plot:我有以下数据集、代码和 plot:

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
data = [['tom', 10,15], ['matt', 13,10]]

df3 = pd.DataFrame(data, columns = ['Name', 'Attempts','L4AverageAttempts']) 

f,ax = plt.subplots(nrows=1,figsize=(16,9))
sns.barplot(x='Attempts',y='Name',data=df3)
plt.show()

在此处输入图像描述

How can get a marker of some description (dot, *, shape, etc) to show that tom has averaged 15 (so is below his average) and matt has averaged 10 so is above average.如何获得某种描述的标记(点、*、形状等)以显示tom的平均得分为 15(因此低于他的平均水平), matt的平均得分为 10,因此高于平均水平。 So a marker basxed off the L4AverageAttempts value for each person.因此,一个标记会根据每个人的L4AverageAttempts值进行标记。

I have looked into axvline but that seems to be only a set number rather than a specific value for each y axis category.我研究了axvline但这似乎只是一个设定的数字,而不是每个 y 轴类别的特定值。 Any help would be much appreciated!任何帮助将非常感激! thanks!谢谢!

You can simply plot a scatter plot on top of your bar plot using L4AverageAttempts as the x value:您可以简单地 plot 散布 plot 在您的酒吧 plot 顶部使用L4AverageAttempts作为 x 值:

You can use seaborn.scatterplot for this.您可以为此使用seaborn.scatterplot Make sure to set the zorder parameter so that the markers appear on top of the bars.确保设置zorder参数,以便标记出现在条形顶部。

import seaborn as sns
import pandas as pd
data = [['tom', 10,15], ['matt', 13,10]]

df3 = pd.DataFrame(data, columns = ['Name', 'Attempts','L4AverageAttempts'])

f,ax = plt.subplots(nrows=1,figsize=(16,9))
sns.barplot(x='Attempts',y='Name',data=df3)

sns.scatterplot(x='L4AverageAttempts', y="Name", data=df3, zorder=10, color='k', edgecolor='k')

plt.show()

在此处输入图像描述

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

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