简体   繁体   English

如何影响 colors 列表到 matplotlib 中的直方图索引栏?

[英]How to affect a list of colors to histogram index bar in matplotlib?

I have the the folowing dataframe "freqs2" with index (SD to SD17) and associated values (frequencies):我有以下 dataframe“freqs2”,带有索引(SD 到 SD17)和相关值(频率):

    freqs
SD  101
SD2 128
...     
SD17 65

I would like to affect a list of precise colors (in order) for each index.我想影响每个索引的精确列表 colors(按顺序)。 I've tried the following code:我试过以下代码:

colors=['#e5243b','#DDA63A', '#4C9F38','#C5192D','#FF3A21','#26BDE2','#FCC30B','#A21942','#FD6925','#DD1367','#FD9D24','#BF8B2E','#3F7E44','#0A97D9','#56C02B','#00689D','#19486A']
freqs2.plot.bar(freqs2.index, legend=False,rot=45,width=0.85, figsize=(12, 6),fontsize=(14),color=colors )
plt.ylabel('Frequency',fontsize=(17))

As result I obtain all my chart bars in red color (first color of the list).结果,我获得了所有红色的图表条(列表的第一种颜色)。

Based on similar questions, I've tried to integrate "freqs2.index" to stipulate that the list of colors concern index but the problem stay the same.基于类似的问题,我尝试整合“freqs2.index”来规定colors列表关注索引但问题保持不变。

It looks like a bug in pandas, plotting directly in matplotlib or using seaborn (which I recommend) works:它看起来像 pandas 中的错误,直接在matplotlib中绘制或使用seaborn (我推荐)有效:

import seaborn as sns

colors=['#e5243b','#dda63a', '#4C9F38','#C5192D','#FF3A21','#26BDE2','#FCC30B','#A21942','#FD6925','#DD1367','#FD9D24','#BF8B2E','#3F7E44','#0A97D9','#56C02B','#00689D','#19486A']

# # plotting directly with matplotlib works too:
# fig = plt.figure()
# ax = fig.add_axes([0,0,1,1])
# ax.bar(x=df.index, height=df['freqs'], color=colors)

ax = sns.barplot(data=df, x= df.index, y='freqs', palette=colors)
ax.tick_params(axis='x', labelrotation=45)

plt.ylabel('Frequency',fontsize=17)
plt.show()

Edit: an issue already exists on Github编辑: Github上已经存在问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM