Plotly

Language: Python

Data Science

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.

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.

Installation

pip: pip install plotly
conda: conda install -c plotly plotly

Usage

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.

Simple line plot with Plotly Express

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()

Creates an interactive line plot from a Pandas DataFrame and displays it in a browser or notebook.

Scatter plot with Plotly Express

import plotly.express as px
df = px.data.iris()
fig = px.scatter(df, x='sepal_width', y='sepal_length', color='species')
fig.show()

Generates a scatter plot with color-coded species categories using Plotly Express.

Customizing layout with Graph Objects

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()

Demonstrates using Graph Objects to create a scatter plot with custom layout titles and styling.

3D Surface plot

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()

Creates an interactive 3D surface plot of a mathematical function.

Subplots and multiple traces

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()

Shows how to create multiple plots in a single figure using subplots with different trace types.

Interactive animations

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()

Creates an animated scatter plot showing changes over years using Plotly Express.

Error Handling

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.