将图例添加到Chartify中的分组条形图中

时间:2019-03-02 15:09:27

标签: pandas chartify

我有以下Chartify代码段。

ch = chartify.Chart(blank_labels=True, x_axis_type='categorical', layout="slide_100%")
ch.set_title("Grouped bar chart")
ch.set_subtitle(
    "Pass a list to group by multiple factors. Color grouped by 'fruit'")
ch.plot.bar(
    data_frame=df_chart,
    categorical_columns=["month", "account"],
    numeric_column="bill",
    color_column="account",
    categorical_order_by=sort_order
)
ch.plot.text(
    data_frame=df_chart,
    categorical_columns=["month", "account"],
    numeric_column='bill',
    text_column='billdisp',
    color_column='account',
    categorical_order_by=sort_order
)
ch.axes.hide_xaxis()
ch.show('png')

它产生以下图表:

enter image description here

每个条形对应于“帐户”列。如您所见,X轴已经很拥挤了。

如何在图表中添加图例?我想告诉您,蓝色=帐户1,绿色=帐户2,橙色=帐户3。

1 个答案:

答案 0 :(得分:0)

您可以通过添加3个额外的空白图来伪造它,这些空白图只能添加您选择的图例。

# Previous code

plt.plot([], [], label='Account 1', c='b')
plt.plot([], [], label='Account 2', c='g')
plt.plot([], [], label='Account 3', c='orange')
plt.legend()
plt.show()