|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "68bb6fe8", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "### Range of axes\n", |
| 9 | + "\n", |
| 10 | + "3D figures have an attribute in `layout` called `scene`, which contains\n", |
| 11 | + "attributes such as `xaxis`, `yaxis` and `zaxis` parameters, in order to\n", |
| 12 | + "set the range, title, ticks, color etc. of the axes.\n", |
| 13 | + "\n", |
| 14 | + "For creating 3D charts, see [this page](https://plotly.com/python/3d-charts/).\n", |
| 15 | + "\n", |
| 16 | + "Set `range` on an axis to manually configure a range for that axis. If you don't set `range`, it's automatically calculated. In this example, we set a `range` on `xaxis`, `yaxis`, and `zaxis`." |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "cell_type": "code", |
| 21 | + "execution_count": null, |
| 22 | + "id": "b6f2d4dc", |
| 23 | + "metadata": {}, |
| 24 | + "outputs": [], |
| 25 | + "source": [ |
| 26 | + "import plotly.graph_objects as go\n", |
| 27 | + "import numpy as np\n", |
| 28 | + "np.random.seed(1)\n", |
| 29 | + "\n", |
| 30 | + "N = 70\n", |
| 31 | + "\n", |
| 32 | + "fig = go.Figure(data=[go.Mesh3d(x=(70*np.random.randn(N)),\n", |
| 33 | + " y=(55*np.random.randn(N)),\n", |
| 34 | + " z=(40*np.random.randn(N)),\n", |
| 35 | + " opacity=0.5,\n", |
| 36 | + " color='rgba(244,22,100,0.6)'\n", |
| 37 | + " )])\n", |
| 38 | + "\n", |
| 39 | + "fig.update_layout(\n", |
| 40 | + " scene = dict(\n", |
| 41 | + " xaxis = dict(nticks=4, range=[-100,100],),\n", |
| 42 | + " yaxis = dict(nticks=4, range=[-50,100],),\n", |
| 43 | + " zaxis = dict(nticks=4, range=[-100,100],),),\n", |
| 44 | + " width=700,\n", |
| 45 | + " margin=dict(r=20, l=10, b=10, t=10))\n", |
| 46 | + "\n", |
| 47 | + "fig.show()" |
| 48 | + ] |
| 49 | + }, |
| 50 | + { |
| 51 | + "cell_type": "markdown", |
| 52 | + "id": "c9e11629", |
| 53 | + "metadata": {}, |
| 54 | + "source": [ |
| 55 | + "### Setting only a Lower or Upper Bound for Range\n", |
| 56 | + "\n", |
| 57 | + "*New in 5.17*\n", |
| 58 | + "\n", |
| 59 | + "You can also set just a lower or upper bound for `range`. In this case, autorange is used for the other bound. In this example, we apply autorange to the lower bound of the `yaxis` and the upper bound of `zaxis` by setting them to `None`." |
| 60 | + ] |
| 61 | + }, |
| 62 | + { |
| 63 | + "cell_type": "code", |
| 64 | + "execution_count": null, |
| 65 | + "id": "1d6c29ba", |
| 66 | + "metadata": {}, |
| 67 | + "outputs": [], |
| 68 | + "source": [ |
| 69 | + "import plotly.graph_objects as go\n", |
| 70 | + "import numpy as np\n", |
| 71 | + "np.random.seed(1)\n", |
| 72 | + "\n", |
| 73 | + "N = 70\n", |
| 74 | + "\n", |
| 75 | + "fig = go.Figure(data=[go.Mesh3d(x=(70*np.random.randn(N)),\n", |
| 76 | + " y=(55*np.random.randn(N)),\n", |
| 77 | + " z=(40*np.random.randn(N)),\n", |
| 78 | + " opacity=0.5,\n", |
| 79 | + " color='rgba(244,22,100,0.6)'\n", |
| 80 | + " )])\n", |
| 81 | + "\n", |
| 82 | + "fig.update_layout(\n", |
| 83 | + " scene = dict(\n", |
| 84 | + " xaxis = dict(nticks=4, range=[-100,100],),\n", |
| 85 | + " yaxis = dict(nticks=4, range=[None, 100],),\n", |
| 86 | + " zaxis = dict(nticks=4, range=[-100, None],),),\n", |
| 87 | + " width=700,\n", |
| 88 | + " margin=dict(r=20, l=10, b=10, t=10))\n", |
| 89 | + "\n", |
| 90 | + "fig.show()" |
| 91 | + ] |
| 92 | + }, |
| 93 | + { |
| 94 | + "cell_type": "markdown", |
| 95 | + "id": "fd61d49e", |
| 96 | + "metadata": {}, |
| 97 | + "source": [ |
| 98 | + "### Fixed Ratio Axes" |
| 99 | + ] |
| 100 | + }, |
| 101 | + { |
| 102 | + "cell_type": "code", |
| 103 | + "execution_count": null, |
| 104 | + "id": "4478ccb7", |
| 105 | + "metadata": {}, |
| 106 | + "outputs": [], |
| 107 | + "source": [ |
| 108 | + "import plotly.graph_objects as go\n", |
| 109 | + "from plotly.subplots import make_subplots\n", |
| 110 | + "import numpy as np\n", |
| 111 | + "\n", |
| 112 | + "N = 50\n", |
| 113 | + "\n", |
| 114 | + "fig = make_subplots(rows=2, cols=2,\n", |
| 115 | + " specs=[[{'is_3d': True}, {'is_3d': True}],\n", |
| 116 | + " [{'is_3d': True}, {'is_3d': True}]],\n", |
| 117 | + " print_grid=False)\n", |
| 118 | + "for i in [1,2]:\n", |
| 119 | + " for j in [1,2]:\n", |
| 120 | + " fig.add_trace(\n", |
| 121 | + " go.Mesh3d(\n", |
| 122 | + " x=(60*np.random.randn(N)),\n", |
| 123 | + " y=(25*np.random.randn(N)),\n", |
| 124 | + " z=(40*np.random.randn(N)),\n", |
| 125 | + " opacity=0.5,\n", |
| 126 | + " ),\n", |
| 127 | + " row=i, col=j)\n", |
| 128 | + "\n", |
| 129 | + "fig.update_layout(width=700, margin=dict(r=10, l=10, b=10, t=10))\n", |
| 130 | + "# fix the ratio in the top left subplot to be a cube\n", |
| 131 | + "fig.update_layout(scene_aspectmode='cube')\n", |
| 132 | + "# manually force the z-axis to appear twice as big as the other two\n", |
| 133 | + "fig.update_layout(scene2_aspectmode='manual',\n", |
| 134 | + " scene2_aspectratio=dict(x=1, y=1, z=2))\n", |
| 135 | + "# draw axes in proportion to the proportion of their ranges\n", |
| 136 | + "fig.update_layout(scene3_aspectmode='data')\n", |
| 137 | + "# automatically produce something that is well proportioned using 'data' as the default\n", |
| 138 | + "fig.update_layout(scene4_aspectmode='auto')\n", |
| 139 | + "fig.show()" |
| 140 | + ] |
| 141 | + }, |
| 142 | + { |
| 143 | + "cell_type": "markdown", |
| 144 | + "id": "38f8ca0d", |
| 145 | + "metadata": {}, |
| 146 | + "source": [ |
| 147 | + "### Set Axes Title" |
| 148 | + ] |
| 149 | + }, |
| 150 | + { |
| 151 | + "cell_type": "code", |
| 152 | + "execution_count": null, |
| 153 | + "id": "a59f6639", |
| 154 | + "metadata": {}, |
| 155 | + "outputs": [], |
| 156 | + "source": [ |
| 157 | + "import plotly.graph_objects as go\n", |
| 158 | + "import numpy as np\n", |
| 159 | + "\n", |
| 160 | + "# Define random surface\n", |
| 161 | + "N = 50\n", |
| 162 | + "fig = go.Figure()\n", |
| 163 | + "fig.add_trace(go.Mesh3d(x=(60*np.random.randn(N)),\n", |
| 164 | + " y=(25*np.random.randn(N)),\n", |
| 165 | + " z=(40*np.random.randn(N)),\n", |
| 166 | + " opacity=0.5,\n", |
| 167 | + " color='yellow'\n", |
| 168 | + " ))\n", |
| 169 | + "fig.add_trace(go.Mesh3d(x=(70*np.random.randn(N)),\n", |
| 170 | + " y=(55*np.random.randn(N)),\n", |
| 171 | + " z=(30*np.random.randn(N)),\n", |
| 172 | + " opacity=0.5,\n", |
| 173 | + " color='pink'\n", |
| 174 | + " ))\n", |
| 175 | + "\n", |
| 176 | + "fig.update_layout(scene = dict(\n", |
| 177 | + " xaxis=dict(\n", |
| 178 | + " title=dict(\n", |
| 179 | + " text='X AXIS TITLE'\n", |
| 180 | + " )\n", |
| 181 | + " ),\n", |
| 182 | + " yaxis=dict(\n", |
| 183 | + " title=dict(\n", |
| 184 | + " text='Y AXIS TITLE'\n", |
| 185 | + " )\n", |
| 186 | + " ),\n", |
| 187 | + " zaxis=dict(\n", |
| 188 | + " title=dict(\n", |
| 189 | + " text='Z AXIS TITLE'\n", |
| 190 | + " )\n", |
| 191 | + " ),\n", |
| 192 | + " ),\n", |
| 193 | + " width=700,\n", |
| 194 | + " margin=dict(r=20, b=10, l=10, t=10))\n", |
| 195 | + "\n", |
| 196 | + "fig.show()" |
| 197 | + ] |
| 198 | + }, |
| 199 | + { |
| 200 | + "cell_type": "markdown", |
| 201 | + "id": "315c4538", |
| 202 | + "metadata": {}, |
| 203 | + "source": [ |
| 204 | + "### Ticks Formatting" |
| 205 | + ] |
| 206 | + }, |
| 207 | + { |
| 208 | + "cell_type": "code", |
| 209 | + "execution_count": null, |
| 210 | + "id": "421cba75", |
| 211 | + "metadata": {}, |
| 212 | + "outputs": [], |
| 213 | + "source": [ |
| 214 | + "import plotly.graph_objects as go\n", |
| 215 | + "import numpy as np\n", |
| 216 | + "\n", |
| 217 | + "# Define random surface\n", |
| 218 | + "N = 50\n", |
| 219 | + "fig = go.Figure(data=[go.Mesh3d(x=(60*np.random.randn(N)),\n", |
| 220 | + " y=(25*np.random.randn(N)),\n", |
| 221 | + " z=(40*np.random.randn(N)),\n", |
| 222 | + " opacity=0.5,\n", |
| 223 | + " color='rgba(100,22,200,0.5)'\n", |
| 224 | + " )])\n", |
| 225 | + "\n", |
| 226 | + "# Different types of customized ticks\n", |
| 227 | + "fig.update_layout(scene = dict(\n", |
| 228 | + " xaxis = dict(\n", |
| 229 | + " ticktext= ['TICKS','MESH','PLOTLY','PYTHON'],\n", |
| 230 | + " tickvals= [0,50,75,-50]),\n", |
| 231 | + " yaxis = dict(\n", |
| 232 | + " nticks=5, tickfont=dict(\n", |
| 233 | + " color='green',\n", |
| 234 | + " size=12,\n", |
| 235 | + " family='Old Standard TT, serif',),\n", |
| 236 | + " ticksuffix='#'),\n", |
| 237 | + " zaxis = dict(\n", |
| 238 | + " nticks=4, ticks='outside',\n", |
| 239 | + " tick0=0, tickwidth=4),),\n", |
| 240 | + " width=700,\n", |
| 241 | + " margin=dict(r=10, l=10, b=10, t=10)\n", |
| 242 | + " )\n", |
| 243 | + "\n", |
| 244 | + "fig.show()" |
| 245 | + ] |
| 246 | + }, |
| 247 | + { |
| 248 | + "cell_type": "markdown", |
| 249 | + "id": "88e41734", |
| 250 | + "metadata": {}, |
| 251 | + "source": [ |
| 252 | + "### Background and Grid Color" |
| 253 | + ] |
| 254 | + }, |
| 255 | + { |
| 256 | + "cell_type": "code", |
| 257 | + "execution_count": null, |
| 258 | + "id": "5fbfaadb", |
| 259 | + "metadata": {}, |
| 260 | + "outputs": [], |
| 261 | + "source": [ |
| 262 | + "import plotly.graph_objects as go\n", |
| 263 | + "import numpy as np\n", |
| 264 | + "\n", |
| 265 | + "N = 50\n", |
| 266 | + "fig = go.Figure(data=[go.Mesh3d(x=(30*np.random.randn(N)),\n", |
| 267 | + " y=(25*np.random.randn(N)),\n", |
| 268 | + " z=(30*np.random.randn(N)),\n", |
| 269 | + " opacity=0.5,)])\n", |
| 270 | + "\n", |
| 271 | + "\n", |
| 272 | + "# xaxis.backgroundcolor is used to set background color\n", |
| 273 | + "fig.update_layout(scene = dict(\n", |
| 274 | + " xaxis = dict(\n", |
| 275 | + " backgroundcolor=\"rgb(200, 200, 230)\",\n", |
| 276 | + " gridcolor=\"white\",\n", |
| 277 | + " showbackground=True,\n", |
| 278 | + " zerolinecolor=\"white\",),\n", |
| 279 | + " yaxis = dict(\n", |
| 280 | + " backgroundcolor=\"rgb(230, 200,230)\",\n", |
| 281 | + " gridcolor=\"white\",\n", |
| 282 | + " showbackground=True,\n", |
| 283 | + " zerolinecolor=\"white\"),\n", |
| 284 | + " zaxis = dict(\n", |
| 285 | + " backgroundcolor=\"rgb(230, 230,200)\",\n", |
| 286 | + " gridcolor=\"white\",\n", |
| 287 | + " showbackground=True,\n", |
| 288 | + " zerolinecolor=\"white\",),),\n", |
| 289 | + " width=700,\n", |
| 290 | + " margin=dict(\n", |
| 291 | + " r=10, l=10,\n", |
| 292 | + " b=10, t=10)\n", |
| 293 | + " )\n", |
| 294 | + "fig.show()" |
| 295 | + ] |
| 296 | + }, |
| 297 | + { |
| 298 | + "cell_type": "markdown", |
| 299 | + "id": "29a75416", |
| 300 | + "metadata": {}, |
| 301 | + "source": [ |
| 302 | + "### Disabling tooltip spikes\n", |
| 303 | + "\n", |
| 304 | + "By default, guidelines originating from the tooltip point are drawn. It is possible to disable this behaviour with the `showspikes` parameter. In this example we only keep the `z` spikes (projection of the tooltip on the `x-y` plane). Hover on the data to show this behaviour." |
| 305 | + ] |
| 306 | + }, |
| 307 | + { |
| 308 | + "cell_type": "code", |
| 309 | + "execution_count": null, |
| 310 | + "id": "cab2807f", |
| 311 | + "metadata": { |
| 312 | + "lines_to_next_cell": 2 |
| 313 | + }, |
| 314 | + "outputs": [], |
| 315 | + "source": [ |
| 316 | + "import plotly.graph_objects as go\n", |
| 317 | + "import numpy as np\n", |
| 318 | + "\n", |
| 319 | + "N = 50\n", |
| 320 | + "fig = go.Figure(data=[go.Mesh3d(x=(30*np.random.randn(N)),\n", |
| 321 | + " y=(25*np.random.randn(N)),\n", |
| 322 | + " z=(30*np.random.randn(N)),\n", |
| 323 | + " opacity=0.5,)])\n", |
| 324 | + "fig.update_layout(scene=dict(xaxis_showspikes=False,\n", |
| 325 | + " yaxis_showspikes=False))\n", |
| 326 | + "fig.show()" |
| 327 | + ] |
| 328 | + }, |
| 329 | + { |
| 330 | + "cell_type": "markdown", |
| 331 | + "id": "9bb6ce1d", |
| 332 | + "metadata": {}, |
| 333 | + "source": [ |
| 334 | + "### What About Dash?\n", |
| 335 | + "\n", |
| 336 | + "[Dash](https://dash.plot.ly/) is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.\n", |
| 337 | + "\n", |
| 338 | + "Learn about how to install Dash at https://dash.plot.ly/installation.\n", |
| 339 | + "\n", |
| 340 | + "Everywhere in this page that you see `fig.show()`, you can display the same figure in a Dash application by passing it to the `figure` argument of the [`Graph` component](https://dash.plot.ly/dash-core-components/graph) from the built-in `dash_core_components` package like this:\n", |
| 341 | + "\n", |
| 342 | + "```python\n", |
| 343 | + "import plotly.graph_objects as go # or plotly.express as px\n", |
| 344 | + "fig = go.Figure() # or any Plotly Express function e.g. px.bar(...)\n", |
| 345 | + "# fig.add_trace( ... )\n", |
| 346 | + "# fig.update_layout( ... )\n", |
| 347 | + "\n", |
| 348 | + "from dash import Dash, dcc, html\n", |
| 349 | + "\n", |
| 350 | + "app = Dash()\n", |
| 351 | + "app.layout = html.Div([\n", |
| 352 | + " dcc.Graph(figure=fig)\n", |
| 353 | + "])\n", |
| 354 | + "\n", |
| 355 | + "app.run_server(debug=True, use_reloader=False) # Turn off reloader if inside Jupyter\n", |
| 356 | + "```" |
| 357 | + ] |
| 358 | + } |
| 359 | + ], |
| 360 | + "metadata": { |
| 361 | + "jupytext": { |
| 362 | + "notebook_metadata_filter": "all" |
| 363 | + }, |
| 364 | + "kernelspec": { |
| 365 | + "display_name": "Python 3 (ipykernel)", |
| 366 | + "language": "python", |
| 367 | + "name": "python3" |
| 368 | + }, |
| 369 | + "language_info": { |
| 370 | + "codemirror_mode": { |
| 371 | + "name": "ipython", |
| 372 | + "version": 3 |
| 373 | + }, |
| 374 | + "file_extension": ".py", |
| 375 | + "mimetype": "text/x-python", |
| 376 | + "name": "python", |
| 377 | + "nbconvert_exporter": "python", |
| 378 | + "pygments_lexer": "ipython3", |
| 379 | + "version": "3.10.4" |
| 380 | + }, |
| 381 | + "plotly": { |
| 382 | + "description": "How to format axes of 3d plots in Python with Plotly.", |
| 383 | + "display_as": "3d_charts", |
| 384 | + "language": "python", |
| 385 | + "layout": "base", |
| 386 | + "name": "3D Axes", |
| 387 | + "order": 1, |
| 388 | + "page_type": "example_index", |
| 389 | + "permalink": "python/3d-axes/", |
| 390 | + "thumbnail": "thumbnail/3d-axes.png" |
| 391 | + } |
| 392 | + }, |
| 393 | + "nbformat": 4, |
| 394 | + "nbformat_minor": 5 |
| 395 | +} |
0 commit comments