Menu

Advanced Plotly with Graph Objects

Advanced Plotly with Graph Objects

Plotly Express is fast and simple, but sometimes you need more control. Graph Objects is the lower-level interface that lets you build custom, layered charts. This topic shows how to use it to create richer interactive visuals.

a. Plotly Express vs Graph Objects

Plotly Express creates a complete chart in one line using a tidy DataFrame. It is the best choice for most everyday charts. It builds a figure made of graph objects behind the scenes.

Graph Objects lets you build the same figure piece by piece. You create a Figure, add traces, and control every detail. It takes more code, but it allows custom designs that Express cannot easily produce.

A good approach is to start with Express and then fine-tune the result using update_traces and update_layout. Switch to Graph Objects when you need to combine different chart types or build unusual layouts.

b. Building Custom Charts with Traces

In Plotly, each series of data on a chart is called a trace. You add traces to a figure, and each one has its own type, data, and style.

import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scatter(x=df["month"], y=df["sales"],
mode="lines+markers", name="Sales"))
fig.add_trace(go.Scatter(x=df["month"], y=df["target"],
mode="lines", name="Target",
line=dict(dash="dash")))
fig.update_layout(title="Sales vs Target", xaxis_title="Month",
yaxis_title="Amount")
fig.show()

The mode option controls whether points, lines, or both are shown. Each trace can have its own color, width, and hover text.

c. Combining Multiple Chart Types

With Graph Objects, you can put different chart types in one figure, such as bars and a line. This is common for showing a volume and a rate together, like revenue and growth percentage.

from plotly.subplots import make_subplots
fig = make_subplots(specs=[[{"secondary_y": True}]])
fig.add_trace(go.Bar(x=df["month"], y=df["revenue"], name="Revenue"),
secondary_y=False)
fig.add_trace(go.Scatter(x=df["month"], y=df["growth"], name="Growth %"),
secondary_y=True)

The secondary_y setting adds a second vertical axis on the right. Use dual axes with care. They can mislead if the scales are chosen to make lines look connected when they are not. Label both axes clearly.

d. Adding Buttons, Sliders, and Dropdowns

Plotly lets you add controls directly inside a chart. Dropdown menus and buttons can switch between views, and sliders can move through values such as time.

fig.update_layout(
updatemenus=[dict(
buttons=[
dict(label="Sales", method="update",
args=[{"visible": [True, False]}]),
dict(label="Target", method="update",
args=[{"visible": [False, True]}]),
],
direction="down"
)]
)

These controls work without any server, so the chart can be shared as a simple HTML file. They are a nice way to give viewers a bit of exploration power.

Keep controls simple. If you need many filters and linked charts, it is time to build a full dashboard.


Data Visualization Tutorial

WR

Wesline Rinisha



Learn via Video Course

Data Visualization Using Python Logo

Data Visualization Using Python

19 hrs



Get in Touch For Details