How to Let Users Switch Between Multiple Metrics on a Dash Plotly Graph

How to Let Users Switch Between Multiple Metrics on a Dash Plotly Graph

Interactive dashboards are a core part of modern data storytelling. One common feature is allowing users to switch between different metrics (like revenue, conversion rate, or satisfaction score) directly on the graph.

But what happens when you wire everything up — and the graph crashes with a mysterious error saying your column name isn’t valid?

Let’s walk through the problem — and how to solve it.

The Use Case

Imagine you’ve built a line chart that visualizes trends over time for multiple categories (say product types or service regions). You want to toggle between metrics like:

  • Average rating
  • Total points
  • Net score

You add radio buttons so the user can pick which metric to view. Each metric is a column in your dataset.

The Common Pitfall

You hook up your Dash callback and pass the selected metric into the graph like this:

But then — boom 💥 — you get an error:

ValueError: Value of 'y' is not the name of a column in 'data_frame'.

You double-check your DataFrame. The columns exist. So why is it complaining?

The Real Issue

The problem is usually how your radio buttons are set up.

A lot of developers assign human-friendly labels and short variable names like this:

dcc.RadioItems(
    options=[
        {"label": "Average Score", "value": "avg_score"},
        {"label": "Total Points", "value": "pts"}
    ],
    value="avg_score"
)

But unless your DataFrame actually has a column called "avg_score" or "pts", Plotly Express will throw an error.

✅ The Solution

Make sure the value in your radio buttons exactly matches the column name in your DataFrame.

Like this:

dcc.RadioItems(
    options=[
        {"label": "Average Score", "value": "Score"},
        {"label": "Total Points", "value": "Points"}
    ],
    value="Score"
)

Now, when the user toggles between options, Dash will pass the correct column name directly to the plotting function — and everything works seamlessly.

🧪 Bonus Tip: Clean Label, Raw Value

You can still keep your labels clean for users — just make sure the value field lines up with your data.

🔁 Wrap-Up

If your dynamic y-axis in Dash isn’t working, check:

  • Are your dropdown or radio values matching real column names?
  • Is there a typo or case mismatch?
  • Are you passing the selected value directly into the y= parameter?

Small naming mismatches cause big frustrations — but once you align your UI values with your DataFrame columns, your graphs will update beautifully.