Missing Values: Line, Path, Area, and Ribbon¶
Missing value (NaN or None) handling in geom_line(), geom_path(), geom_area(), and geom_ribbon().
Note: geom_path() handles missing values slightly differently than the others.
In [1]:
import numpy as np
import pandas as pd
from datetime import datetime
from lets_plot import *
In [2]:
LetsPlot.setup_html()
In [3]:
economics_url = 'https://raw.githubusercontent.com/JetBrains/lets-plot-docs/master/data/economics.csv'
economics = pd.read_csv(economics_url)
economics['date'] = pd.to_datetime(economics['date'])
start = datetime(2006, 1, 1)
economics = economics.loc[economics['date'] >= start]
In [4]:
economics.head()
Out[4]:
In [5]:
# Make some gaps in the data
economics_with_gaps = economics.copy()
economics_with_gaps.loc[
(economics_with_gaps['date'] >= '2008-01-01') &
(economics_with_gaps['date'] <= '2009-06-01'),
'date'
] = None
economics_with_gaps.loc[
(economics_with_gaps['date'] >= '2012-01-01') &
(economics_with_gaps['date'] <= '2012-04-01'),
'unemploy'
] = np.nan
1. Line¶
In [6]:
(
ggplot(mapping=aes('date', 'unemploy')) +
geom_line(data=economics, size=10, alpha=0.1, tooltips='none') +
geom_line(data=economics_with_gaps, color='teal') +
geom_label(label='Missing dates', x=datetime(2008, 8, 15), y=11000, nudge_x=-70, nudge_unit='px') +
geom_label(label='Missing unemployment\nfigures', x=datetime(2012, 2, 15), y=13000, nudge_x=80, nudge_y=40, nudge_unit='px') +
theme_classic() +
ggsize(800, 300)
)
Out[6]:
2. Path¶
In [8]:
(
ggplot(mapping=aes('date', 'unemploy')) +
geom_line(data=economics, size=10, alpha=0.1, tooltips='none') +
geom_path(data=economics_with_gaps, color='teal') +
geom_label(label='Missing dates', x=datetime(2008, 8, 15), y=11000, nudge_x=-70, nudge_unit='px') +
geom_label(label='Missing unemployment\nfigures', x=datetime(2012, 2, 15), y=13000, nudge_x=80, nudge_y=40, nudge_unit='px') +
theme_classic() +
ggsize(800, 300)
)
Out[8]:
3. Area¶
In [9]:
(
ggplot(mapping=aes('date', 'unemploy')) +
geom_line(data=economics, size=10, alpha=0.1, tooltips='none') +
geom_area(data=economics_with_gaps, color='teal', fill='teal', alpha=0.2) +
geom_label(label='Missing dates', x=datetime(2008, 8, 15), y=12000, nudge_x=-70, nudge_unit='px') +
geom_label(label='Missing unemployment\nfigures', x=datetime(2012, 2, 15), y=13000, nudge_x=80, nudge_y=40, nudge_unit='px') +
theme_classic() +
coord_cartesian(ylim=[None, 20000]) +
ggsize(800, 300)
)
Out[9]:
4. Ribbon¶
In [10]:
# Make some gaps in the data
economics_with_gaps2 = economics.copy()
economics_with_gaps2.loc[
(economics_with_gaps2['date'] >= '2008-01-01') &
(economics_with_gaps2['date'] <= '2009-06-01'),
'date'
] = None
economics_with_gaps2.loc[
(economics_with_gaps2['date'] >= '2011-08-01') &
(economics_with_gaps2['date'] <= '2011-11-01'),
'uempmed'
] = np.nan
economics_with_gaps2.loc[
(economics_with_gaps2['date'] >= '2013-05-01') &
(economics_with_gaps2['date'] <= '2013-08-01'),
'psavert'
] = np.nan
In [11]:
(
ggplot(mapping=aes('date', ymin='psavert', ymax='uempmed')) +
geom_line(aes(y='psavert'), data=economics, size=10, alpha=0.1, tooltips='none') +
geom_line(aes(y='uempmed'), data=economics, size=10, alpha=0.1, tooltips='none') +
geom_ribbon(data=economics_with_gaps2, color='teal', fill='teal', alpha=0.2) +
geom_label(label='Missing dates', x=datetime(2008, 8, 15), y=13, nudge_x=-70, nudge_unit='px') +
geom_label(label='Missing unemployment figures', x=datetime(2011, 8, 15), y=24, nudge_x=80, nudge_unit='px') +
geom_label(label='Missing savings rate figures', x=datetime(2013, 5, 15), y=3.5, nudge_x=50, nudge_unit='px') +
theme_classic() +
ggsize(800, 300)
)
Out[11]: