avg
avg

Reputation: 823

Plotting stacked area charts for individual days with Holoviews Holomap

I am trying to plot a Holomap from a pandas Dataframe in wide form. The dataframe has four columns; Datetime, day, V1, V2. Datetime is of the form yyyy-mm-dd hh:mm:ss at 15-minute resolution over two days. V1 and V2 contain the data. After creating the HoloMap object, I can access individual stacked Area charts but when I plot the entire object, I am getting the data for both days laid end to end, with the day selector (Holomap's kdim) only hiding the data for the day not chosen. How can get the map to show the data for only the chosen day?

import os, sys
import pandas as pd
import numpy as np
from datetime import datetime as DateTime
from holoviews import opts
import holoviews as hv
hv.extension('bokeh')
%matplotlib inline
opts.defaults(opts.Area(framewise=True))

tstamp = pd.date_range('2030-04-01',periods = 24*4*2,freq='15min')
V1 = np.random.randn(len(tstamp))
V2 = np.random.randn(len(tstamp))+5

df_in = pd.DataFrame({
            'Datetime':tstamp,
            'V1':V1,
            'V2':V2
        })
df_in['day'] = df_in.Datetime.dt.day
selected_days = df_in.day.unique()

testAreaHmap = hv.HoloMap({d: hv.Area.stack((hv.Area(df_in[df_in.day==d], label='V1',kdims=['Datetime'],vdims=['V1']).opts(color='orange')*\
                hv.Area(df_in[df_in.day==d], label='V2',kdims=['Datetime'],vdims=['V2']).opts(color='blue'))) for d in selected_days}, kdims=['d'])

testAreaHmap

Upvotes: 0

Views: 349

Answers (1)

philippjfr
philippjfr

Reputation: 4080

The problem in your example seems to be that the framewise option didn't stick, likely because of the inlined options (.opts(color='orange'). The most straightforward rewrite of your code therefore looks like this:

testAreaHmap = hv.HoloMap({d: hv.Area.stack((hv.Area(df_in[df_in.day==d], 'Datetime', 'V1', label='V1')*\
                                             hv.Area(df_in[df_in.day==d], 'Datetime', 'V2', label='V2')))
                           for d in selected_days}, kdims=['d'])

testAreaHmap.opts(opts.Area(framewise=True, color=hv.Cycle(['orange', 'blue'])))

Here we separate the declaration of the data from setting the options, which is generally the recommended approach. Another approach to rewriting this kind of code is to use the facilities of HoloViews to apply groupby operations. If you're using the .to API this would look like this:

area_v1 = hv.Dataset(df_in).to(hv.Area, 'Datetime', 'V1', 'day', label='V1')
area_v2 = hv.Dataset(df_in).to(hv.Area, 'Datetime', 'V2', 'day', label='V2')
stacked_areas = (area_v1 * area_v2).map(hv.Area.stack, hv.Overlay)

stacked_areas.opts(opts.Area(color=hv.Cycle(['orange', 'blue']), framewise=True))

Both versions produce this:

enter image description here

Upvotes: 1

Related Questions