简体   繁体   中英

How to add a marker from a different column to a seaborn pandas barplot

I have the following dataset, code and 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. So a marker basxed off the L4AverageAttempts value for each person.

I have looked into axvline but that seems to be only a set number rather than a specific value for each y axis category. 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:

You can use seaborn.scatterplot for this. Make sure to set the zorder parameter so that the markers appear on top of the bars.

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()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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