question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Plotly Express labels dict not applying to traces generated with the color keyword

See original GitHub issue

I have a dataframe that I am using to generate an area chart with three components. These components are split out using the color keyword. Unfortunately, the labels keyword only renames the title of columns, not the names of traces generated by the color keyword. In the example below, it would be great to be able to replace steady, new_users and churn with more descriptive labels.

Of course, I can work around this by modifying the original data frame, but the labels dict is so elegant…

image

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
nicolaskruchtencommented, Jan 27, 2021

Right, the labels only operate on column names today, although I can see why for figures like this one, legend item text like “steady” could be thought of as a “label”. I might be open to a new keyword argument for this to remap values…

in the meantime, you can swap these out post-px like this:

import plotly.express as px

df = px.data.tips()

fig = px.scatter(df, x="total_bill", y="tip", color="day")
value_map = { "Sun": "Sunday",  "Sat": "Saturday" }
for trace in fig.data:
    trace.name = value_map.get(trace.name, trace.name)

# or the chainable function version
fig.for_each_trace(lambda t: t.update(name=value_map.get(t.name, t.name)))

fig.show()
0reactions
janoshcommented, Oct 14, 2022

While these strings come from column data, when shown in the legend, they should count as labels and be subject to px.defaults.labels imo.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Creating and updating figures in Python - Plotly
Over 28 examples of Creating and Updating Figures including changing color, size, log axes, and more in Python.
Read more >
plotly.express package — 5.11.0 documentation
The keys of this dict should correspond to column names, and the values should correspond to the desired label to be displayed. color_discrete_sequence...
Read more >
How to add traces in plotly.express - Stack Overflow
You can add traces using an Express plot by using .select_traces() . Something like: fig.add_traces( list(px.line(...).select_traces()) ).
Read more >
Construct various types of Bar Race Charts with Plotly
You should specify the color of the subcategory because if you have different color then the audience will not be able to interpret...
Read more >
st.plotly_chart - Streamlit Docs
st.plotly_chart displays an interactive Plotly chart. ... dict/list of plotly.graph_objs. ... import plotly.express as px import streamlit as st df ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found