Workaround for date-based histogram tick labels?

Hi, I am having trouble controlling tick labels for histogram-type plots. I have time series data, and would like to bin the data according to different time resolutions. If there is some way I can change the plotting approach, or the pandas dataframe so that I can have the x axis ticks from the top plot to be used in the bottom plot also, I would be very happy.

I am using hvplot 0.5.2.

import numpy as np
import pandas as pd

import hvplot.pandas
import holoviews as hv
from bokeh.models.formatters import DatetimeTickFormatter

# Create data frame
days = pd.date_range(start=pd.to_datetime('2001-01-01'), end=pd.to_datetime('2001-12-31'))
ncols = 5
columns = np.arange(ncols) + 1
data = np.random.rand(365, ncols)
df = pd.DataFrame(data, index=days, columns=columns)
df.index.name = 'time'

# Determine number of days where row mean is above 0.5
mean_above_midpoint = (df.agg('mean', axis=1) > 0.5).astype(int)
df['days_above_midpoint'] = mean_above_midpoint
df_weekly = df.resample('W').mean()
df_weekly['days_above_midpoint'] = df['days_above_midpoint'].resample('W').sum()
df_weekly.head()

formatter = DatetimeTickFormatter(months='%b')
columnLabels = [str(x) for x in columns]

lines = df_weekly.hvplot.line(x='time', y=columnLabels, title='Measurements', xformatter=formatter)
hist  = df_weekly.hvplot.bar(x='time', y='days_above_midpoint', title='Days Above Midpoint', xformatter=formatter)

(lines + hist).cols(1)

1 Like

So this is an unfortunate quirk of the Bars element in HoloViews which is always categorical and secondarily an issue with categorical axes in bokeh, which do not allow reducing the tick frequency. All I can suggest for the time being is to make use of the Histogram element in HoloViews:

lines = df_weekly.hvplot.line(x='time', y=columnLabels, title='Measurements', xformatter=formatter)
hist  = hv.Histogram(df_weekly.hvplot.bar(x='time', y='days_above_midpoint')).opts(title='Days Above Midpoint', xformatter=formatter, width=700)

(lines + hist).cols(1)

That said, I would appreciate it if you could file an issue about this in hvPlot.

Thanks for the help! I will file an issue today.

Out of curiosity, would using altair/vega also be worth exploring? I am eventually aiming for plots that link the x axes for pan/zoom, that also support datetime x axis ticks as mentioned above.

Sure, altair/vega are also nice solution, but I can’t help you much there.

I am eventually aiming for plots that link the x axes for pan/zoom, that also support datetime x axis ticks as mentioned above.

That’s already the case with the solution I suggested above.

Apologies if I am pushing my luck, but I have also been spending a lot of time (several days) trying to get the top plot to be a box+whisker plot, binned by week, with linked pan/zoom to the bottom bar plot, also binned by week. This seems to be more difficult than the top plot being lines, because box+whisker seems to require a by= parameter that changes the axis meaning. Any thoughts on how to overcome this problem and have both plots share the same time axis?