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

发布于 2025-01-20 20:26:33 字数 600 浏览 1 评论 0原文

我拥有带有索引(SD至SD17)和关联值(频率)的“ freqs2”的“ freqs2”:

    freqs
SD  101
SD2 128
...     
SD17 65

我想影响每个索引的精确颜色列表(按顺序)。我已经尝试了以下代码:

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

结果,我获得了红色的所有图表条(列表的第一颜色)。

基于类似的问题,我试图集成“ freqs2.index”,以规定颜色列表关注索引,但问题保持不变。

I have the the folowing dataframe "freqs2" with index (SD to SD17) and associated values (frequencies) :

    freqs
SD  101
SD2 128
...     
SD17 65

I would like to affect a list of precise colors (in order) for each index. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

属性 2025-01-27 20:26:33

它看起来像 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()

编辑:Github

It looks like a bug in pandas, plotting directly in matplotlib or using seaborn (which I recommend) works:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文