Dataslope logoDataslope

Introducing Plotly Express

Your first proper tour of the library — what it imports, what it returns, and how to read its function names

You have seen Plotly Express in action through several previous pages. Now we slow down and look at it carefully — as a library, not as magic. By the end of this page you will understand what import plotly.express as px actually does, what a Plotly figure is, and how to read the names of all the chart functions.

The standard import

Every Plotly Express chart in this course (and in essentially every notebook in the wild) starts the same way:

import plotly.express as px

That single line does two things:

  1. It loads the plotly.express module, which contains all the chart functions: px.bar, px.line, px.scatter, and so on.
  2. It gives the module the short name px. By convention, every Python data analyst calls Plotly Express px. Just like NumPy is np and pandas is pd.

You will also frequently see:

import pandas as pd
import plotly.express as px

pd and px are the canonical pair. Most charts in this course will begin with those two imports.

The basic shape of a Plotly Express call

Every Plotly Express function takes a DataFrame plus some encoding arguments and returns a Figure object. The pattern looks like this:

fig = px.bar(
    df,                      # the data
    x="col_name",            # what goes on x
    y="col_name",            # what goes on y
    color="col_name",        # optional: color encoding
    title="My chart",        # optional: title
    template="simple_white", # optional: visual theme
)
fig.show()

That's the entire shape of essentially every chart call you will ever make. The first argument is the data; the rest are encodings and styling.

Try it:

Code Block
Python 3.13.2

Notice the separation of concerns:

  • df is the data.
  • x="fruit" and y="kg" are encodings — they say "use the fruit column for x and the kg column for y."
  • title=... and template=... are styling.

This separation is what makes Plotly Express so readable.

The Plotly Express function family

Plotly Express has about 30 chart functions. You don't need to memorize them; the names follow a tiny set of patterns. Here are the ones we'll meet in this course:

FunctionPurpose
px.scatterTwo-variable relationships, also bubble charts
px.lineTrends over an ordered axis (usually time)
px.barCompare values across categories
px.histogramDistribution of one variable
px.boxDistribution + outliers across categories
px.violinDistribution shape across categories
px.pieParts of a whole (use sparingly)
px.density_heatmap2-D density via colored grid
px.imshowImage / matrix / heatmap
px.choroplethMap colored by region
px.scatter_geoPoints on a world map
px.scatter_mapboxPoints on a tile-based map
px.timelineGantt-style time intervals
px.sunburst / px.treemapHierarchical compositions

There are more (px.parallel_coordinates, px.scatter_matrix, px.density_contour, ...). The cheat sheet on plotly.com is the canonical reference; don't try to memorize.

What fig.show() actually does

When you call fig.show(), Plotly Express does the following:

  1. Builds a JSON description of the chart (axes, data, encodings, styling).
  2. Sends that JSON to the Plotly.js library running in your browser.
  3. Plotly.js renders the chart as interactive SVG or WebGL, wrapped in HTML.

You don't normally need to think about this pipeline. But it explains why Plotly charts are interactive for free: the chart is a small JavaScript application.

The Figure object — a brief intro

The fig variable returned by every Plotly Express call is a Figure. You can inspect it:

Code Block
Python 3.13.2

Most of the time you won't print the figure; you'll just call .show(). But knowing that fig is a real Python object — that you can modify it before showing — opens the door to more advanced customization later (fig.update_layout(...), fig.update_traces(...), and so on).

Where do datasets come from?

Throughout this course we'll use three sources of data:

  1. Built-in Plotly datasets — small, classic datasets that ship with Plotly Express. Examples: px.data.iris(), px.data.tips(), px.data.gapminder(), px.data.stocks().
  2. Hand-built DataFrames — small tables we type out with pd.DataFrame({...}). Useful for tiny illustrative examples.
  3. CSV / Parquet files loaded with pandas — the real-world workflow. We will introduce these in the next chapter.

The built-in datasets are perfect for learning because they are small, real, and don't require any file I/O.

Check your understanding

QuestionSelect one

What does import plotly.express as px do?

It imports the plotly module and runs it.

It installs Plotly Express.

It loads the Plotly Express module and gives it the short alias px, so you can write px.bar(...) instead of plotly.express.bar(...).

It downloads a Plotly chart.

QuestionSelect one

A Plotly Express function call like px.bar(df, x="month", y="sales") returns:

A PNG image.

An HTML string.

A Plotly Figure object that can be inspected, modified, and rendered with fig.show().

The DataFrame, sorted.

QuestionSelect one

Which of these is NOT part of the standard Plotly Express call pattern?

A DataFrame as the first argument.

Column names as the values of x, y, color, etc.

A template=... argument for visual styling.

A function that runs the chart against a SQL database directly.

QuestionSelect one

Which built-in Plotly Express dataset is most useful for time-series examples?

px.data.iris()

px.data.tips()

px.data.gapminder() or px.data.stocks()

px.data.election()

On this page