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.
fig.show(), use rx.plotly(data=fig) within your UI code to ensure the graph is properly rendered and displayed within the user interfaceBasic 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:
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.
ExpandCollapse
📊 Dataset source: mt_bruno_elevation.csv
Plot as State Var
If the figure is set as a state var, it can be updated during run time.
ExpandCollapse
Adding Styles and Layouts
Use update_layout() method to update the layout of your chart. Checkout Plotly Layouts for all layouts props.
ExpandCollapse
API Reference
rx.plotly
Display a plotly graph.
Props
| Prop | Type | Description |
|---|---|---|
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. |