Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 31fd10a

Browse files
ticklabelmode docs
1 parent 0c8f2e6 commit 31fd10a

File tree

1 file changed

+57
-4
lines changed

1 file changed

+57
-4
lines changed

doc/python/time-series.md

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,8 @@ Plotly auto-sets the axis type to a date format when the corresponding data are
4545
# Using plotly.express
4646
import plotly.express as px
4747

48-
import pandas as pd
49-
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
50-
51-
fig = px.line(df, x='Date', y='AAPL.High')
48+
df = px.data.stocks()
49+
fig = px.line(df, x='date', y="GOOG")
5250
fig.show()
5351
```
5452

@@ -63,6 +61,61 @@ fig = go.Figure([go.Scatter(x=df['Date'], y=df['AAPL.High'])])
6361
fig.show()
6462
```
6563

64+
### Different Chart Types on Date Axes
65+
66+
Any kind of cartesian chart can be placed on `date` axes, for example this bar chart of relative stock ticker values.
67+
68+
```python
69+
import plotly.express as px
70+
71+
df = px.data.stocks(indexed=True)-1
72+
fig = px.bar(df, x=df.index, y="GOOG")
73+
fig.show()
74+
```
75+
76+
Or this [facetted](/python/facet-plots/) area plot:
77+
78+
```python
79+
import plotly.express as px
80+
81+
df = px.data.stocks(indexed=True)-1
82+
fig = px.area(df, facet_col="company", facet_col_wrap=2)
83+
fig.show()
84+
```
85+
86+
### Configuring Tick Labels
87+
88+
By default, the tick labels (and optional ticks) are associated with a specific grid-line, and represent an *instant* in time, for example, "midnight on February 1, 2018". Tick labels can be formatted using the `tickformat` attribute (which accepts the [d3 time-format formatting strings](https://github.com/d3/d3-time-format)) to display only the month and year, but they still represent an instant by default, so in the figure below, the text of the label "Feb 2018" spans part of the month of January and part of the month of February. The `dtick` attribute controls the spacing between gridlines, and the `"M1"` setting means "1 month". This attribute also accepts a number of milliseconds, which can be scaled up to days by multiplying by `24*60*60*1000`.
89+
90+
```python
91+
import plotly.express as px
92+
df = px.data.stocks(indexed=True)
93+
fig = px.line(df, title="custom tick labels")
94+
fig.update_xaxes(
95+
dtick="M1",
96+
tickformat="%b %Y",
97+
range=["2018-01-01", "2018-12-31"])
98+
fig.show()
99+
```
100+
101+
### Moving Tick Labels to the Middle of the Period
102+
103+
_new in 4.10_
104+
105+
By setting the `ticklabelmode` attribute to `"period"` (the default is `"instant"`) we can move the tick labels to the middle of the period they represent. The gridlines remain at the beginning of each month (thanks to `dtick="M1"`) but the labels now span the month they refer to.
106+
107+
```python
108+
import plotly.express as px
109+
df = px.data.stocks(indexed=True)
110+
fig = px.line(df, title='custom tick labels with ticklabelmode="period"')
111+
fig.update_xaxes(
112+
dtick="M1",
113+
tickformat="%b %Y",
114+
ticklabelmode="period",
115+
range=["2018-01-01", "2018-12-31"])
116+
fig.show()
117+
```
118+
66119
### Time Series Plot with Custom Date Range
67120

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

0 commit comments

Comments
 (0)