Data Visualization with Plotly Basics β€” Intro to Plotly Express

https://youtu.be/b1Fyf83eWnM

Plotly Express is one of the most intuitive and powerful tools for creating interactive charts in Python. Whether you’re preparing a data report, building a dashboard, or exploring trends, this tutorial will guide you through essential visualizations like line, bar, and scatter plots.


πŸ“ˆ What is Plotly Express?

Plotly is a flexible graphing library that lets you build interactive, web-based visualizations. Plotly Express is its high-level interface, designed to make data visualization easy with minimal code. From hovers to zooms, and from color mapping to exporting, it’s a dream for anyone working in data.


βš™οΈ Setup and Sample Data

Installation

pip install plotly

Create a Simple DataFrame

import plotly.express as px
import pandas as pd

# Sample data
data = {
    'Year': [2019, 2020, 2021, 2022, 2023],
    'Sales': [150, 200, 300, 250, 400],
    'Region': ['North'] * 5
}
df = pd.DataFrame(data)
print(df)

This dataset shows annual sales figures for a single region. You can apply the same plotting techniques to more complex datasets too.


✍️ Create a Line Chart

A line chart helps show trends over time:

fig = px.line(df, x='Year', y='Sales', title='Annual Sales Trend')
fig.show()

Interactive features include:

  • Hover labels
  • Zoom & pan
  • Export options

πŸŒ† Create a Bar Chart

To compare values year-over-year, try a bar chart:

fig = px.bar(df, x='Year', y='Sales', title='Sales by Year', text='Sales')
fig.update_traces(textposition='outside')
fig.show()

The text='Sales' argument displays the numeric values directly on each bar.


πŸ”Ή Create a Scatter Plot

Want to visualize relationships? Here’s how to create a multivariate scatter plot using Plotly’s built-in Iris dataset:

df2 = px.data.iris()
fig = px.scatter(df2, x='sepal_width', y='sepal_length', color='species', size='petal_length',
                 title='Iris Dataset Scatter Plot')
fig.show()

This scatter plot maps:

  • X and Y axes
  • Color by species
  • Size by petal length

πŸ–ŠοΈ Customize the Charts

You can easily adjust aesthetics:

fig.update_layout(
    title='Customized Chart',
    xaxis_title='Year',
    yaxis_title='Sales',
    template='plotly_dark'
)
fig.show()

Plotly offers various themes: plotly_dark, ggplot2, seaborn, etc.


πŸ”„ Export and Share

Need to send or embed your chart?

fig.write_html("sales_chart.html")

This generates a standalone interactive HTML file you can send or embed anywhere.


πŸ” Wrap-Up

With just a few lines of code, Plotly Express enables you to create beautiful, dynamic visualizations. Whether it’s for dashboards, reports, or presentations, the clarity and interactivity you get are unmatched.


πŸ“Œ Summary

  • Installed Plotly
  • Created and visualized sample data
  • Built line, bar, and scatter plots
  • Customized layout and themes
  • Exported an interactive chart

πŸ”— Resources

  • Plotly Express Documentation
  • Chart Templates
  • Pandas Documentation

Explore more and keep building beautiful visualizations!

Leave a Reply

Your email address will not be published. Required fields are marked *