Why don't the multiple stacked histplot bars match the legend?

45 views Asked by At
# my 'new_ba', 'route', and 'sentiment' are defined

plt.figure(figsize = (10,5))
plt.title('Sentiment of Route')
colors = {'Positive': 'green', 'Neutral': 'blue', 'Negative': 'red'}

sns.histplot(data=new_ba, x='route', hue='sentiment', palette=colors.values())
plt.xticks(rotation=45, ha='right')

plt.savefig('problem.png')
plt.show()

I don't get an error, but the legend has different colors than the histplot.

enter image description here

1

There are 1 answers

1
norie On

With some dummy data and by simply adding multiple='stack' I was able to produce the image below.

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

new_ba = pd.read_csv('data.csv')
plt.figure(figsize = (10,5))
plt.title('Sentiment of Route')
colors = {'Positive': 'green', 'Neutral': 'blue', 'Negative': 'red'}
sns.histplot(data=new_ba, x='route', hue='sentiment', palette=colors.values(), multiple='stack')
plt.xticks(rotation=45, ha='right')

plt.savefig('problem.png')
plt.show()

Stacked bar chart