break_width Parameter in Datetime Scales#
In datetime scales, the break_width parameter accepts a string specifying the interval between breaks, e.g., “2 weeks”, “3 months”, “12 hours”.
import pandas as pd
from datetime import datetime
from lets_plot import *
LetsPlot.setup_html()
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(2007, 1, 1)
end = datetime(2013, 1, 1)
economics = economics.loc[economics['date'].between(start, end)]
economics.head()
| Unnamed: 0 | date | pce | pop | psavert | uempmed | unemploy | |
|---|---|---|---|---|---|---|---|
| 474 | 475 | 2007-01-01 | 9516.3 | 300574.0 | 3.7 | 8.3 | 7116 |
| 475 | 476 | 2007-02-01 | 9546.8 | 300802.0 | 4.1 | 8.5 | 6927 |
| 476 | 477 | 2007-03-01 | 9585.1 | 301021.0 | 4.4 | 9.1 | 6731 |
| 477 | 478 | 2007-04-01 | 9615.7 | 301254.0 | 4.2 | 8.6 | 6850 |
| 478 | 479 | 2007-05-01 | 9651.3 | 301483.0 | 4.0 | 8.2 | 6766 |
# a datetime scale with auto-selected breaks
(
ggplot(mapping=aes('date', 'unemploy'))
+ geom_line(data=economics, size=5, alpha=0.5, color='teal')
+ theme_classic()
+ ggsize(800, 300)
)
(
ggplot(mapping=aes('date', 'unemploy'))
+ geom_line(data=economics, size=5, alpha=0.5, color='teal')
+ scale_x_datetime(
break_width='3 months', # <-- breaks every 3 months
format="%b '%y")
+ theme_classic()
+ ggsize(800, 300)
)