-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Comments
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() It might not work with a lot of points but it should help many users :) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It would be useful to have a setting to exclude text labels from being graphed if they would overlap with other labels.

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

The text was updated successfully, but these errors were encountered: