Plotly
Plotly is an interactive, open-source data visualization library for Python, built on top of the Plotly JavaScript library (plotly.js). It supports over 40 unique chart types, enabling users to create rich, interactive web-based visualizations that can be displayed in Jupyter notebooks, saved as standalone HTML files, or integrated into pure Python web applications using Dash. Currently at version 6.6.0, Plotly maintains an active development and release cadence.
Common errors
-
ModuleNotFoundError: No module named 'plotly'
cause The Plotly library is not installed in the current Python environment where the code is being executed.fixInstall Plotly using pip: `pip install plotly` -
AttributeError: 'Figure' object has no attribute 'show'
cause This error typically occurs when using an older version of Plotly (prior to v4.0) where the `.show()` method for displaying figures was not implemented or if the Figure object itself is `None` due to a previous error.fixUpgrade Plotly to the latest version using pip: `pip install --upgrade plotly`. Also ensure the `Figure` object is correctly instantiated and not `None`. -
NameError: name 'px' is not defined
cause The `plotly.express` module, commonly aliased as `px`, has not been imported before being used in the code.fixAdd the import statement `import plotly.express as px` at the beginning of your script or notebook cell. -
AttributeError: module 'plotly.graph_objects' has no attribute 'Figure'
cause This error occurs due to incorrect capitalization. The class name for creating a figure from `plotly.graph_objects` is `Figure` (with a capital 'F'), not `figure` (lowercase 'f').fixChange `go.figure()` to `go.Figure()` when instantiating a graph object figure. -
Plotly charts not showing in Jupyter Notebook/Lab (empty space)
cause This can be caused by various factors, including an unresponsive kernel, outdated Plotly or Jupyter packages, browser compatibility issues, missing or incorrectly installed JupyterLab extensions (`jupyterlab-plotly` and `plotlywidget`), or an outdated `plotly.js` rendering engine.fixEnsure `plotly` and `jupyterlab` are up-to-date (`pip install --upgrade plotly jupyterlab`). Install JupyterLab extensions (`jupyter labextension install jupyterlab-plotly @jupyter-widgets/jupyterlab-manager`) and restart Jupyter Lab. For some versions, downgrading JupyterLab might be a temporary workaround.
Warnings
- breaking Plotly.py 6.0 dropped support for Jupyter Notebook versions prior to 7.0. Users on older Jupyter versions will need to upgrade.
- breaking Plotly.py 6.0 introduced a rewrite of dataframe performance using the Narwhals abstraction layer instead of solely relying on the Pandas API. While generally providing performance gains, this might subtly affect direct Pandas dataframe interactions for users upgrading from older versions.
- breaking Plotly.js 3.0 (which plotly.py 6.0 is built upon) removed support for several deprecated attributes (e.g., `titlefont`, `titleposition`, `titleside`, `titleoffset`) and specific trace types like `pointcloud` and `heatmapgl`.
- gotcha For static image export of Plotly figures (e.g., to PNG, JPEG, SVG, PDF), the `kaleido` package is the recommended engine since Plotly 4.9. The older `orca` command-line utility is now considered legacy and may have compatibility issues.
- gotcha When creating figures, `plotly.express` automatically infers sensible defaults and is much more concise (5-100 times less code) than building figures directly with `plotly.graph_objects`. Using `go` requires explicit definition of traces and layout.
Install
-
pip install plotly -
pip install 'plotly[kaleido]' # For static image export pip install plotly-geo # For extended geographic features
Imports
- plotly.express
import plotly.express as px
- plotly.graph_objects
import plotly.graph_objects as go
- plotly.io
import plotly.io as pio
Quickstart
import plotly.express as px # Create a simple bar chart fig = px.bar(x=["A", "B", "C"], y=[1, 3, 2], title="My First Plotly Chart") # Display the figure (opens in browser/notebook) fig.show()