0
# 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

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
EG Film
  • 9
  • 1
  • What does `new_ba` look like? – jared Aug 18 '23 at 04:35
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Aug 18 '23 at 09:36

1 Answers1

1

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

norie
  • 9,609
  • 2
  • 11
  • 18
  • Just to be clear, by default the bars are drawn transparently one of the other. With `multiple='dodge'` the bars would be drawn next to each other. ``multiple='stack'`` stacks them. So, depending on what you want to emphasize, you can consider which visualization best suits your intent. – JohanC Aug 18 '23 at 12:21