5

I have a recurring problem with legends on many of my plots with legends. The legend often obscures part of the data.

Reproducible example:

enter image description here

import seaborn.objects as so
import numpy as np
import pandas as pd
res_df = pd.DataFrame(
 {'value': np.random.rand(20),
 'cfg__status_during_transmit': ['OK']*5 + ['ERR']*5 + ['WARN']*5 + ['OTH']*5}
)

(
    so.Plot(res_df, y='value', x='cfg__status_during_transmit', color='cfg__status_during_transmit')
    .add(so.Dots(), so.Jitter(width=0.5))
    .layout(size=(8, 4))
).show()

I've tried elongating the plot. This is a hack and isn't convenient, especially since it makes everything a lot smaller.

I've also tried plotting using .on() onto a matplotlib figure or axis, but the same problem persists. There's a related SO issue which advises that the legend properties are still in development. I would appreciate suggestions for how to get around this problem.

Thanks.

2
  • 1
    Where do you want the legend? Outside the plot? Commented Jul 25, 2023 at 16:11
  • Yes outside, such that it doesn't overlap with the data points. Commented Jul 25, 2023 at 16:12

1 Answer 1

4

Control over the legend using seaborn.objects might still be a work in progress according to this post. However, if I used plot() instead of show() I got a figure where the legend was placed outside the graph:

import seaborn.objects as so
import numpy as np
import pandas as pd
res_df = pd.DataFrame(
 {'value': np.random.rand(20),
 'cfg__status_during_transmit': ['OK']*5 + ['ERR']*5 + ['WARN']*5 + ['OTH']*5}
)

(so.Plot(res_df, y='value', x='cfg__status_during_transmit', color='cfg__status_during_transmit')
 .add(so.Dots(), so.Jitter(width=0.5))
 .layout(size=(8, 4))
).plot()

Output: enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

This works just as needed, many thanks. I found that starting the code block with display, i.e. display( so.Plot(... also works.
I am using so.Plot(....).show() so that I am able to zoom and pan. With .plot(), this is not possible for me. Is there a way to have the legend outside, but still be able to zoom?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.