Holoviews: Sort Categorical Axis

One of the challenges when working with categorical data in pandas and Holoviews can be ordering appearance in a plot. To get around this, if you sort the pandas dataframe prior to sending the data to your Holoviews boxplot you can avoid the issue.

Source for example: https://stackoverflow.com/a/63508053

# Sort the df by Treatment for later ordering.
def trt_sorter(column):
"""Sort function"""
trt_order = ['1_naive','2_naive_wc','2_even-shuf_wc','2_flip1_wc','2_flip50_wc']
correspondence = {trt: order for order, trt in enumerate(trt_order)}
return column.map(correspondence)
Remove the "10000" from treatments.
df['Treatment'] = df['Treatment'].map(lambda x: x.rstrip('_10000'))
df = df.sort_values(by="Treatment", key=trt_sorter)

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you a spammer? *