What it is
Plotly is a Python graphing library that makes interactive, publication-quality graphs online. It supports a wide variety of chart types, including line plots, scatter plots, bar charts, 3D plots, maps, and dashboards.
Plotly allows the creation of interactive visualizations with Python. You can generate figures using Plotly Express for simple charts or use the lower-level Graph Objects API for more complex and customized visualizations.
Installation
pip install plotlyGetting started
The smallest useful thing you can do with it, and what each part means.
import plotly.express as px
import pandas as pd
df = pd.DataFrame({'x':[1,2,3,4], 'y':[10,15,13,17]})
fig = px.line(df, x='x', y='y', title='Line Plot')
fig.show()import plotly.express as px
df = px.data.iris()
fig = px.scatter(df, x='sepal_width', y='sepal_length', color='species')
fig.show()Advanced usage
Where the library earns its place over a simpler alternative.
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scatter(x=[1,2,3], y=[4,5,6], mode='lines+markers', name='Line 1'))
fig.update_layout(title='Custom Layout', xaxis_title='X Axis', yaxis_title='Y Axis')
fig.show()import plotly.graph_objects as go
import numpy as np
x = np.linspace(-5,5,50)
y = np.linspace(-5,5,50)
x, y = np.meshgrid(x, y)
z = np.sin(np.sqrt(x**2 + y**2))
fig = go.Figure(data=[go.Surface(z=z, x=x, y=y)])
fig.show()from plotly.subplots import make_subplots
import plotly.graph_objects as go
fig = make_subplots(rows=1, cols=2)
fig.add_trace(go.Bar(x=[1,2,3], y=[2,5,3]), row=1, col=1)
fig.add_trace(go.Scatter(x=[1,2,3], y=[5,3,6]), row=1, col=2)
fig.show()import plotly.express as px
df = px.data.gapminder()
fig = px.scatter(df.query('year==2007'), x='gdpPercap', y='lifeExp', size='pop', color='continent', hover_name='country', size_max=60, animation_frame='year')
fig.show()Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- ValueError: Invalid value
- Check that your x and y data arrays have the same length and that column names match your DataFrame.
- ModuleNotFoundError: No module named 'plotly'
- Ensure Plotly is installed in your environment using pip or conda.
- TypeError: figure.show() missing
- Verify you have created a valid figure object using Plotly Express or Graph Objects.
Best practices
- Use Plotly Express for quick, simple visualizations; use Graph Objects for detailed, customized figures.
- Leverage hover labels, annotations, and legends for better interactivity.
- Combine subplots to display multiple visualizations in one figure.
- Use color scales and consistent styling for clarity and accessibility.
- Export figures to HTML for sharing interactive visualizations easily.
Background
Why it exists, and what it was reacting to.
Plotly was created by Chris Parmer, Jack Parmer, and Alex Johnson in 2013 to provide a framework for interactive, web-ready visualizations. It integrates seamlessly with Python, R, MATLAB, and JavaScript, and allows users to create interactive plots for analysis, reporting, and dashboards.
