You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
66
119
### Time Series Plot with Custom Date Range
67
120
68
121
The data range can be set manually using either `datetime.datetime` objects, or date strings.
0 commit comments