I am trying to highlight a specific City value (categorical) in my seaborn Barplot but every time I feed it the x condition it highlights the wrong bar. e.g. below – I am trying to highlight Los Angeles but it highlights San Francisco instead
df30 = df[df['pop03']>=1700000] #this just selects the 30 most populous cities from my dataframe
plt.figure(figsize=(30,20))
plt.title("Single-Family Home Price Appreciation from 1995Q1 to 2012Q3 for the 30 Largest Metropolitan Areas in the U.S.", fontsize = 30)
cols = ['red' if x == "Los Angeles" else 'green' for x in df30.MSA] # where I run into issues
sns.barplot(x="MSA", y="homepriceg", data=df30, palette = cols,
order=df30.sort_values("homepriceg", ascending=False).MSA)
plt.xlabel("")
plt.ylabel("%", size=30)
plt.xticks(fontsize=20, rotation=60)
plt.yticks(fontsize=20)
sns.set_style("whitegrid")
plt.show()
As you can see – my code currently highlights the “San Francisco” bar vs “Los Angeles” not sure what I am doing wrong. I have tried other states and it still highlights the wrong state. Which is what makes this confusing to debug. New to using seaborn and python.
Highlights wrong city
pandawan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.