How to make area chart cover all area below it in pandas bokeh?

113 Views Asked by At

I tried to make an area chart with pandas-bokeh package using this code:

import pandas as pd
import numpy as np
import pandas_bokeh

df = pd.read_excel('D:/Coding Practice/data/data_corona_usa.xlsx')
df = df.iloc[::-1].reset_index()
df['Cummulative Cases'] = df['cases'].cumsum()
df['date'] = df['dateRep'].dt.strftime('%D')


#to make it appear in your notebook
pandas_bokeh.output_notebook()

df.plot_bokeh(
    kind='area',
    x= 'dateRep',
    y='Cummulative Cases',
    xlabel = 'Date',
    ylabel = 'Cummulative Cases',
    hovertool_string= r'''<h1> Date: @{date} </h1>
        <h2> Cummulative Cases: @{Cummulative Cases} </h2>''',
    title='US Corona Cases (cummulative)',
    hovertool=True,
    fontsize_title=18,
    logy=True,
    stacked=False,
    legend='top_left'
)

But the resulting area chart is just like this image.

strange area chart

The chart do not cover the area below that unknown diagonal line. This result happens because of the code "logy = True" that make ytick uses logarithmic scale. If i delete this one line of code, the area chart would appear normal like this.

normal area chart

Even so, i want the area chart use logarithmic scale on y-axis. How can i fix it?

1

There are 1 best solutions below

0
On

First of all you have to understand that you minimal example is not a working example. Your local path makes it very difficult to reproduce the behavior, because nobody is able to load the data like you do.

Second, I have to tell you, that your problem isn't solveable, if you want to use pandas-bokeh. This is because the area plot is initialized with zeros inside this module and if you use a logarithmic y axes zero doesn't exist. The reason is, that log(0) is not defined. What you can see is the default behavior.

If you are willing to switch to bokeh itself, you are welcome to follow this minimal example:

Some example data:

import pandas as pd
df = pd.DataFrame({'cases':range(100)}))
df['Cummulative Cases'] = df['cases'].cumsum()
df 

initial data

Now we want to reproduce your observation in plain bokeh.

from bokeh.io import show, output_notebook
from bokeh.plotting import figure
from bokeh.models import ColumnDataSource
output_notebook()

p = figure(x_range=(0, 100), y_range=(0.1,100000),y_axis_type="log")
source = ColumnDataSource(dict(x=df['cases'].values, 
                               y1=df['Cummulative Cases'].values, 
                               y2=[0]*df.shape[0]
                              )
                          )
p.varea(x='x', y1='y1', y2='y2', source=source)
show(p)

old behavior

If you set the baseline diffrent from zero, may like this y2=[0.01]*df.shape[0] you get want you are looking for.

new behavior