以下代码用于获取数据集中每个类的颜色:
data = data.sort_values(by="Sentiment")
sentiment=data['Sentiment'].unique()
palette = sns.husl_palette(len(sentiment), l=0.7)
SAge_Graph = sns.countplot(x='Age', hue='Sentiment',
data=data,palette=palette ,order=sorted(unique_age_groups))
legendInf = {}
for sentiment, color in zip(sentiment, palette):
legendInf[matplotlib.colors.rgb2hex(color)] = sentiment
print(legendInf)
哪个给了我以下输出:
{'#f8889a': 'Negative', '#57bf36': 'Neutral', '#59b0f6': 'Positive'}
在下面的代码中,我遍历每个条以获取其坐标和颜色:
bars = [rect for rect in SAge_Graph.get_children() if isinstance(rect, matplotlib.patches.Rectangle)]
barInfo={}
barInfo['coordinates']=[]
barInfo['colors']=[]
for bar in bars :
w,h=bar.get_width(),bar.get_height()
x0,y0=bar.xy
x1, y1 = x0 + w, y0
x2, y2 = x0, y0 + h
x3, y3 = x0 + w, y0 + h
barInfo['coordinates'].append(((x0, y0), (x1, y1), (x2, y2), (x3, y3)))
barInfo['colors'].append(matplotlib.colors.rgb2hex(bar.get_facecolor()))
哪个给我以下输出:
coordinates colors
0 ((-0.4, 0), (-0.13333333333333336, 0), (-0.4, ... #ea96a3
1 ((2.8666666666666667, 0), (3.1333333333333333,... #60ae47
2 ((3.1333333333333333, 0), (3.4, 0), (3.1333333... #6daee2
并且两者的颜色都不相同,为什么会产生这种差异?