Master Plotly Express: Create Stunning Visualizations with One Line of Code
This tutorial walks through installing Plotly Express in a Python 3.7 Jupyter environment and demonstrates how to create a wide range of visualizations—including line, scatter, area, geographic, violin, and polar charts—using concise one‑line commands and built‑in datasets.
Environment Setup
The examples were tested with the following environment:
Python 3.7
Jupyter notebook
Pandas 1.1.3
Plotly_express 0.4.1
$ python3 -m pip install plotly_expressTool Overview
Plotly_express is a high‑level wrapper around Plotly that provides a simplified API. It bundles modern templates and requires only a single function call to generate attractive, interactive visualizations. It is free, fully compatible with the Plotly ecosystem, and works seamlessly with Dash and Orca for exporting.
Getting Started with Plotting
Plotly_express ships with several built‑in datasets such as gapminder, tips, and iris. Below are examples of how to load these datasets and create different chart types.
Loading a Dataset
import pandas as pd
import numpy as np
import plotly_express as px # or import plotly.express as px
gapminder = px.data.gapminder()
gapminder.head()Line Chart
fig = px.line(
gapminder,
x="year",
y="lifeExp",
color="continent",
line_group="continent",
hover_name="country",
line_shape="spline",
render_mode="svg"
)
fig.show()Area Chart
fig = px.area(
gapminder,
x="year",
y="pop",
color="continent",
line_group="country"
)
fig.show()Scatter Plot
fig = px.scatter(
gapminder,
x="gdpPercap",
y="lifeExp",
color="continent",
size="pop",
size_max=60,
hover_name="country",
facet_col="continent",
animation_frame="year",
log_x=True,
range_x=[100, 100000],
range_y=[25, 90],
labels={"pop": "Populations", "gdpPercap": "GDP per Capital", "lifeExp": "Life Expectancy"}
)
fig.show()Geographic Maps
fig = px.choropleth(
gapminder,
locations="iso_alpha",
color="lifeExp",
hover_name="country",
animation_frame="year",
color_continuous_scale=px.colors.sequential.Plasma,
projection="natural earth"
)
fig.show()Using the Iris Dataset
fig = px.scatter(
iris,
x="sepal_width",
y="sepal_length",
color="species"
)
fig.show()Joint Distribution (Scatter + Marginals)
fig = px.scatter(
iris,
x="sepal_width",
y="sepal_length",
color="species",
marginal_x="histogram",
marginal_y="rug"
)
fig.show()Violin Plot with Box and Trendline
fig = px.scatter(
iris,
x="sepal_width",
y="sepal_length",
color="species",
marginal_y="violin",
marginal_x="box",
trendline="ols"
)
fig.show()Scatter Matrix
fig = px.scatter_matrix(
iris,
dimensions=["sepal_width", "sepal_length", "petal_width", "petal_length"],
color="species"
)
fig.show()Parallel Coordinates
fig = px.parallel_coordinates(
iris,
color="species_id",
labels={
"species_id": "Species",
"sepal_width": "Sepal Width",
"sepal_length": "Sepal Length",
"petal_length": "Petal Length",
"petal_width": "Petal Width"
},
color_continuous_scale=px.colors.diverging.Tealrose,
color_continuous_midpoint=2
)
fig.show()Box Plot with Error Bars
iris["e"] = iris["sepal_width"] / 100
fig = px.scatter(
iris,
x="sepal_width",
y="sepal_length",
color="species",
error_x="e",
error_y="e"
)
fig.show()Density Contour and Heatmap
fig = px.density_contour(
iris,
x="sepal_width",
y="sepal_length",
color="species"
)
fig.show()fig = px.density_heatmap(
iris,
x="sepal_width",
y="sepal_length",
marginal_y="rug",
marginal_x="histogram"
)
fig.show()Parallel Categories (Tips Dataset)
fig = px.parallel_categories(
tips,
color="size",
color_continuous_scale=px.colors.sequential.Inferno
)
fig.show()Bar Chart with Facets
fig = px.bar(
tips,
x="sex",
y="total_bill",
color="smoker",
barmode="group",
facet_row="time",
facet_col="day",
category_orders={"day": ["Thur", "Fri", "Sat", "Sun"], "time": ["Lunch", "Dinner"]}
)
fig.show()Histogram
fig = px.histogram(
tips,
x="sex",
y="tip",
histfunc="avg",
color="smoker",
barmode="group",
facet_row="time",
facet_col="day",
category_orders={"day": ["Thur", "Fri", "Sat", "Sun"], "time": ["Lunch", "Dinner"]}
)
fig.show()Box and Violin Plots
px.box(tips, x="day", y="total_bill", color="smoker", notched=True)px.violin(
tips,
x="smoker",
y="tip",
color="sex",
box=True,
points="all",
hover_data=tips.columns
)Polar Charts
fig = px.line_polar(
wind,
r="frequency",
theta="direction",
color="strength",
line_close=True,
color_discrete_sequence=px.colors.sequential.Plasma_r
)
fig.show()fig = px.bar_polar(
wind,
r="frequency",
theta="direction",
color="strength",
template="plotly_dark",
color_discrete_sequence=px.colors.sequential.Plasma_r
)
fig.show()Color Palettes
Plotly_express provides both qualitative and sequential color swatches.
px.colors.qualitative.swatches()px.colors.sequential.swatches()Themes
Three built‑in themes are available: plotly, plotly_white, and plotly_dark. They can be applied via the template argument.
px.scatter(
iris,
x="sepal_width",
y="sepal_length",
color="species",
marginal_x="box",
marginal_y="histogram",
height=600,
trendline="ols",
template="plotly"
)px.scatter(
iris,
x="sepal_width",
y="sepal_length",
color="species",
marginal_x="box",
marginal_y="histogram",
height=600,
trendline="ols",
template="plotly_white"
)px.scatter(
iris,
x="sepal_width",
y="sepal_length",
color="species",
marginal_x="box",
marginal_y="histogram",
height=600,
trendline="ols",
template="plotly_dark"
)Summary
Plotly_express enables rapid creation of a variety of charts—including bar, line, scatter, violin, and polar plots—with minimal code. Its advantages are:
Fast chart generation with just a few parameters.
Visually appealing, vibrant color schemes.
Interactive, dynamic graphics when used in Jupyter notebooks.
These features make Plotly_express an excellent tool for quickly mastering data visualization in Python.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
