What are plotly.express, plotly.io, and plotly.go and when do we use them? I keep running into them in the Plotly documentation, but I can't really figure out what the differences are. My guess is that plotly.express is the most general-use part of the plotly API and plotly.go is used more commonly when you want to publish a graph as HTML or in some other format, but I suspect that this crude impression is incorrect. Any thoughts, comments, or general advice is appreciated. Thanks.
Plotly express is high-level interface, which is usually your primary starting point (most of the tutorials demonstrate the same). It turns a DataFrame into a complete, interactive chart with a single line of code.
But for specific low-level customization, you need plotly.graph_objects, which is the lower-level engine that Plotly Express is actually running under the hood.
The difference is similar to using seaborn for quick plotting with reasonble defaults and matplotlib for low-level customizations. (It's just an analogy)
Finally, plotly.io serves a completely different purpose. It is the utility module for management and file handling rather than chart construction. While the other two are for building the visual, plotly.io is what you use to save that visual as a static PNG or export it as a standalone HTML file, or managing the renderer configuration, etc.
In short: use Plotly Express to build fast, Graph Objects to build custom, and IO to save or display the results.
Edit: Adding sample code:
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import plotly.io as pio
df = pd.DataFrame({
"x": [1, 2, 3, 4],
"y": [10, 15, 13, 17],
"group": ["A", "A", "B", "B"]
})
pio.templates.default = "plotly_dark" # Comment or uncomment this line
# Turning a DataFrame into a complete figure with one line
fig_px = px.scatter(df, x="x", y="y", color="group",
title="One-line interactive chart")
fig_px.show()
# Fine-grained customization_Different marker symbols per trace, custom hover text
fig_go = go.Figure()
fig_go.add_trace(
go.Scatter(
x=df[df["group"] == "A"]["x"],
y=df[df["group"] == "A"]["y"],
mode="markers",
name="Group A",
marker=dict(symbol="star", size=14),
hovertemplate="Group A<br>x=%{x}<br>y=%{y}<extra></extra>"
)
)
fig_go.add_trace(
go.Scatter(
x=df[df["group"] == "B"]["x"],
y=df[df["group"] == "B"]["y"],
mode="markers",
name="Group B",
marker=dict(symbol="diamond", size=14),
hovertemplate="Group B<br>x=%{x}<br>y=%{y}<extra></extra>"
)
)
fig_go.update_layout(title="Low-level control with graph_objects")
fig_go.show()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With