Plotly.Express (plotly_ex v0.3.0)

Copy Markdown View Source

High-level Express-style API. Accepts Explorer DataFrames or plain lists of atom- or string-keyed maps (e.g. [%{col: value}] or [%{"col" => value}]).

Returns a Plotly.Figure ready to pipe into Plotly.show/1.

Express-level options

These options are consumed by Plotly.Express before the trace is built. Everything else is forwarded to the underlying trace struct as-is.

  • :color — group rows by a column value; each unique value becomes a separate trace. Supported by scatter/2, bar/2, line/2, and quiver/2.
  • :title — sets layout.title
  • :width, :height — set layout.width / layout.height (pixels)
  • :template — sets layout.template (bundled template name or inline map, e.g. "plotly_dark", "plotly_white", "ggplot2")

Data shapes accepted

List of string-keyed maps — most common:

data = [
  %{"month" => "Jan", "sales" => 42},
  %{"month" => "Feb", "sales" => 55}
]

Plotly.Express.bar(data, x: "month", y: "sales", title: "Revenue")
|> Plotly.show()

Column selectors may be atoms or strings. For map rows an exact key wins, including when its value is nil or false; otherwise the other key form is used. Missing columns return nil, without creating atoms.

Explorer DataFrame — requires {:explorer, "~> 0.11"} in deps:

df = Explorer.DataFrame.from_csv!("data.csv")
Plotly.Express.scatter(df, x: "date", y: "price")
|> Plotly.show()

Summary

Functions

Polar bar chart.

Candlestick chart.

Funnel chart.

2D histogram (density heatmap).

Indicator / gauge chart.

Vector field arrows from positions and vector components.

3D scatter chart.

Scatter chart.

Geographic scatter chart (geo projection).

Tile map scatter (uses open-street-map by default, no API key needed).

Polar scatter chart.

Sunburst chart.

Treemap chart.

Waterfall chart.

Functions

bar(data, opts \\ [])

Bar chart.

Required: :x, :y. Optional: :color (grouped bars — one trace per group), :title, :width, :height, :template.

Examples

monthly = [
  %{"month" => "Jan", "sales" => 1200},
  %{"month" => "Feb", "sales" => 1450},
  %{"month" => "Mar", "sales" => 1100}
]

Plotly.Express.bar(monthly, x: "month", y: "sales", title: "Monthly Sales")
|> Plotly.show()

# Grouped bar chart — one bar series per region
Plotly.Express.bar(regional_data, x: "quarter", y: "revenue", color: "region")
|> Plotly.show()

iex> data = [%{"x" => "A", "y" => 1, "g" => "r"}, %{"x" => "B", "y" => 2, "g" => "b"}]; :ok
:ok
iex> fig = Plotly.Express.bar(data, x: "x", y: "y", color: "g"); :ok
:ok
iex> length(fig.data)
2

barpolar(data, opts \\ [])

Polar bar chart.

Required: :r, :theta. Optional: :title, :width, :height.

Examples

Plotly.Express.barpolar(wind_data, r: "frequency", theta: "direction")
|> Plotly.show()

box(data, opts \\ [])

Box plot.

Required: :x, :y. Optional: :title, :width, :height, :template.

Examples

Plotly.Express.box(results, x: "group", y: "value", title: "Distribution by Group")
|> Plotly.show()

candlestick(data, opts \\ [])

Candlestick chart.

Required: :x, :open, :high, :low, :close. Optional: :title, :width, :height.

Examples

prices = [
  %{"date" => "2024-01-01", "o" => 150, "h" => 158, "l" => 148, "c" => 155},
  %{"date" => "2024-01-02", "o" => 155, "h" => 162, "l" => 153, "c" => 160}
]

Plotly.Express.candlestick(prices,
  x: "date", open: "o", high: "h", low: "l", close: "c",
  title: "AAPL Jan 2024"
)
|> Plotly.show()

choropleth(data, opts \\ [])

Choropleth map.

Required: :locations, :z. Defaults locationmode to "ISO-3". Optional: :title, :width, :height.

Examples

gdp = [
  %{"iso" => "USA", "gdp" => 23_000},
  %{"iso" => "DEU", "gdp" => 4_200},
  %{"iso" => "CHN", "gdp" => 18_000}
]

Plotly.Express.choropleth(gdp, locations: "iso", z: "gdp", title: "GDP by Country")
|> Plotly.show()

funnel(data, opts \\ [])

Funnel chart.

Required: :x, :y. Optional: :title, :width, :height.

Examples

funnel_data = [
  %{"stage" => "Leads", "count" => 1000},
  %{"stage" => "Qualified", "count" => 400},
  %{"stage" => "Proposals", "count" => 150},
  %{"stage" => "Closed", "count" => 60}
]

Plotly.Express.funnel(funnel_data, x: "count", y: "stage", title: "Sales Funnel")
|> Plotly.show()

histogram2d(data, opts \\ [])

2D histogram (density heatmap).

Required: :x, :y. Optional: :title, :width, :height.

Examples

Plotly.Express.histogram2d(measurements, x: "height", y: "weight",
  title: "Height vs Weight Density"
)
|> Plotly.show()

histogram(data, opts \\ [])

Histogram.

Required: :x. Optional: :title, :width, :height, :template.

Examples

data = Enum.map(1..500, fn _ -> %{"score" => :rand.normal(70.0, 10.0)} end)

Plotly.Express.histogram(data, x: "score", title: "Score Distribution")
|> Plotly.show()

indicator(data, opts \\ [])

Indicator / gauge chart.

Required: :value (first row's value is used). Optional: :title, :width, :height.

Examples

Plotly.Express.indicator([%{"kpi" => 92.5}], value: "kpi", title: "Satisfaction Score")
|> Plotly.show()

line(data, opts \\ [])

Line chart.

Required: :x, :y. Optional: :color, :title, :width, :height, :template.

Examples

Plotly.Express.line(time_series, x: "date", y: "value", title: "Trend")
|> Plotly.show()

# Multiple lines via color grouping
Plotly.Express.line(multi_series, x: "date", y: "value", color: "series")
|> Plotly.show()

ohlc(data, opts \\ [])

OHLC chart.

Required: :x, :open, :high, :low, :close. Optional: :title, :width, :height.

Examples

Plotly.Express.ohlc(prices,
  x: "date", open: "open", high: "high", low: "low", close: "close"
)
|> Plotly.show()

pie(data, opts \\ [])

Pie chart.

Required: :values. Optional: :names, :title, :width, :height.

Examples

browser_share = [
  %{"browser" => "Chrome", "pct" => 65},
  %{"browser" => "Firefox", "pct" => 18},
  %{"browser" => "Safari", "pct" => 12},
  %{"browser" => "Other", "pct" => 5}
]

Plotly.Express.pie(browser_share,
  values: "pct", names: "browser", title: "Browser Market Share"
)
|> Plotly.show()

iex> data = [%{"v" => 10}, %{"v" => 20}]; :ok
:ok
iex> fig = Plotly.Express.pie(data, values: "v"); :ok
:ok
iex> hd(fig.data).values
[10, 20]

quiver(data, opts \\ [])

Vector field arrows from positions and vector components.

Required column selectors: :x, :y, :u, :v. Optional: :color (one trace per group), :title, :width, :height, :template. Remaining options, such as :anchor and :lengthfactor, go to each quiver trace.

Examples

iex> field = [%{"x" => 0, "y" => 0, "u" => 1, "v" => 2}]; :ok
:ok
iex> fig = Plotly.Express.quiver(field, x: "x", y: "y", u: "u", v: "v"); :ok
:ok
iex> hd(fig.data).u
[1]

scatter3d(data, opts \\ [])

3D scatter chart.

Required: :x, :y, :z. Optional: :title, :width, :height, :template.

Examples

points = [
  %{"x" => 1, "y" => 2, "z" => 3},
  %{"x" => 4, "y" => 5, "z" => 6},
  %{"x" => 7, "y" => 8, "z" => 9}
]

Plotly.Express.scatter3d(points, x: "x", y: "y", z: "z", title: "3D Scatter")
|> Plotly.show()

scatter(data, opts \\ [])

Scatter chart.

Required: :x, :y. Optional: :color, :title, :width, :height, :template.

Examples

data = [
  %{"year" => 2020, "sales" => 120},
  %{"year" => 2021, "sales" => 145},
  %{"year" => 2022, "sales" => 180}
]

Plotly.Express.scatter(data, x: "year", y: "sales", title: "Annual Sales")
|> Plotly.show()

# :color groups rows into separate traces — one per unique value
Plotly.Express.scatter(market_data, x: "date", y: "close", color: "ticker")
|> Plotly.show()

iex> data = [%{"a" => 1, "b" => 2}, %{"a" => 3, "b" => 4}]; :ok
:ok
iex> fig = Plotly.Express.scatter(data, x: "a", y: "b"); :ok
:ok
iex> hd(fig.data).x
[1, 3]

scattergeo(data, opts \\ [])

Geographic scatter chart (geo projection).

Required: :lat, :lon. Optional: :title, :width, :height.

Examples

cities = [
  %{"city" => "London", "lat" => 51.5, "lon" => -0.1, "pop" => 9_000_000},
  %{"city" => "Tokyo", "lat" => 35.7, "lon" => 139.7, "pop" => 14_000_000}
]

Plotly.Express.scattergeo(cities, lat: "lat", lon: "lon", title: "World Cities")
|> Plotly.show()

scattermap(data, opts \\ [])

Tile map scatter (uses open-street-map by default, no API key needed).

Required: :lat, :lon. Optional: :title, :width, :height.

Examples

stations = [
  %{"name" => "London", "lat" => 51.5074, "lon" => -0.1278},
  %{"name" => "Paris", "lat" => 48.8566, "lon" => 2.3522},
  %{"name" => "Berlin", "lat" => 52.5200, "lon" => 13.4050}
]

Plotly.Express.scattermap(stations, lat: "lat", lon: "lon", title: "European Capitals")
|> Plotly.show()

scatterpolar(data, opts \\ [])

Polar scatter chart.

Required: :r, :theta. Optional: :title, :width, :height.

Examples

wind = Enum.map(0..11, fn i -> %{"deg" => i * 30, "speed" => :rand.uniform(20)} end)

Plotly.Express.scatterpolar(wind, r: "speed", theta: "deg", title: "Wind Rose")
|> Plotly.show()

sunburst(data, opts \\ [])

Sunburst chart.

Required: :labels, :parents. Optional: :values, :title, :width, :height.

Examples

hierarchy = [
  %{"id" => "World", "parent" => "", "pop" => 8_000},
  %{"id" => "Asia", "parent" => "World", "pop" => 4_600},
  %{"id" => "Europe", "parent" => "World", "pop" => 750}
]

Plotly.Express.sunburst(hierarchy,
  labels: "id", parents: "parent", values: "pop", title: "Population by Region"
)
|> Plotly.show()

treemap(data, opts \\ [])

Treemap chart.

Required: :labels, :parents. Optional: :values, :title, :width, :height.

Examples

Plotly.Express.treemap(hierarchy, labels: "id", parents: "parent", values: "size")
|> Plotly.show()

violin(data, opts \\ [])

Violin plot.

Required: :x, :y. Optional: :title, :width, :height, :template.

Examples

Plotly.Express.violin(measurements, x: "category", y: "value", title: "Distribution")
|> Plotly.show()

waterfall(data, opts \\ [])

Waterfall chart.

Required: :x, :y. Optional: :title, :width, :height.

Examples

cashflow = [
  %{"item" => "Revenue", "amount" => 500},
  %{"item" => "COGS", "amount" => -200},
  %{"item" => "Gross Profit", "amount" => 300}
]

Plotly.Express.waterfall(cashflow, x: "item", y: "amount", title: "P&L Waterfall")
|> Plotly.show()