Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Consider removing column name and = sign in legend #36

Closed
mazzma12 opened this issue Mar 29, 2019 · 12 comments
Closed

Consider removing column name and = sign in legend #36

mazzma12 opened this issue Mar 29, 2019 · 12 comments

Comments

@mazzma12
Copy link

mazzma12 commented Mar 29, 2019

Hi again @nicolaskruchten,

Something that puzzled me about the color arg in the plot is that it includes the name of the column followed by an equal sign. Would you consider removing it and just let the value of the column in place? Here are a few reasons to support that point :

  • It is not displayed in plotly by default
  • It's redundant information when we see date= orcountry= it easy to infer by the value of the field
  • It takes some extra place, that is valuable, especially in the vertical orientation.

image

Also, for personal taste, I think equal sign does not play well outside equations and prefer : instead.

Happy to discuss with you about it. Maybe there is an option that can disable it, that I am not aware of.

@mazzma12 mazzma12 changed the title Consider removing attribute name and = sign in legend Consider removing column name and = sign in legend Mar 29, 2019
@nicolaskruchten
Copy link
Contributor

Thanks for the input!

This is the best compromise I could find at the moment for dealing with cases where in fact it's not easy to infer from the value (i.e. the values are "Yes" and "No" and the column is called "Smoker" etc) and because when you are using colours and symbols, you need to be able to differentiate which is which ("smoker=Yes, child=Yes" vs "Yes, Yes").

The best way to deal wit this would be to have separate legends for color, symbol, size and line-dash, with titles, but plotly.js doesn't (yet) support this kind of thing.

In terms of how to actually get the effect you want, unfortunately you'll have to iterate through the traces and change the names. We're looking at various ways to make that easier: plotly/plotly.py#1484

@mazzma12
Copy link
Author

mazzma12 commented Apr 5, 2019

Hi @nicolaskruchten eventually I found the time to reply!

This is the best compromise I could find at the moment for dealing with cases where in fact it's not easy to infer from the value (i.e. the values are "Yes" and "No" and the column is called "Smoker" etc) and because when you are using colours and symbols, you need to be able to differentiate which is which ("smoker=Yes, child=Yes" vs "Yes, Yes").

From my experience, such ambiguity would be limited as the graph would come with a title s.a evolution by smoking behaviour. But I hadn't realized you could have composed categories, in this case, I understand that might be tricky.

The best way to deal wit this would be to have separate legends for color, symbol, size and line-dash, with titles, but plotly.js doesn't (yet) support this kind of thing.
Do you mean having a title for each legend section like in theseaborn API for categorical data? I think that was the reference I had in mind when opening the issue, too bad!

@nicolaskruchten
Copy link
Contributor

With the newest release, you can now easily change this after the fact with something like:

.for_each_trace(lambda t: t.update(name=t.name.replace("=",": ")))

For example:

image

@nicolaskruchten
Copy link
Contributor

You could of course use this to edit any property etc.

@prateekshrivastav786
Copy link

If you just want to show the values in legend, you can use this:

fig = px.scatter(data_grouped_month, x="resolved_tf", y="alert_role",
                 size='incident_count',color='alert_role', hover_data=['alert_role']).for_each_trace(lambda t: t.update(name=t.name.split("=")[1]))

like if you have something like:

alert_role=cpu
alert_role=clone
alert_role=memory
alert_role=io wait time
alert_role= databse

Like:
Screenshot 2020-02-19 at 12 39 27

so the below snippet will remove the "alert_role" and will show only values like cpu, clone,memory etc:
.for_each_trace(lambda t: t.update(name=t.name.split("=")[1]))

The output will be:

cpu
clone
memory
io wait time
database

Screenshot 2020-02-19 at 12 31 58

@nicolaskruchten
Copy link
Contributor

This is actually no longer necessary... As of Plotly.py 4.5, Plotly Express no longer puts the = in trace names, because legends support titles.

@tconnor23
Copy link

tconnor23 commented Feb 21, 2020

Love the update to the legend in 4.5! Now I'm wondering if there's a way to supress the '=' expression from the titles of sub-plots. or if not, is there a helper function like there used to be for traces, something like:

for_each_subplot( lambda z: z.update(title=z.title[(z.title.find("=")+1):]))

i.e. want to remove the highlighted part
image

@nicolaskruchten
Copy link
Contributor

Yep:

import plotly.express as px

fig = px.scatter(px.data.tips(), x="total_bill", y="tip", facet_col="smoker")
fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))
fig.show()

@nicolaskruchten
Copy link
Contributor

@jdamiba can you add this example to the facet docs please? ☝️

@anuraghuram
Copy link

Similarly, is there a way to hide the facet_col appearing as a header above subplots?

@tabeacodes
Copy link

Hey, is there a way to set the x and y axis label for every subplot?

@KeithWM
Copy link

KeithWM commented Aug 7, 2023

Yep:

import plotly.express as px

fig = px.scatter(px.data.tips(), x="total_bill", y="tip", facet_col="smoker")
fig.for_each_annotation(lambda a: a.update(text=a.text.split("=")[-1]))
fig.show()

This is a working solution of course, but it seems odd for this hack to be necessary.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants