4

How can users change the order of the grouped bars in the example below?

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

matt b
  • 138,234
  • 66
  • 282
  • 345
chalpert
  • 252
  • 1
  • 8

1 Answers1

5

The bar plot method has a categorical_order_by parameter that can be used to change the order. As specified in the documentation, set it equal to values or labels to sort by those corresponding dimensions.

For a custom sort, you can pass a list of values to the categorical_order_by parameter. Since the bar is grouped by two dimensions, the list should contain tuples as in the example below:

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

chalpert
  • 252
  • 1
  • 8