【问题标题】:Multi colored bars based on category using matplotlib使用 matplotlib 基于类别的多色条
【发布时间】:2019-12-12 22:27:24
【问题描述】:

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

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

【问题讨论】:

标签: python python-3.x pandas matplotlib


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-05
    • 1970-01-01
    • 2011-11-28
    相关资源
    最近更新 更多