为什么我的图例与我的条形颜色不同?

问题描述 投票:0回答:1
# 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()

我没有收到错误,但图例的颜色与

histplot
不同。

enter image description here

python matplotlib seaborn diagram
1个回答
0
投票

使用一些虚拟数据并简单地添加

multiple='stack'
,我就能够生成下面的图像。

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

© www.soinside.com 2019 - 2024. All rights reserved.