Related Data Science Links
Learn Plotly Data Science Tutorial, validate concepts with Plotly Data Science MCQ Questions, and prepare interviews through Plotly Data Science Interview Questions and Answers.
Plotly
Interactive
Beginner Friendly
Dashboards
Plotly Interactive Visualization
Learn how to build interactive charts in Python using Plotly, including scatter plots, bar charts, and line charts with hover, zoom, and export features.
What is Plotly?
Plotly is a powerful library for building interactive visualizations in Python. It works well in Jupyter notebooks, web apps (Dash), and static HTML exports.
Why Use Plotly?
- Interactive charts with zoom, pan, and hover tooltips.
- Easy integration into web dashboards and data apps.
- Supports many chart types: scatter, bar, line, maps, 3D and more.
Installation & Basic Import
Install Plotly
# Install Plotly (terminal)
pip install plotly
Import Plotly Express
import plotly.express as px
Example 1: Interactive Scatter Plot
Scatter plots can show relationships between two variables with interactive tooltips.
Iris Sepal Length vs Width
import plotly.express as px
import pandas as pd
# Sample data (could be the full Iris dataset)
data = {
"sepal_length": [5.1, 4.9, 6.7, 5.9],
"sepal_width": [3.5, 3.0, 3.1, 3.0],
"species": ["setosa", "setosa", "versicolor", "virginica"]
}
df = pd.DataFrame(data)
# Create an interactive scatter plot
fig = px.scatter(
df,
x="sepal_length",
y="sepal_width",
color="species", # color points by species
title="Iris Sepal Length vs Width",
hover_data=["species"], # show extra info on hover
size_max=10
)
fig.show() # opens in notebook or browser
Example 2: Interactive Bar Chart
Fruit Count
import plotly.express as px
import pandas as pd
data = {
"fruit": ["Apple", "Banana", "Orange"],
"count": [10, 15, 7]
}
df = pd.DataFrame(data)
fig = px.bar(
df,
x="fruit", # categories
y="count", # values
title="Fruit Count",
color="fruit", # each bar with different color
text="count" # show value on top of bar
)
# Make labels easier to read
fig.update_traces(textposition="outside")
fig.show()
Example 3: Line Chart
Sales Over Time
import plotly.express as px
import pandas as pd
data = {
"day": [1, 2, 3, 4, 5],
"sales": [100, 120, 90, 150, 130]
}
df = pd.DataFrame(data)
fig = px.line(
df,
x="day",
y="sales",
title="Sales Over 5 Days",
markers=True # show markers on the line
)
# Customize axes labels
fig.update_layout(
xaxis_title="Day",
yaxis_title="Sales ($)"
)
fig.show()