2

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.

some3128
  • 1,430
  • 1
  • 2
  • 8

1 Answers1

2

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

Michael S.
  • 3,050
  • 4
  • 19
  • 34
  • This works just as needed, many thanks. I found that starting the code block with `display`, i.e. `display( so.Plot(...` also works. – some3128 Jul 25 '23 at 19:43