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

prevent scatter text overlap #925

Open
FilipMiscevic opened this issue Jan 24, 2018 · 2 comments
Open

prevent scatter text overlap #925

FilipMiscevic opened this issue Jan 24, 2018 · 2 comments
Labels
feature something new P3 not pressing

Comments

@FilipMiscevic
Copy link

It would be useful to have a setting to exclude text labels from being graphed if they would overlap with other labels.
newplot 13

For instance, ggplot2 in R has a check_overlap parameter - when true, it produces a graph like this:
hyp

@nicolaskruchten
Copy link
Contributor

See plotly/plotly.js#4674

@Karim-53
Copy link

One solution is to alternate the text position like in this live demo:

    import pandas as pd
    from plotly import express as px, graph_objects as go
    
    df = pd.DataFrame()
    df['x'] = [0, 1, 1, 2, 3, 6, 7, 7, 8, 8, 9, 9, 10, 11, 11, 12]
    df['y'] = [57, 55, 75, 23, 80, 66, 66, 23, 79, 79, 20, 71, 59, 74, 82, 77]
    df['explainer_name'] = ['tree_shap_approximation', 'saabas', 'tree_shap', 'baseline_random', 'archipelago',
                            'shapley_taylor_interaction', 'partition', 'anova', 'permutation_partition', 'permutation',
                            'shap_interaction', 'sage', 'maple', 'lime', 'kernel_shap', 'exact_shapley_values']
    
    fig = px.scatter(df,
                         x='x',
                         y='y',
                         # size='dot_size',
                         text='explainer_name',
                         # log_x=True,
                         labels={
                             "x": "Time",
                             "y": "Score",
                             # 'dot_size': 'Portability',
                             'explainer_name': 'Explainer '
                         },
                         title='No overlapping annotations',  # take some vertical space
                         )
    def improve_text_position(x):
        """ it is more efficient if the x values are sorted """
    positions = ['top center', 'bottom center']  # you can add more: left center ...
    return [positions[i % len(positions)] for i in range(len(x))]
    
    fig.update_traces(textposition=improve_text_position(df['x']))
    fig.show()

Before:
enter image description here

After:
enter image description here

It might not work with a lot of points but it should help many users :)

@gvwilson gvwilson self-assigned this Jun 10, 2024
@gvwilson gvwilson removed their assignment Aug 2, 2024
@gvwilson gvwilson added feature something new and removed ♥ NEEDS SPON$OR labels Aug 12, 2024
@gvwilson gvwilson changed the title Feature request: prevent scatter text overlap prevent scatter text overlap Aug 12, 2024
@gvwilson gvwilson added the P3 not pressing label Aug 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature something new P3 not pressing
Projects
None yet
Development

No branches or pull requests

5 participants