|
| 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.6 |
| 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.7.3 |
| 24 | +plotly: |
| 25 | +description:Displaying plotly figures from Python |
| 26 | +v4upgrade:true |
| 27 | +--- |
| 28 | + |
| 29 | +#Displaying plotly figures |
| 30 | +This section covers the many ways to display plotly figures from Python. At the highest level, there are three general approaches: |
| 31 | + |
| 32 | +1. Using the renderers framework |
| 33 | +2. Using Dash in a web app context |
| 34 | +3. Using a`FigureWidget` in an ipywidgets context |
| 35 | + |
| 36 | +Each of these approaches is discussed below. |
| 37 | + |
| 38 | +#Displaying figures using the renderers framework |
| 39 | + |
| 40 | +The renderers framework is a flexible approach for displaying plotly figures in a variety of contexts. To display a figure using the renderers framework, you call the`.show` method on a graph object figure, or pass the figure to the`plotly.io.show` function. With either approach, plotly.py will display the figure using the current default renderer(s). |
| 41 | + |
| 42 | +```python |
| 43 | +import plotly.graph_objectsas go |
| 44 | +fig= go.Figure( |
| 45 | +data=[go.Bar(y=[2,1,3])], |
| 46 | +layout_title_text="A Figure Displayed with fig.show()" |
| 47 | +) |
| 48 | +fig.show() |
| 49 | +``` |
| 50 | + |
| 51 | +In most situations, you can omit the call to`.show()` and allow the figure to display itself. |
| 52 | + |
| 53 | +```python |
| 54 | +import plotly.graph_objectsas go |
| 55 | +fig= go.Figure( |
| 56 | +data=[go.Bar(y=[2,1,3])], |
| 57 | +layout_title_text="A Figure Displaying Itself" |
| 58 | +) |
| 59 | +fig |
| 60 | +``` |
| 61 | + |
| 62 | +>To be precise, figures will display themselves using the current default renderer when the two following conditions are true. First, the last expression in a cell must evaluates to a figure. Second, plotly.py must be running from within an IPython kernel. |
| 63 | +
|
| 64 | +In many contexts, an appropriate renderer will be chosen automatically and you will not need to perform any additional configuration. These contexts include the classic Jupyter Notebook, JupyterLab (provided the`jupyterlab-plotly` JupyterLab extension is installed), Visual Studio Code notebooks, Colab, Kaggle notebooks, Azure notebooks, and the Python interactive shell. Additional contexts are supported by choosing a compatible renderer including the IPython console, QtConsole, Spyder, and more. |
| 65 | + |
| 66 | +Next, we will show how to configure the default renderer. After that, we will describe all of the built-in renderers and discuss why you might choose to use each one. |
| 67 | + |
| 68 | +>Note: The renderers framework is a generalization of the`plotly.offline.iplot` and`plotly.offline.plot` functions that were the recommended way to display figures prior to plotly.py version 4. These functions are still supported for backward compatibility, but they will not be discussed here. |
| 69 | +
|
| 70 | +##Setting the default renderer |
| 71 | +The current and available renderers are configured using the`plotly.io.renderers` configuration object. Display this object to see the current default renderer and the list of all available renderers. |
| 72 | + |
| 73 | +```python |
| 74 | +import plotly.ioas pio |
| 75 | +pio.renderers |
| 76 | +``` |
| 77 | + |
| 78 | +The default renderer that you see when you display`pio.renderers` might be different than what is shown here. This is because plotly.py attempts to autodetect an appropriate renderer at startup. You can change the default renderer by assigning the name of an available renderer to the`pio.renderers.default` property. For example, to switch to the`'browser'` renderer, which opens figures in a tab of the default web browser, you would run the following. |
| 79 | + |
| 80 | +```python |
| 81 | +import plotly.ioas pio |
| 82 | +pio.renderers.default="browser" |
| 83 | +``` |
| 84 | + |
| 85 | +It is also possible to set the default renderer using a system environment variable. At startup, plotly.py checks for the existence of an environment variable named`PLOTLY_RENDERER`. If this environment variable is set to the name of an available renderer, this renderer is set as the default. |
| 86 | + |
| 87 | +##Overriding the default renderer |
| 88 | +It is also possible to override the default renderer temporarily by passing the name of an available renderer as the`renderer` keyword argument to the`.show` method. Here is an example of displaying a figure using the`svg` renderer (described below) without changing the default renderer. |
| 89 | + |
| 90 | +```python |
| 91 | +import plotly.graph_objectsas go |
| 92 | +fig= go.Figure( |
| 93 | +data=[go.Bar(y=[2,1,3])], |
| 94 | +layout_title_text="A Figure Displayed with the 'svg' Renderer" |
| 95 | +) |
| 96 | +fig.show(renderer="svg") |
| 97 | +``` |
| 98 | + |
| 99 | +##The built-in renderers |
| 100 | +In this section, we will describe the built-in renderers so that you can choose the one(s) that best suite your needs. |
| 101 | + |
| 102 | +###Interactive renderers |
| 103 | +Interactive renderers display figures using the Plotly.js JavaScript library and are fully interactive, supporting pan, zoom, hover tooltips, etc. |
| 104 | + |
| 105 | +####`notebook` |
| 106 | +This renderer is intended for use in the classic[Jupyter Notebook](https://jupyter.org/install.html) (not JupyterLab). The full Plotly.js JavaScript library bundle is added to the notebook the first time a figure is rendered, so this renderer will work without an internet connection. |
| 107 | + |
| 108 | +This renderer is a good choice for notebooks that will be exported to HTML files (Either using[nbconvert](https://nbconvert.readthedocs.io/en/latest/) or the "Download as HTML" menu action) because the exported HTML files will work without an internet connection. |
| 109 | + |
| 110 | +>Note: Adding the Plotly.js bundle to the notebook does add a few megabytes to the notebook size, so if you can count on having an internet connection you may want to consider the`notebook_connected` renderer. |
| 111 | +
|
| 112 | +####`notebook_connected` |
| 113 | +This renderer is the same as`notebook`, except the Plotly.js JavaScript library bundle is loaded from an online CDN location. This saves a few megabytes in notebook size, but an internet connection is required in order to display figures that are rendered this way. |
| 114 | + |
| 115 | +This renderer is a good choice for notebooks that will be shared with[nbviewer](https://nbviewer.jupyter.org/) since users must have an active internet connection to access nbviewer in the first place. |
| 116 | + |
| 117 | +####`kaggle` and`azure` |
| 118 | +These are aliases for`notebook_connected` because this renderer is a good choice for use with[Kaggle kernels](https://www.kaggle.com/docs/kernels) and[Azure Notebooks](https://notebooks.azure.com/). |
| 119 | + |
| 120 | +####`colab` |
| 121 | +This is a custom renderer for use with[Google Colab](https://colab.research.google.com). |
| 122 | + |
| 123 | +####`browser` |
| 124 | +This renderer will open a figure in a browser tab using the default web browser. This renderer can only be used when the Python kernel is running locally on the same machine as the web browser, so it is not compatible with Jupyter Hub or online notebook services. |
| 125 | + |
| 126 | +>Implementation Note 1: The "default browser" is the browser that is chosen by the Python[`webbrowser`](https://docs.python.org/2/library/webbrowser.html) module. |
| 127 | +
|
| 128 | +>Implementation Note 2: The`browser` renderer works by setting up a single use local webserver on a local port. Since the webserver is shut down as soon as the figure is served to the browser, the figure will not be restored if the browser is refreshed. |
| 129 | +
|
| 130 | +####`firefox`,`chrome`, and`chromium` |
| 131 | +These renderers are the same as the`browser` renderer, but they force the use of a particular browser. |
| 132 | + |
| 133 | +####`iframe` and`iframe_connected` |
| 134 | +These renderers write figures out as standalone HTML files and then display[`iframe`](https://www.w3schools.com/html/html_iframe.asp) elements that reference these HTML files. The`iframe` renderer will include the Plotly.js JavaScript bundle in each HTML file that is written, while the`iframe_connected` renderer includes only a reference to an online CDN location from which to load Plotly.js. Consequently, the`iframe_connected` renderer outputs files that are smaller than the`iframe` renderer, but it requires an internet connection while the`iframe` renderer can operate offline. |
| 135 | + |
| 136 | +This renderer may be useful when working with notebooks than contain lots of large figures. When using the`notebook` or`notebook_connected` renderer, all of the data for all of the figures in a notebook are stored inline in the notebook itself. If this would result in a prohibitively large notebook size, an`iframe` or`iframe_connected` renderer could be used instead. With the iframe renderers, the figure data are stored in the individual HTML files rather than in the notebook itself, resulting in a smaller notebook size. |
| 137 | + |
| 138 | +>Implementation Note: The HTML files written by the iframe renderers are stored in a subdirectory named`iframe_figures`. The HTML files are given names based on the execution number of the notebook cell that produced the figure. This means that each time a notebook kernel is restarted, any prior HTML files will be overwritten. This also means that you should not store multiple notebooks using an iframe renderer in the same directory, because this could result in figures from one notebook overwriting figures from another notebook. |
| 139 | +
|
| 140 | + |
| 141 | +####`plotly_mimetype` |
| 142 | + |
| 143 | +The`plotly_mimetype` renderer creates a specification of the plotly figure (called a MIME-type bundle), and requests that the current user interface display it. User interfaces that support this renderer include[JupyterLab](https://jupyterlab.readthedocs.io/en/stable/) (requires the[`jupyterlab-plotly`](https://www.npmjs.com/package/jupyterlab-plotly) extension),[nteract](https://nteract.io/), and the Visual Studio Code[notebook interface](https://code.visualstudio.com/docs/python/jupyter-support). |
| 144 | + |
| 145 | +####`jupyterlab`,`nteract`, and`vscode` |
| 146 | +These are aliases for`plotly_mimetype` since this renderer is a good choice when working in JupyterLab, nteract, and the Visual Studio Code notebook interface. |
| 147 | + |
| 148 | +###Static image renderers |
| 149 | +A set of renderers is provided for displaying figures as static images. These renderers all rely on the orca static image export utility. See the[Static Image Export](https://plot.ly/python/static-image-export/) page for more information on getting set up with orca. |
| 150 | + |
| 151 | +####`png`,`jpeg`, and`svg` |
| 152 | +These renderers display figures as static PNG, JPEG, and SVG images respectively. These renderers are useful for user interfaces that do not support inline HTML output, but do support inline static images. Examples include the[QtConsole](https://qtconsole.readthedocs.io/en/stable/),[Spyder](https://www.spyder-ide.org/), and the PyCharm[notebook interface](https://www.jetbrains.com/help/pycharm/jupyter-notebook-support.html). |
| 153 | + |
| 154 | +```python |
| 155 | +import plotly.graph_objectsas go |
| 156 | +fig= go.Figure( |
| 157 | +data=[go.Bar(y=[2,1,3])], |
| 158 | +layout_title_text="A Figure Displayed with the 'png' Renderer" |
| 159 | +) |
| 160 | +fig.show(renderer="png") |
| 161 | +``` |
| 162 | + |
| 163 | +####`pdf` |
| 164 | +This renderer displays figures as static PDF files. This is especially useful for notebooks that will be exported to PDF files using the LaTeX export capabilities of nbconvert. |
| 165 | + |
| 166 | +###Other renderers |
| 167 | +Other miscellaneous renderers |
| 168 | + |
| 169 | +####`json` |
| 170 | +In editors that support it (JupyterLab, nteract, and the Visual Studio Code notebook interface), this renderer displays the JSON representation of a figure in a collapsible interactive tree structure. This can be very useful for examining the structure of complex figures |
| 171 | + |
| 172 | +###Multiple renderers |
| 173 | +You can specify that multiple renderers should be used by joining their names on`"+"` characters. This is useful when writing code that needs to support multiple contexts. For example, if a notebook specifies a default renderer string of`"notebook+plotly_mimetype+pdf"`then this notebook would be able to run in the classic Jupyter Notebook, in JupyterLab, and it would support being exported to PDF using nbconvert. |
| 174 | + |
| 175 | +##Customizing built-in renderers |
| 176 | +Most built-in renderers have configuration options to customize their behavior. To view a description of a renderer, including its configuration options, access the renderer object using dictionary-style key lookup on the`plotly.io.renderers` configuration object and then display it. Here is an example of accessing and displaying the`png` renderer. |
| 177 | + |
| 178 | +```python |
| 179 | +import plotly.ioas pio |
| 180 | +png_renderer= pio.renderers["png"] |
| 181 | +png_renderer |
| 182 | +``` |
| 183 | + |
| 184 | +From this output, you can see that the`png` renderer supports 3 properties:`width`,`height`, and`scale`. You can customize these properties by assigning new values to them. |
| 185 | + |
| 186 | +Here is an example that customizes the`png` renderer to change the resulting image size, sets the`png` renderer as the default, and then displays a figure. |
| 187 | + |
| 188 | +```python |
| 189 | +import plotly.ioas pio |
| 190 | +png_renderer= pio.renderers["png"] |
| 191 | +png_renderer.width=500 |
| 192 | +png_renderer.height=500 |
| 193 | + |
| 194 | +pio.renderers.default="png" |
| 195 | + |
| 196 | +import plotly.graph_objectsas go |
| 197 | +fig= go.Figure( |
| 198 | +data=[go.Bar(y=[2,1,3])], |
| 199 | +layout_title_text="A Figure Displayed with the 'png' Renderer" |
| 200 | +) |
| 201 | +fig.show() |
| 202 | +``` |
| 203 | + |
| 204 | +You can also override the values of renderer parameters temporarily by passing them as keyword arguments to the`.show` method. For example |
| 205 | + |
| 206 | +```python |
| 207 | +import plotly.graph_objectsas go |
| 208 | + |
| 209 | +fig= go.Figure( |
| 210 | +data=[go.Bar(y=[2,1,3])], |
| 211 | +layout_title_text="A Figure Displayed with the 'png' Renderer" |
| 212 | +) |
| 213 | +fig.show(renderer="png",width=800,height=300) |
| 214 | +``` |
| 215 | + |
| 216 | +#Displaying figures using Dash |
| 217 | +Dash is a Python framework for building web applications, and it provides built-in support for displaying Plotly figures. See the[Dash User Guide](https://dash.plot.ly/) for more information. |
| 218 | + |
| 219 | +It is important to note that Dash does not use the renderers framework discussed above, so you should not use the`.show` figure method or the`plotly.io.show` function inside Dash applications. |
| 220 | + |
| 221 | +#Displaying figures using ipywidgets |
| 222 | +Plotly figures can be displayed in[ipywidgets](https://ipywidgets.readthedocs.io/en/stable/) contexts using`plotly.graph_objects.FigureWidget` objects.`FigureWidget` is a figure graph object (Just like`plotly.graph_objects.Figure`) so you can add traces to it and update it just like a regular`Figure`. But`FigureWidget` is also an ipywidgets object, which means that you can display it alongside other ipywidgets to build user interfaces right in the notebook. See the[Plotly FigureWidget Overview](https://plot.ly/python/figurewidget/) for more information on integrating plotly figures with ipywidgets. |
| 223 | + |
| 224 | +It is important to note that`FigureWidget` does not use the renderers framework discussed above, so you should not use the`.show` figure method or the`plotly.io.show` function on`FigureWidget` objects. |