Plotly In Python

Overview

The plotly Python library is an interactive, open-source plotting library that supports over 40 unique chart types covering a wide range of statistical, financial, geographic, scientific, and 3-dimensional use-cases.

Built on top of the Plotly JavaScript library (plotly.js), plotly enables Python users to create beautiful interactive web-based visualizations that can be displayed in Jupyter notebooks, saved to standalone HTML files, or served as part of pure Python-built web applications using Dash. The plotly Python library is sometimes referred to as "plotly.py" to differentiate it from the JavaScript library.


Thanks to deep integration with the orca image export utility, plotly also provides great support for non-web contexts including desktop editors (e.g. QtConsole, Spyder, PyCharm) and static document publishing (e.g. exporting notebooks to PDF with high-quality vector images).


This Getting Started guide explains how to install plotly and related optional pages.


Installation

plotly may be installed using pip...

$ pip install plotly==4.12.0
or conda.
$ conda install -c plotly plotly=4.12.0

This package contains everything you need to write figures to standalone HTML files.

Note: No internet connection, account, or payment is required to use plotly.py. Prior to version 4, this library could operate in either an "online" or "offline" mode. The documentation tended to emphasize the online mode, where graphs get published to the Chart Studio web service. In version 4, all "online" functionality was removed from the plotly package and is now available as the separate, optional, chart-studio package (See below). plotly.py version 4 is "offline" only, and does not include any functionality for uploading figures or data to cloud services.

import plotly.graph_objects as go 
fig = go.Figure(data=go.Bar(y=[2, 3, 1])) fig.write_html('first_figure.html', auto_open=True) 

Jupyter Notebook Support

For use in the classic Jupyter Notebook, install the notebook and ipywidgets packages using pip...

$ pip install "notebook>=5.3" "ipywidgets>=7.2"
or conda.
$ conda install "notebook>=5.3" "ipywidgets>=7.2"

These packages contain everything you need to run a Jupyter notebook...

$ jupyter notebook

and display plotly figures inline using the notebook renderer...

import plotly.graph_objects as go 
fig = go.Figure(data=go.Bar(y=[2, 3, 1])) 
fig.show() 


or using FigureWidget objects.

import plotly.graph_objects as go 
fig = go.FigureWidget(data=go.Bar(y=[2, 3, 1])) 
fig 

JupyterLab Support (Python 3.5+)

For use in JupyterLab, install the jupyterlab and ipywidgets packages using pip...

$ pip install jupyterlab "ipywidgets>=7.5"
or conda.
$ conda install jupyterlab "ipywidgets=7.5"

Then run the following commands to install the required JupyterLab extensions (note that this will require node to be installed):

# JupyterLab renderer support 
jupyter labextension install jupyterlab-plotly@4.12.0  
# OPTIONAL: Jupyter widgets extension 
jupyter labextension install @jupyter-widgets/jupyterlab-manager plotlywidget@4.12.0

These packages contain everything you need to run JupyterLab...

$ jupyter lab

and display plotly figures inline using the plotly_mimetype renderer...

import plotly.graph_objects as go 
fig = go.Figure(data=go.Bar(y=[2, 3, 1])) 
fig.show() 


or using FigureWidget objects (if the "OPTIONAL" step above was executed).

import plotly.graph_objects as go 
fig = go.FigureWidget(data=go.Bar(y=[2, 3, 1])) 
fig 

Static Image Export

plotly.py supports static image export, using the either the kaleido package (recommended, supported as of plotly version 4.9) or the orca command line utility (legacy as of plotly version 4.9).

Kaleido

The kaleido package has no dependencies and can be installed using pip...

$ pip install -U kaleido
or conda.
$ conda install -c plotly python-kaleido

Orca

While Kaleido is now the recommended image export approach because it is easier to install and more widely compatible, static image export can also be supported by the legacy orca command line utility and the psutil Python package.

These dependencies can both be installed using conda:

conda install -c plotly plotly-orca==1.3.1 psutil

Or, psutil can be installed using pip...

pip install psutil

and orca can be installed according to the instructions in the orca README.

Extended Geo Support

Some plotly.py features rely on fairly large geographic shape files. The county choropleth figure factory is one such example. These shape files are distributed as a separate plotly-geo package. This package can be installed using pip...

$ pip install plotly-geo==1.0.0
or conda.
$ conda install -c plotly plotly-geo=1.0.0

Chart Studio Support

The chart-studio package can be used to upload plotly figures to Plotly's Chart Studio Cloud or On-Prem services. This package can be installed using pip...

$ pip install chart-studio==1.0.0
or conda.
$ conda install -c plotly chart-studio=1.0.0

Note: This package is optional, and if it is not installed it is not possible for figures to be uploaded to the Chart Studio cloud service.

Where to next?

Once you've installed, you can use our documentation in three main ways:

  1. You jump right in to examples of how to make basic charts, statistical charts, scientific charts, financial charts, maps, and 3-dimensional charts.

  2. If you prefer to learn about the fundamentals of the library first, you can read about the structure of figures, how to create and update figures, how to display figures, how to theme figures with templates, how to export figures to various formats and about Plotly Express, the high-level API for doing all of the above.

  3. You can check out our exhaustive reference guides: the Python API reference or the Figure Reference



What About Dash?

Dash is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.

Learn about how to install Dash at https://dash.plot.ly/installation.

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 from the built-in dash_core_components package like this:

import plotly.graph_objects as go 
# or plotly.express as px 
fig = go.Figure() 
# or any Plotly Express function e.g. px.bar(...) 
# fig.add_trace( ... ) 
# fig.update_layout( ... ) 

import dash 
import dash_core_components as dcc 
import dash_html_components as html 
app = dash.Dash() 
app.layout = html.Div([ 
    dcc.Graph(figure=fig) 
    ]) 
    
app.run_server(debug=True, use_reloader=False) 
# Turn off reloader if inside Jupyter

Recent Posts

See All

Tuning using HyperOpt in python

HyperOpt provides an optimization interface that accepts an evaluation function and parameter space, and can calculate the loss function value of a point in the parameter space. The user also specifie