Plotly Express Quick Start: Create Stunning Interactive Visualizations with Minimal Code
This article introduces Plotly Express, a high‑level Python visualization library, covering installation, built‑in datasets, color palettes, themes, and step‑by‑step examples for bar, scatter, bubble, matrix, area, line, pie, sunburst, funnel, 3D, map, and polar charts, demonstrating how to generate dynamic, publication‑ready plots with just a few lines of code.
Plotly Express Quick Introduction
Plotly Express is a high‑level wrapper for plotly.py that provides a simple API for creating beautiful, interactive visualizations with minimal code. It includes many built‑in datasets and color palettes.
Installation
Install the package via pip:
pip install plotly_expressBuilt‑in Datasets
Import the libraries and explore the datasets:
import pandas as pd
import numpy as np
import plotly_express as px # or import plotly.express as pxGDP Data
country
continent
year
lifeExp
pop
gdpPercap
iso_alpha
iso_num
Restaurant Tips Data
bill
tip
sex
smoker
day
time
size
Iris Data
sepal_length
sepal_width
petal_length
petal_width
species
species_id
Wind Data
direction
strength
frequency
Election Results
district
Coderre
Bergeron
Joly
total
winner
result
district_id
Car Sharing Availability
centroid_lat
centroid_lon
car_hours
peak_hour
Stock Data
date
GOOG
AAPL
AMZN
FB
NFLX
MSFT
Built‑in Color Palettes
Plotly Express provides many color palettes. Example calls:
px.colors.carto.swatches()px.colors.cmocean.swatches()px.colors.colorbrewer.swatches()px.colors.cyclical.swatches()px.colors.diverging.swatches()px.colors.qualitative.swatches()px.colors.sequential.swatches()Plotting Examples
Bar Chart
# Select Switzerland data
gapminder = px.data.gapminder()
Switzerland = gapminder[gapminder["country"] == "Switzerland"]
px.bar(Switzerland, x="year", y="pop", color="pop")Scatter Plot
gapminder_2002 = gapminder[gapminder["year"] == 2002]
px.scatter(gapminder_2002, x="gdpPercap", y="lifeExp", color="continent")Bubble Scatter
px.scatter(gapminder_2002, x="gdpPercap", y="lifeExp", color="continent", size="pop", size_max=60)Scatter Matrix
px.scatter_matrix(iris, dimensions=["sepal_width","sepal_length","petal_width","petal_length"], color="species")Area Chart
px.area(gapminder, x="year", y="pop", color="continent", line_group="country")Line Chart (Stock)
px.line(stock, x='date', y="FB")Pie Chart
px.pie(total_bill_byday, names="day", values="total_bill")Sunburst
px.sunburst(gapminder_2002, path=['continent','country'], values='pop', color='lifeExp', hover_data=['iso_alpha'])Funnel
data = dict(number=[1000,800,400,200,100,30], stage=["UV","搜索","搜藏","加购","下单","付款"])
px.funnel(data, x="number", y="stage")
px.funnel(data, x="number", y="stage", color="number")3D Scatter
px.scatter_3d(election, x="Joly", y="Coderre", z="Bergeron", color="winner", size="total", hover_name="district_id", symbol="result", color_discrete_map={"Joly":"blue","Bergeron":"green","Coderre":"red"})Map Visualizations
# Choropleth
px.choropleth(gapminder, locations="iso_alpha", color="lifeExp", hover_name="country", animation_frame="year", color_continuous_scale=px.colors.sequential.Plasma, projection="natural earth")
# Line Geo
px.line_geo(gapminder_2002, locations="iso_alpha", color="continent", projection="orthographic")Built‑in Themes
Plotly Express offers three themes: plotly, plotly_white, and plotly_dark. Example:
px.scatter(gapminder_2002, x="gdpPercap", y="lifeExp", color="continent", template="plotly_dark")Conclusion
This article provides a comprehensive overview of Plotly Express, highlighting its ease of use, extensive chart types, built‑in datasets, color palettes, and themes. With just a few lines of code, users can create sophisticated, interactive visualizations, making data exploration and presentation both efficient and visually appealing.
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.
