4 min read min read
Introduction to Plotly
Learn to create interactive charts with Plotly
Introduction to Plotly
What is Plotly?
Plotly creates interactive charts. Unlike matplotlib:
- You can zoom in/out
- Hover to see values
- Pan around the chart
- Export as image
Install Plotly
terminal
pip install plotlyYour First Interactive Chart
code.py
import plotly.express as px
# Simple line chart
fig = px.line(x=[1, 2, 3, 4, 5], y=[1, 4, 9, 16, 25])
fig.show()Try hovering over the points!
Why Plotly?
| Matplotlib | Plotly |
|---|---|
| Static images | Interactive |
| More control | Easier syntax |
| Print/PDF | Web/Dashboard |
Basic Chart Types
code.py
import plotly.express as px
# Line chart
px.line(x=[1,2,3], y=[1,4,9])
# Bar chart
px.bar(x=['A','B','C'], y=[10,20,15])
# Scatter plot
px.scatter(x=[1,2,3], y=[1,4,9])With DataFrame
code.py
import plotly.express as px
import pandas as pd
df = pd.DataFrame({
'Month': ['Jan', 'Feb', 'Mar', 'Apr'],
'Sales': [100, 120, 115, 140]
})
fig = px.bar(df, x='Month', y='Sales', title='Monthly Sales')
fig.show()Interactive Features
When you view a Plotly chart:
- Hover: See exact values
- Zoom: Click and drag to zoom
- Pan: Shift + drag to move
- Reset: Double-click to reset
- Download: Camera icon to save
Key Points
- plotly.express (px) is the easy way
- Charts are interactive by default
- Works great with pandas DataFrames
- fig.show() displays the chart
- Perfect for dashboards and web apps
What's Next?
Learn Plotly Express basics for creating charts quickly.