asimo
asimo

Reputation: 2500

Bokeh Area Chart unable to plot

I am unable to plot the area chart in bokeh for some reason..

Below is the code used for the same..

from bokeh.charts import Area, show, output_file 

Areadict = dict( 
    I = df['IEXT'], 
    Date=df['Month'], 
    O = df['OT'] 
) 

 area = Area(Areadict, x='Date', y=['I','O'], title="Area Chart", 
             legend="top_left", 
               xlabel='time', ylabel='memory') 

 output_file('area.html') 
 show(area)

All i see if the date axis getting plotted, but no signs of the two areacharts that I am interested in. Please advise

Upvotes: 0

Views: 1641

Answers (1)

bigreddot
bigreddot

Reputation: 34568

I would recommend looking at Holoviews which is a very high level API built on top of Bokeh, and is endorsed by the Bokeh team. You can see an Area chart example in their documentation. Basically it looks like:

# create holoviews objects
dims = dict(kdims='time', vdims='memory')
python = hv.Area(python_array, label='python', **dims)
pypy   = hv.Area(pypy_array,   label='pypy',   **dims)
jython = hv.Area(jython_array, label='jython', **dims)

# plot
overlay.relabel("Area Chart") + hv.Area.stack(overlay).relabel("Stacked Area Chart")

Which results in

enter image description here

Otherwise, as of Bokeh 0.13 to create a stacked area chart with the stable bokeh.plotting API, you will need to stack the data yourself, as shown in this example:

import numpy as np
import pandas as pd

from bokeh.plotting import figure, show, output_file
from bokeh.palettes import brewer

N = 20
cats = 10
df = pd.DataFrame(np.random.randint(10, 100, size=(N, cats))).add_prefix('y')

def  stacked(df):
    df_top = df.cumsum(axis=1)
    df_bottom = df_top.shift(axis=1).fillna({'y0': 0})[::-1]
    df_stack = pd.concat([df_bottom, df_top], ignore_index=True)
    return df_stack

areas = stacked(df)
colors = brewer['Spectral'][areas.shape[1]]
x2 = np.hstack((df.index[::-1], df.index))

p = figure(x_range=(0, N-1), y_range=(0, 800))
p.grid.minor_grid_line_color = '#eeeeee'

p.patches([x2] * areas.shape[1], [areas[c].values for c in areas],
          color=colors, alpha=0.8, line_color=None)

show(p)

which results in

enter image description here

Upvotes: 2

Related Questions