#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
15 min read

Visualization II: Plotly

Creating interactive, web-ready visualizations

What You'll Learn

  • Why Plotly?
  • Plotly Express (High-level API)
  • Interactive features (zoom, hover, filter)
  • Creating 3D plots
  • Exporting HTML

Why Plotly?

  • Interactive: Zoom, pan, hover tooltips built-in.
  • Web-ready: Renders as HTML/JavaScript.
  • Beautiful: Modern default aesthetics.

Plotly Express (px)

The easiest way to use Plotly.

code.py
import plotly.express as px

# Load data
df = px.data.gapminder()

# Interactive Scatter Plot
fig = px.scatter(
    df.query("year==2007"),
    x="gdpPercap",
    y="lifeExp",
    size="pop",
    color="continent",
    hover_name="country",
    log_x=True,
    size_max=60,
    title="World Development in 2007"
)

fig.show()

Common Plots

Bar Chart:

code.py
fig = px.bar(df, x='country', y='pop', title='Population')
fig.show()

Line Chart:

code.py
fig = px.line(df.query("country=='Canada'"), x='year', y='lifeExp', title='Life Expectancy in Canada')
fig.show()

Histogram:

code.py
fig = px.histogram(df, x="lifeExp", nbins=30, title="Life Expectancy Distribution")
fig.show()

Advanced Features

Faceting (Subplots):

code.py
fig = px.scatter(
    df, x="gdpPercap", y="lifeExp", color="continent",
    facet_col="continent", # Split by column
    log_x=True
)
fig.show()

3D Plots:

code.py
fig = px.scatter_3d(
    df.query("year==2007"),
    x='gdpPercap', y='lifeExp', z='pop',
    color='continent'
)
fig.show()

Animation:

code.py
fig = px.scatter(
    df, x="gdpPercap", y="lifeExp", animation_frame="year",
    animation_group="country", size="pop", color="continent",
    hover_name="country", log_x=True, size_max=55,
    range_x=[100,100000], range_y=[25,90]
)
fig.show()

Saving Plotly Figures

code.py
# Save as interactive HTML
fig.write_html("my_plot.html")

# Save as static image (requires kaleido)
# fig.write_image("my_plot.png")

Practice Exercise

Use the tips dataset to create an interactive scatter plot of total_bill vs tip, colored by sex, and faceted by day.

Next Steps

You've mastered visualization! Now let's wrap up with Machine Learning basics.

Practice & Experiment

Test your understanding by running Python code directly in your browser. Try the examples from the article above!

SkillsetMaster - AI, Web Development & Data Analytics Courses