如何使用Chartify设置条形图的顺序?

时间:2018-11-09 17:16:15

标签: python data-visualization chartify

在下面的示例中,用户如何更改分组条的顺序?

ch = chartify.Chart(blank_labels=True, x_axis_type='categorical') 
ch.plot.bar(
    data_frame=quantity_by_fruit_and_country,
    categorical_columns=['fruit', 'country'],
    numeric_column='quantity')
ch.show('png')

Grouped bar plot

1 个答案:

答案 0 :(得分:4)

条形图方法具有一个categorical_order_by参数,可用于更改顺序。如文档中所指定,将其设置为valueslabels以按那些相应的尺寸排序。

对于自定义排序,可以将值列表传递给categorical_order_by参数。由于栏是按二维分组的,因此列表应包含元组,如下例所示:

from itertools import product
outside_groups = ['Apple', 'Orange', 'Banana', 'Grape']
inner_groups = ['US', 'JP', 'BR', 'CA', 'GB']
sort_order = list(product(outside_groups, inner_groups))

# Plot the data
ch = chartify.Chart(blank_labels=True, x_axis_type='categorical')
ch.plot.bar(
    data_frame=quantity_by_fruit_and_country,
    categorical_columns=['fruit', 'country'],
    numeric_column='quantity',
    categorical_order_by=sort_order)
ch.show('png')

enter image description here