JetBrains / lets-plot

Multiplatform plotting library based on the Grammar of Graphics
https://lets-plot.org
MIT License
1.57k stars 51 forks source link

Tooltips overrunning the plot #1188

Open orausch opened 1 month ago

orausch commented 1 month ago

Similar to #1187, it's tricky to use plots with lots of tooltips. Unlike #1187 I think this case might require some API changes?

For example, maybe there some way to add padding around the plot such that the tooltips would display?

from lets_plot import *
LetsPlot.setup_html()

d = {
    l: [i] for i, l in enumerate(string.ascii_lowercase)
}
ggplot(d, aes(x='a', y='b')) + geom_point(tooltips=layer_tooltips(list(string.ascii_lowercase))) + ggsize(300, 200)
Screenshot 2024-09-16 at 8 21 35 AM
alshan commented 1 month ago

Hi, you can add padding around the plot panel (on top specifically) using the panel_inset parameter in theme(). But in this case you will have to increase the plot vertical size as well:

+ theme(panel_inset=[400, 0, 0]) + ggsize(600, 1000)

image

Similar effect can be achieve by setting wider y-scale limits or larger "extent".

However, the better solution in my opinion would be to customize the tooltip layout, for example:

value_format_list = [f'{k}=@{k}' for k in list(string.ascii_lowercase)]
format_str = ',\n'.join(', '.join(value_format_list[i:i+4]) for i in range(0, len(value_format_list), 4))
format_str

'a=@a, b=@b, c=@c, d=@d,\ne=@e, f=@f, g=@g, h=@h,\ni=@i, j=@j, k=@k, l=@l,\nm=@m, n=@n, o=@o, p=@p,\nq=@q, r=@r, s=@s, t=@t,\nu=@u, v=@v, w=@w, x=@x,\ny=@y, z=@z'

ggplot(d, aes(x='a', y='b')) + geom_point(tooltips=layer_tooltips().line(format_str))

image

orausch commented 1 month ago

Ah panel_inset seems great, thanks! However, it seems to interact weirdly with facet_wrap:

from lets_plot import *

LetsPlot.setup_html()

d = {
    l: [i]*3 for i, l in enumerate(string.ascii_lowercase)

} | {'facet': [0, 1, 2]}
ggplot(d, aes(x='a', y='b')) + geom_point(tooltips=layer_tooltips(list(string.ascii_lowercase)))  + theme(panel_inset=[400, 0, 0]) + ggsize(800, 1500) + facet_wrap("facet")

Some issues I see:

Screenshot 2024-09-16 at 1 31 29 PM

Any idea what's up with that?

alshan commented 1 month ago

'facet': [0, 1, 2] : the series size here should be the sane as in the data (i.e. 26)

Upd: maybe not, it depends on what you want to achieve. Y-axis labels seem wrong indeed.

alshan commented 1 month ago

Maybe better try scale_y_continuous(limits=..) or scale_y_continuous(expand=..) to have adequate empty space above the point.

orausch commented 1 month ago
Screenshot 2024-09-17 at 6 51 38 AM

expanding the limit doesn't work with this toy example (since I only have one datapoint 😛 ), but would probably be sufficient in real plots.

If #1187 were fixed that would also be sufficient since facets usually have enough space below

alshan commented 1 month ago

expanding the limit doesn't work with this toy example

To make it work you need to make plot larger (taller). But why don't you try to re-format the tooltip's content to make it more compact?

orausch commented 1 month ago

Yep to be clear that's a good suggestion and I've starting using that, thanks!

my tooltips are still pretty beefy despite that, but it definitely helps.