gpt4 book ai didi

python - 使用 matplotlib 基于类别的多色条

转载 作者:太空宇宙 更新时间:2023-11-03 20:00:03 24 4
gpt4 key购买 nike

我已经根据“结果”字段中的三个值创建了一个图表。你知道我如何使用下面已有的代码根据三个值(已放弃、已连接、到语音邮件)更改颜色吗:

data.direction.describe()
import seaborn as sns
%matplotlib inline
import matplotlib.pyplot as plt
sns.set() # use Seaborn styles

df = data.pivot_table('call_id', index='timeslot', columns='result', aggfunc='count')
ax = df.plot(kind='bar', width=0.7, align='center', stacked=False, rot=90, figsize=(12,6), legend=False, zorder=3)

plt.grid(zorder=0)
plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.3))
plt.xlabel("Timeslot")
plt.ylabel("Number of calls")
plt.title("Figure 4: Number of calls connected to the admin line by time slot")

ax.spines['bottom'].set_color('black')
ax.spines['top'].set_color('white')
ax.spines['right'].set_color('white')
ax.spines['left'].set_color('black')

ax.yaxis.label.set_color('black')
ax.xaxis.label.set_color('black')

ax.title.set_color('black')

ax.patch.set_facecolor('white')

plt.savefig('figure4.png', dpi=300, facecolor=ax.get_facecolor(), transparent=True, bbox_inches='tight', pad_inches=0.1)
plt.show()


这就是它目前的样子,但我希望能够选择颜色。 Chart

最佳答案

由于您使用的是 pandas,我建议您使用 color 参数将颜色添加到对 df.plot 的调用中。

ax = df.plot(color=my_colors, kind='bar', width=0.7,

其中“my_colors”是您要使用的颜色列表。

关于python - 使用 matplotlib 基于类别的多色条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59305902/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com