0

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
avg
  • 793
  • 2
  • 12
  • 24

1 Answers1

1

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

philippjfr
  • 3,997
  • 14
  • 15
  • How can I pass options for multiple entities (in this case, for both Area and Overlay) in your solution? Also, is there a way to define the options once, via a `dict` object for both Area and Overlay, and then set it via the Holomap.opts call? – avg Mar 30 '19 at 05:36