2

I have a data set with one key dimension and one value dimension, where the key dimension is month name and the value dimension is a number. When I plot this using a hv.Curve(...) the months are not in chronological order. What is the best way to fix this?

enter image description here

rindis
  • 869
  • 11
  • 25

1 Answers1

1

My guess is your months are probably strings right now.

So easiest thing is to:
1. first convert your string months to proper pandas datetime field
2. and then sort your dataframe on the new datetime field

Here's an example:

# import libraries
import pandas as pd
import holoviews as hv
hv.extension('bokeh', logo=False)

# create sample dataframe
data = {
    'month': ['August', 'June', 'May', 'April', 'July'], 
    'value': [3, 5, 7, 9, 5],
}
df = pd.DataFrame(data=data)

# convert month string to pandas datetime
# here I've added 2019 to the string, to make it a proper 2019 year
# but this isn't really necessary.
df['month2'] = pd.to_datetime('2019 ' + df['month'], format='%Y %B')

# sort your dataframe on the datetime field
df = df.sort_values(by='month2')

# since your dataframe is now sorted correctly, you can make your plot
hv.Curve(data=df, kdims='month', vdims='value')
Sander van den Oord
  • 10,986
  • 5
  • 51
  • 96