Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce group scatter attributes #6381

Merged
merged 6 commits into from Dec 21, 2022
Merged

introduce group scatter attributes #6381

merged 6 commits into from Dec 21, 2022

Conversation

archmoj
Copy link
Contributor

@archmoj archmoj commented Nov 30, 2022

Resolves #4913.
@plotly/plotly_js

@LiamConnors
Copy link
Contributor

@archmoj, I know we talked about group legend items that would avoid this, but I wonder if the scatter group going back to the centre when you click it on the legend is the right behaviour here and why it would differ to the behaviour for bar.

image

image

image

@archmoj
Copy link
Contributor Author

archmoj commented Dec 20, 2022

@LiamConnors now with the implementation of the alignmentgroup & offsetgroup for scatter trace, you could maintain initial positions. See 918068d as an example.

@alexcjohnson
Copy link
Contributor

@archmoj this looks great! There's one piece I don't understand: if I load zz-scatter-grouping-vs-defaults, then clear all the alignmentgroup attrs - Plotly.restyle(gd,'alignmentgroup',null) - then the traces in the upper right subplot disappear. They also disappear if I set the alignmentgroup for just those two traces to 'bottom' to match the ones below - Plotly.restyle(gd,'alignmentgroup','bottom', [3,9]).

If I also clear offsetgroup they come back, and if I set the offsetgroup for those two to something explicit they get their own group.

So I guess the odd case is when alignmentgroup is either all null or all the same but one trace, in a different subplot from the others, doesn't have an offsetgroup.

There's a related case that's clearly broken: in the same mock, if I just clear offsetgroup for the last trace it stays where it was. But if I first give it a new offsetgroup (so it correctly gets new space) and THEN clear it, it gets pushed into the next group!

Plotly.restyle(gd, 'offsetgroup', null, 11) // clear the group for the last trace, this is OK

Screenshot 2022-12-20 at 19 24 03

Plotly.restyle(gd, 'offsetgroup', '3', 11) // put it in a new group
Plotly.restyle(gd, 'offsetgroup', null, 11) // clear its group

Screenshot 2022-12-20 at 19 18 43

I don't think this is unique to scatter, I can get a similar history-dependent errors from bars from setting / clearing the offsetgroup of the FIRST bar in the bottom right subplot (ie replace the 11 with 4 in those commands). Maybe we just treat null as its own offsetgroup if there are other traces with an offsetgroup on the same axis?

Copy link
Contributor

@alexcjohnson alexcjohnson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💃 Actually, since the bug I identified - when there are some traces with offsetgroup and others without it - was already there with bars, I guess it's fine if we merge as is, and just make an issue to come back to this behavior later. Great work!

@archmoj archmoj merged commit 9014a11 into master Dec 21, 2022
@archmoj archmoj deleted the group-scattermode branch December 21, 2022 14:47
kMutagene added a commit to plotly/Plotly.NET that referenced this pull request Feb 13, 2023
@Braintelligence
Copy link

In case anyone stumbles upon this change and wonders why it doesn't work for them in a horizontal barchart: Make sure your scatter traces also have orientation: "h" or else it won't work. I only found this out while looking through all the comments surrounding this change; I believe it is not mentioned in the documentation at offsetgroup and there is also no example with horizontal orientation within the example made for the Python news article.

@LiamConnors
Copy link
Contributor

Thanks for the feedback @Braintelligence - just to clarify, is the following demonstrating the type of behaviour you are talking about?

I'll add an example to the docs to clarify using with orientation: "h"

import plotly.graph_objects as go
from plotly import data

df = data.tips()[data.tips()["day"] == "Sun"]

mean_values_df = df.groupby(by=["sex", "smoker"], as_index=False).mean(
    numeric_only=True
)

smoker_mean = mean_values_df[mean_values_df.smoker == "Yes"].sort_values(
    by="tip", ascending=False
)
non_smoker_mean = mean_values_df[mean_values_df.smoker == "No"].sort_values(
    by="tip", ascending=False
)

smoker = df[df.smoker == "Yes"].sort_values(by="tip", ascending=False)
non_smoker = df[df.smoker == "No"].sort_values(by="tip", ascending=False)

fig = go.Figure(
    layout=dict(
        xaxis=dict(categoryorder="category descending"),
        scattermode="group",
        legend=dict(groupclick="toggleitem"),
    )
)

fig.add_trace(
    go.Bar(
        orientation='h',
        x=smoker_mean.tip,
        y=smoker_mean.sex,
        name="Average",
        marker_color="IndianRed",
        offsetgroup="smoker",
        legendgroup="smoker",
        legendgrouptitle_text="Smoker",
    )
)


fig.add_trace(
    go.Scatter(
        orientation='h',
        x=smoker.tip,
        y=smoker.sex,
        mode="markers",
        name="Individual tips",
        marker=dict(color="LightSlateGrey", size=5),
        offsetgroup="smoker",
        legendgroup="smoker",
    )
)

fig.add_trace(
    go.Bar(
        orientation='h',
        x=non_smoker_mean.tip,
        y=non_smoker_mean.sex,
        name="Average",
        marker_color="LightSalmon",
        offsetgroup="non-smoker",
        legendgroup="non-smoker",
        legendgrouptitle_text="Non-Smoker",
    )
)


fig.add_trace(
    go.Scatter(
        orientation='h',
        x=non_smoker.tip,
        y=non_smoker.sex,
        mode="markers",
        name="Individual tips",
        marker=dict(color="LightSteelBlue", size=5),
        offsetgroup="non-smoker",
        legendgroup="non-smoker",
    )
)

fig.show()

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

Successfully merging this pull request may close these issues.

scattermode = group
4 participants