2

I have the following dataframe producing the following plot:

# Import pandas library 
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# initialize data
data = [['tom', 10,1,'a'], ['matt', 15,5,'a'], ['Nick', 14,1,'a']]

# Create the pandas DataFrame 
df = pd.DataFrame(data, columns = ['Name', 'Attempts','Score','Category']) 
print(df.head(3))

   Name  Attempts  Score Category
0   tom        10      1        a
1  matt        15      5        a
2  Nick        14      1        a

# Initialize the matplotlib figure
sns.set()
sns.set_context("paper")
sns.axes_style({'axes.spines.left': True})

f, ax = plt.subplots(nrows=3,figsize=(8.27,11.7))

# Plot
sns.set_color_codes("muted")
sns.barplot(x="Attempts", y='Name', data=df,
            label="Total", color="b", ax=ax[0])
sns.scatterplot(x='Score',y='Name',data=df,zorder=10,color='k',edgecolor='k',ax=ax[0],legend=False)
ax[0].set_title("title")
plt.show()

enter image description here

I want to highlight just the bar Nick in a different color (eg red). Is there an easy way to do this?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
SOK
  • 1,732
  • 2
  • 15
  • 33

1 Answers1

3

In the barplot method, you can use the palette instead of the parameter color and do a loop to check which value you want to change.

sns.barplot(x="Attempts", y='Name', data=df,
            label="Total", palette=["b" if x!='Nick' else 'r' for x in df.Name], ax=ax[0])

and you get enter image description here

Ben.T
  • 29,160
  • 6
  • 32
  • 54