Skip to content

Commit 3c2fdfb

Browse files
document xperiod and date histograms
1 parent a116f26 commit 3c2fdfb

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

doc/python/time-series.md

+68
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,74 @@ fig.update_xaxes(
122122
fig.show()
123123
```
124124

125+
### Summarizing Time-series Data with Histograms
126+
127+
Plotly [histograms](/python/histograms/) are powerful data-aggregation tools which even work on date axes. In the figure below, we pass in daily data and display it as monthly averages by setting `histfunc="avg` and `xbins_size="M1"`.
128+
129+
```python
130+
import plotly.express as px
131+
import plotly.graph_objects as go
132+
import pandas as pd
133+
134+
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
135+
136+
fig = px.histogram(df, x="Date", y="AAPL.Close", histfunc="avg", title="Histogram on Date Axes")
137+
fig.update_traces(xbins_size="M1")
138+
fig.update_xaxes(showgrid=True, ticklabelmode="period", dtick="M1", tickformat="%b\n%Y")
139+
fig.update_layout(bargap=0.1)
140+
fig.add_trace(go.Scatter(mode="markers", x=df["Date"], y=df["AAPL.Close"], name="daily"))
141+
fig.show()
142+
```
143+
144+
### Displaying Period Data
145+
146+
_new in 4.11_
147+
148+
If your data coded "January 1" or "January 31" in fact refers to data collected throughout the month of January, for example, you can configure your traces to display their marks at the start end, or middle of the month with the `xperiod` and `xperiodalignment` attributes. In the example below, the raw data is all coded with an X value of the 10th of the month, but is binned into monthly periods with `xperiod="M1"` and then displayed at the start, middle and end of the period.
149+
150+
```python
151+
import plotly.graph_objects as go
152+
import pandas as pd
153+
154+
df = pd.DataFrame(dict(
155+
date=["2020-01-10", "2020-02-10", "2020-03-10", "2020-04-10", "2020-05-10", "2020-06-10"],
156+
value=[1,2,3,1,2,3]
157+
))
158+
159+
fig = go.Figure()
160+
fig.add_trace(go.Scatter(
161+
name="Raw Data",
162+
mode="markers+lines", x=df["date"], y=df["value"],
163+
marker_symbol="star"
164+
))
165+
fig.add_trace(go.Scatter(
166+
name="Start-aligned",
167+
mode="markers+lines", x=df["date"], y=df["value"],
168+
xperiod="M1",
169+
xperiodalignment="start"
170+
))
171+
fig.add_trace(go.Scatter(
172+
name="Middle-aligned",
173+
mode="markers+lines", x=df["date"], y=df["value"],
174+
xperiod="M1",
175+
xperiodalignment="middle"
176+
))
177+
fig.add_trace(go.Scatter(
178+
name="End-aligned",
179+
mode="markers+lines", x=df["date"], y=df["value"],
180+
xperiod="M1",
181+
xperiodalignment="end"
182+
))
183+
fig.add_trace(go.Bar(
184+
name="Middle-aligned",
185+
x=df["date"], y=df["value"],
186+
xperiod="M1",
187+
xperiodalignment="middle"
188+
))
189+
fig.update_xaxes(showgrid=True, ticklabelmode="period")
190+
fig.show()
191+
```
192+
125193
### Time Series Plot with Custom Date Range
126194

127195
The data range can be set manually using either `datetime.datetime` objects, or date strings.

0 commit comments

Comments
 (0)