|
| 1 | +--- |
| 2 | +jupyter: |
| 3 | + jupytext: |
| 4 | + notebook_metadata_filter: all |
| 5 | + text_representation: |
| 6 | + extension: .md |
| 7 | + format_name: markdown |
| 8 | + format_version: '1.1' |
| 9 | + jupytext_version: 1.1.1 |
| 10 | + kernelspec: |
| 11 | + display_name: Python 3 |
| 12 | + language: python |
| 13 | + name: python3 |
| 14 | + language_info: |
| 15 | + codemirror_mode: |
| 16 | + name: ipython |
| 17 | + version: 3 |
| 18 | + file_extension: .py |
| 19 | + mimetype: text/x-python |
| 20 | + name: python |
| 21 | + nbconvert_exporter: python |
| 22 | + pygments_lexer: ipython3 |
| 23 | + version: 3.6.8 |
| 24 | + plotly: |
| 25 | + description: How to format axes ticks in Python with Plotly. |
| 26 | + display_as: file_settings |
| 27 | + has_thumbnail: true |
| 28 | + ipynb: ~notebook_demo/1 |
| 29 | + language: python |
| 30 | + layout: user-guide |
| 31 | + name: Formatting Ticks |
| 32 | + order: 10 |
| 33 | + permalink: python/tick-formatting/ |
| 34 | + thumbnail: thumbnail/tick-formatting.gif |
| 35 | + title: Formatting Ticks | Plotly |
| 36 | +--- |
| 37 | + |
| 38 | +#### Tickmode - Linear |
| 39 | + |
| 40 | + |
| 41 | +If `"linear"`, the placement of the ticks is determined by a starting position `tick0` and a tick step `dtick` |
| 42 | + |
| 43 | +```python |
| 44 | +import plotly.graph_objects as go |
| 45 | + |
| 46 | +fig = go.Figure(go.Scatter( |
| 47 | + x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], |
| 48 | + y = [28.8, 28.5, 37, 56.8, 69.7, 79.7, 78.5, 77.8, 74.1, 62.6, 45.3, 39.9] |
| 49 | +)) |
| 50 | + |
| 51 | +fig.update_layout( |
| 52 | + xaxis = dict( |
| 53 | + tickmode = 'linear', |
| 54 | + tick0 = 0.5, |
| 55 | + dtick = 0.75 |
| 56 | + ) |
| 57 | +) |
| 58 | + |
| 59 | +fig.show() |
| 60 | +``` |
| 61 | + |
| 62 | +#### Tickmode - Array |
| 63 | + |
| 64 | + |
| 65 | +If `"array"`, the placement of the ticks is set via `tickvals` and the tick text is `ticktext`. |
| 66 | + |
| 67 | +```python |
| 68 | +import plotly.graph_objects as go |
| 69 | + |
| 70 | +go.Figure(go.Scatter( |
| 71 | + x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], |
| 72 | + y = [28.8, 28.5, 37, 56.8, 69.7, 79.7, 78.5, 77.8, 74.1, 62.6, 45.3, 39.9] |
| 73 | +)) |
| 74 | + |
| 75 | +fig.update_layout( |
| 76 | + xaxis = dict( |
| 77 | + tickmode = 'array', |
| 78 | + tickvals = [1, 3, 5, 7, 9, 11], |
| 79 | + ticktext = ['One', 'Three', 'Five', 'Seven', 'Nine', 'Eleven'] |
| 80 | + ) |
| 81 | +) |
| 82 | + |
| 83 | +fig.show() |
| 84 | +``` |
| 85 | + |
| 86 | +#### Using Tickformat Attribute |
| 87 | + |
| 88 | + |
| 89 | +For more formatting types, see: https://github.com/d3/d3-format/blob/master/README.md#locale_format |
| 90 | + |
| 91 | +```python |
| 92 | +import plotly.graph_objects as go |
| 93 | + |
| 94 | +go.Figure(go.Scatter( |
| 95 | + x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], |
| 96 | + y = [28.8, 28.5, 37, 56.8, 69.7, 79.7, 78.5, 77.8, 74.1, 62.6, 45.3, 39.9] |
| 97 | +)) |
| 98 | + |
| 99 | +fig.update_layout(yaxis_tickformat = '%') |
| 100 | + |
| 101 | +fig.show() |
| 102 | +``` |
| 103 | + |
| 104 | +#### Using Tickformat Atttribute - Date/Time |
| 105 | + |
| 106 | + |
| 107 | +For more date/time formatting types, see: https://github.com/d3/d3-time-format/blob/master/README.md |
| 108 | + |
| 109 | +```python |
| 110 | +import plotly.graph_objects as go |
| 111 | + |
| 112 | +import pandas as pd |
| 113 | + |
| 114 | +df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv') |
| 115 | + |
| 116 | +fig = go.Figure(go.Scatter( |
| 117 | + x = df['Date'], |
| 118 | + y = df['AAPL.High'], |
| 119 | +)) |
| 120 | + |
| 121 | +fig.update_layout( |
| 122 | + title = 'Time Series with Custom Date-Time Format', |
| 123 | + xaxis_tickformat = '%d %B (%a)<br>%Y' |
| 124 | +) |
| 125 | + |
| 126 | +fig.show() |
| 127 | +``` |
| 128 | + |
| 129 | +#### Using Exponentformat Attribute |
| 130 | + |
| 131 | +```python |
| 132 | +import plotly.graph_objects as go |
| 133 | + |
| 134 | +fig = go.Figure(go.Scatter( |
| 135 | + x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], |
| 136 | + y = [68000, 52000, 60000, 20000, 95000, 40000, 60000, 79000, 74000, 42000, 20000, 90000] |
| 137 | +)) |
| 138 | + |
| 139 | +fig.update_layout( |
| 140 | + yaxis = dict( |
| 141 | + showexponent = 'all', |
| 142 | + exponentformat = 'e' |
| 143 | + ) |
| 144 | +) |
| 145 | + |
| 146 | +fig.show() |
| 147 | +``` |
| 148 | + |
| 149 | +#### Tickformatstops to customize for different zoom levels |
| 150 | + |
| 151 | +```python |
| 152 | +import plotly.graph_objects as go |
| 153 | + |
| 154 | +import pandas as pd |
| 155 | + |
| 156 | +df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv') |
| 157 | + |
| 158 | +fig = go.Figure(go.Scatter( |
| 159 | + x = df['Date'], |
| 160 | + y = df['mavg'] |
| 161 | +)) |
| 162 | + |
| 163 | +fig.update_layout( |
| 164 | + xaxis_tickformatstops = [ |
| 165 | + dict(dtickrange=[None, 1000], value="%H:%M:%S.%L ms"), |
| 166 | + dict(dtickrange=[1000, 60000], value="%H:%M:%S s"), |
| 167 | + dict(dtickrange=[60000, 3600000], value="%H:%M m"), |
| 168 | + dict(dtickrange=[3600000, 86400000], value="%H:%M h"), |
| 169 | + dict(dtickrange=[86400000, 604800000], value="%e. %b d"), |
| 170 | + dict(dtickrange=[604800000, "M1"], value="%e. %b w"), |
| 171 | + dict(dtickrange=["M1", "M12"], value="%b '%y M"), |
| 172 | + dict(dtickrange=["M12", None], value="%Y Y") |
| 173 | + ] |
| 174 | +) |
| 175 | + |
| 176 | +fig.show() |
| 177 | +``` |
| 178 | + |
| 179 | +#### Reference |
| 180 | +See https://plot.ly/python/reference/#layout-xaxis for more information and chart attribute options! |
| 181 | + |
0 commit comments