For AI agents: the complete documentation index is at llms.txt. Markdown versions are available by appending .md or sending Accept: text/markdown.
Reflex Logo
Docs Logo
Library

/

Graphing

/

Other Charts

/

Plotly

Plotly in Python: Interactive Charts with Reflex

Plotly is a popular Python graphing library for creating interactive, publication-quality charts. Reflex wraps it with the rx.plotly component so you can embed any Plotly or Plotly Express figure — line charts, scatter plots, histograms, heatmaps, or 3D surface plots — directly into a Python web app with no JavaScript. Because Reflex compiles to a full-stack web app, these charts stay interactive in the browser and can update live from your app state.

When integrating Plotly graphs into your UI code, note that the method for displaying the graph differs from a regular Python script. Instead of using fig.show(), use rx.plotly(data=fig) within your UI code to ensure the graph is properly rendered and displayed within the user interface

Basic Example

Let's create a line graph of life expectancy in Canada.

Plotly Express Chart Types

Plotly Express (plotly.express, imported as px) builds common chart types in a single line of Python, and every figure renders in Reflex with rx.plotly.

Bar Chart

Create a Plotly Express bar chart with px.bar:

Scatter Plot

Create a Plotly scatter plot with px.scatter:

Pie Chart

Create a Plotly pie chart with px.pie:

Heatmap

Create a Plotly heatmap with px.density_heatmap:

Histogram

Create a Plotly histogram with px.histogram:

Box Plot

Create a Plotly box plot with px.box:

Bubble Chart

A bubble chart is a scatter plot in which a third dimension of the data is shown through the size of the markers. Create one with px.scatter by passing a column to the size argument:

Gantt Chart

A Gantt chart is a type of bar chart that illustrates a project schedule: tasks are listed on the vertical axis, time intervals on the horizontal axis, and the width of each bar shows the duration of the activity. Create one with px.timeline:

Sunburst Chart

Sunburst charts visualize hierarchical data spanning outwards radially from root to leaves: the root sits at the center and children are added to the outer rings. Create one with px.sunburst, defining the hierarchy with names and parents:

Expand

Funnel Chart

Funnel charts represent data as it moves through the stages of a business process, making them a common Business Intelligence tool for spotting where a process loses volume. Create one with px.funnel:

Locale Configuration

Use locale to localize Plotly number/date formatting and modebar labels:

You can still pass config; when both are provided, locale= is applied as the final locale value.

3D graphing example

Let's create a 3D surface plot of Mount Bruno. This is a slightly more complicated example, but it wraps in Reflex using the same method. In fact, you can wrap any figure using the same approach.

Expand

📊 Dataset source: mt_bruno_elevation.csv

Financial Charts

Candlestick Chart

The candlestick chart is a financial chart describing the open, high, low, and close values for a given x coordinate (most likely time): boxes show the spread between open and close, and lines show the spread between low and high. Create one with go.Candlestick:

Expand

Waterfall Chart

The waterfall chart visualizes how an initial value is affected by a series of positive and negative changes — for example, a profit and loss statement. Create one with go.Waterfall, marking each value as "relative" or "total" via the measure argument:

Expand

Bullet Chart

The bullet chart, designed by Stephen Few as a compact replacement for dashboard gauges and meters, combines a quantitative bar, qualitative ranges (steps), and a performance threshold line in one simple layout. Build one with go.Indicator using the "bullet" gauge shape:

Expand

Statistical Charts

Continuous Error Bands

Continuous error bands represent error or uncertainty as a shaded region around a main trace, rather than as discrete whisker-like error bars. Build one with go.Scatter by drawing the main line, then a second trace that walks the upper bound forward and the lower bound in reverse, filled with fill="toself":

Expand

Maps

Geo Map

Geo maps are outline-based maps drawn from geographic features rather than map tiles. Figures created with px.scatter_geo, px.line_geo, or px.choropleth — or containing go.Scattergeo or go.Choropleth traces — store their map configuration in the figure's layout.geo object, which you can adjust with update_geos:

Scatter Map

Scatter maps plot markers on a tile-based map, sized and colored by your data — useful for visualizing geographic point data like vehicle locations or store sites. Create one with px.scatter_map (or a go.Scattermap trace for lower-level control):

Tables and Diagrams

Table

Plotly can also render data as an interactive table. Create one with go.Table, passing column headers to header and column data to cells:

Sankey Diagram

A Sankey diagram is a flow diagram in which the width of the arrows is proportional to the flow quantity. Create one with go.Sankey, defining the nodes and the links between them by index:

Expand

3D Charts

3D Scatter Plot

3D scatter plots show the relationship between three variables at once, with an optional fourth encoded as color. Create one with px.scatter_3d:

3D Axis

3D figures place their traces in a scene, and each scene axis is configured through the figure's scene layout — set nticks, range, or axis titles per axis. This example renders a go.Mesh3d cloud with custom tick counts and ranges on all three axes:

Expand

Plot as State Var

If the figure is set as a state var, it can be updated during run time.

Expand

Adding Styles and Layouts

Use update_layout() method to update the layout of your chart. Checkout Plotly Layouts for all layouts props.

Note that the width and height props are not recommended to ensure the plot remains size responsive to its container. The size of plot will be determined by it's outer container.
Expand

API Reference

rx.plotly

Display a plotly graph.

Props

PropTypeDescription
data
Figure

The figure to display. This can be a plotly figure or a plotly data json.

layout
dict

The layout of the graph.

template
Template

The template for visual appearance of the graph.

config
dict

The config of the graph.

locale
str

The locale code used for Plotly formatting and modebar labels.

use_resize_handler
bool

If true, the graph will resize when the window is resized.

Event Triggers

See the full list of default event triggers
TriggerDescription
on_after_plotFired after the plot is redrawn.
on_animatedFired after the plot was animated.
on_animating_frameFired while animating a single frame (does not currently pass data through).
on_animation_interruptedFired when an animation is interrupted (to start a new animation for example).
on_autosizeFired when the plot is responsively sized.
on_before_hoverFired whenever mouse moves over a plot.
on_button_clickedFired when a plotly UI button is clicked.
on_deselectFired when a selection is cleared (via double click).
on_hoverFired when a plot element is hovered over.
on_relayoutFired after the plot is laid out (zoom, pan, etc).
on_relayoutingFired while the plot is being laid out.
on_restyleFired after the plot style is changed.
on_redrawFired after the plot is redrawn.
on_selectedFired after selecting plot elements.
on_selectingFired while dragging a selection.
on_transitioningFired while an animation is occurring.
on_transition_interruptedFired when a transition is stopped early.
on_unhoverFired when a hovered element is no longer hovered.
Built with Reflex