Plotly in Python
Introduction to Plotly in python
Plotly
is a popular interactive graphing library that makes it easy to create highly customizable, interactive charts in Python.
It's widely used for creating plots that can be embedded into web applications or shared with others for interactive analysis.
Installation
First, you need to install Plotly if you haven't already:
pip install plotly
Plotly Basic Usage
Plotly allows you to create interactive charts such as line plots, bar charts, scatter plots, histograms, pie charts, and much more.
- Basic Line Plot
import plotly.graph_objects as go
# Data
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
# Create a figure with a line plot
fig = go.Figure(data=go.Scatter(x=x, y=y, mode='lines', name='y = x^2'))
# Add titles and labels
fig.update_layout(title='Basic Line Plot', xaxis_title='X-axis', yaxis_title='Y-axis')
# Show the plot
fig.show()

- Basic Scatter Plot
import plotly.graph_objects as go
# Data
x = [1, 2, 3, 4, 5]
y = [5, 4, 3, 2, 1]
# Create a figure with a scatter plot
fig = go.Figure(data=go.Scatter(x=x, y=y, mode='markers', name='Scatter'))
# Add titles and labels
fig.update_layout(title='Basic Scatter Plot', xaxis_title='X-axis', yaxis_title='Y-axis')
# Show the plot
fig.show()

Bar Charts
You can create vertical or horizontal bar charts with Plotly.
- Bar Chart
import plotly.graph_objects as go
# Data
categories = ['A', 'B', 'C', 'D']
values = [10, 15, 7, 20]
# Create a bar chart
fig = go.Figure(data=go.Bar(x=categories, y=values))
# Add titles
fig.update_layout(title='Basic Bar Chart', xaxis_title='Categories', yaxis_title='Values')
# Show the plot
fig.show()

Pie Charts
Pie charts are great for showing proportions of categories.
- Pie Chart
import plotly.graph_objects as go
# Data
labels = ['A', 'B', 'C', 'D']
sizes = [25, 35, 25, 15]
# Create a pie chart
fig = go.Figure(data=go.Pie(labels=labels, values=sizes))
# Add title
fig.update_layout(title='Basic Pie Chart')
# Show the plot
fig.show()

Histogram
Histograms are used to visualize the distribution of data.
- Histogram
import plotly.express as px
import numpy as np
# Random data
data = np.random.randn(1000)
# Create a histogram
fig = px.histogram(data, nbins=30, title='Histogram')
# Show the plot
fig.show()

Interactive Plots
One of the greatest strengths of Plotly is the interactivity it provides in its plots. Users can hover over data points, zoom in/out, and pan through the data.
- Interactive Scatter Plot with Hover
import plotly.express as px
# Data
df = px.data.iris()
# Create an interactive scatter plot
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species", title="Interactive Scatter Plot")
# Show the plot
fig.show()

Subplots in Plotly
Plotly makes it easy to create multiple plots in a single figure using make_subplots
.
- Creating Subplots
import plotly.graph_objects as go
from plotly.subplots import make_subplots
# Create subplots (1 row, 2 columns)
fig = make_subplots(rows=1, cols=2)
# Line plot in the first subplot
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[1, 4, 9], mode='lines', name='Line Plot'), row=1, col=1)
# Scatter plot in the second subplot
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[9, 4, 1], mode='markers', name='Scatter Plot'), row=1, col=2)
# Add title
fig.update_layout(title='Subplots in Plotly')
# Show the plot
fig.show()

Customizing Plotly Plots
Plotly offers extensive customization options for fonts, colors, legends, and more.
- Customized Line Plot
import plotly.graph_objects as go
# Data
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
# Create a customized line plot
fig = go.Figure(data=go.Scatter(x=x, y=y, mode='lines+markers', name='y = x^2', line=dict(color='red', width=4)))
# Customize the layout
fig.update_layout(
title='Customized Line Plot',
xaxis_title='X-axis',
yaxis_title='Y-axis',
plot_bgcolor='lightgray',
font=dict(family='Arial', size=14, color='darkblue')
)
# Show the plot
fig.show()

3D Plotting
Plotly also supports 3D plots for advanced data visualization.
- 3D Scatter Plot
import plotly.graph_objects as go
import numpy as np
# Random 3D data
x = np.random.randn(100)
y = np.random.randn(100)
z = np.random.randn(100)
# Create a 3D scatter plot
fig = go.Figure(data=[go.Scatter3d(x=x, y=y, z=z, mode='markers', marker=dict(size=4, color='blue'))])
# Add titles and labels
fig.update_layout(
title='3D Scatter Plot',
scene=dict(
xaxis_title='X-axis',
yaxis_title='Y-axis',
zaxis_title='Z-axis'
)
)
# Show the plot
fig.show()

Saving Plotly Plots
You can save your Plotly figures as static images (PNG, JPEG) or interactive HTML files.
- Saving Plot to HTML
import plotly.graph_objects as go
# Data
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
# Create a line plot
fig = go.Figure(data=go.Scatter(x=x, y=y, mode='lines'))
# Save the plot as an HTML file
fig.write_html("plotly_plot.html")
# Show the plot
fig.show()
Plotly Features
- Interactivity: Zoom, pan, hover over points, etc.
- Customizable: Titles, legends, axes, colors, etc.
- Multiple Plot Types: Line, bar, scatter, pie, histograms, 3D plots, etc.
- Subplots: Combine multiple plots into one figure.
- Save as HTML: Share interactive plots or export them.
- 3D Plotting: Advanced visualizations for complex data.