laurent.orseau
2022-1-18 13:31:21

Is there a way to display only the legend box from a plot?

Context: I have several plots with the same legends, I want to draw the legend outside of them and not be bother with calculating sizes, so I want to draw the legend independently


alexharsanyi
2022-1-18 13:41:01

No. This question came up in the past, and there is even an issue created for it, with a long comment section: https://github.com/racket/plot/issues/49


mflatt
2022-1-18 13:42:22

To confirm, these commits have been picked for v8.4


alexharsanyi
2022-1-18 13:48:41

There is also an unmerged pull request for this: https://github.com/racket/plot/pull/73, I provided reasons for rejecting it, however, it should be a clean merge, so you can merge it in your own copy of the plot package and experiment if the feature is suitable for what you want. If it is, and you can provide examples, I would be happy to reconsider and merge it in.


laurent.orseau
2022-1-18 14:05:04

Thank Alex, I’ll take a look. It’s not uncommon for me to have several plots to display side by side, with a common legend. Sometimes I can plot the legend inside one of the plots, but sometimes I want the legend at the left of the first plot, or above or below the plots, centered on the hc-append combined plots. The plot library currently has several issues for multiple plots: • x-min and friends include the ticks size, which means that the actual plotting area can vary from picture to picture (which I want to avoid, most of the time) • the legend is attached to a plot. I can’t share my actual use cases as they involve unsharable data, but here’s a mock-up:


laurent.orseau
2022-1-18 14:11:19

#lang racket (require plot) (for ([x-min '(0 100 1e5 1e10)] [x-max '(100 1000 1e6 1e11)] [i (in-naturals)]) (display (plot #:legend-anchor (if (= i 0) 'outside-top 'no-legend) #:width 300 #:x-min x-min #:x-max x-max (list (function sqrt #:label "√") (function (λ (x) (sqr (log x))) #:color 2 #:label "log²")))))


laurent.orseau
2022-1-18 14:12:20

There is also the issue of the overlapping ticks. This group of plots represents fairly well some of the actual situations I happen to face


laurent.orseau
2022-1-18 14:13:15

@ben same issues for you?


laurent.orseau
2022-1-18 14:15:49

Of course 'outside-left doesn’t work either, since it squashes the plot to the right


alexharsanyi
2022-1-18 14:15:51

alexharsanyi
2022-1-18 14:17:29

Yes, this is a limitation of the plot package. Unfortunately I don’t know how to solve it in an elegant way that addresses all cases. I have some ideas, but didn’t have time to try them out


alexharsanyi
2022-1-18 14:20:41

However, for your use case you could just manually create a legend using pict. It is actually not that complicated, I showed an example in one of the linked racket users threads. Not ideal, but might work


laurent.orseau
2022-1-18 14:23:15

That’s a possibility, but far from ideal indeed. Plot already offers a good way to draw the legend automatically; as a user I don’t want to have to redo that. (Please let me be clear that I’m not complaining against anyone, and certainly not you, who’s helping on your free time.)


laurent.orseau
2022-1-18 14:23:42

So would you consider the case above a good example justifying having something like the PR #49?


laurent.orseau
2022-1-18 14:25:03

Possibly one simple workaround would be to have non-plot drawer, that draws only the legend without the plotting area.


laurent.orseau
2022-1-18 14:25:32

Not perfect either unless plot is smart enough to not perform unnecessary computation is such cases


laurent.orseau
2022-1-18 14:40:11

I had a quick look at racket-users but couldn’t find your example. However, it seems that the renderer2d should have all the relevant information to draw a line with the provided style, in which case it may be easier than I thought.

But the renderer2d struct is not provided. The fields can be extracted with struct->vector but I don’t know what arguments they ask for


ben
2022-1-18 15:41:51

Yes Laurent, your code is similar to my use-case. I rarely use plot though, and almost always use plot-pict.


jbclements
2022-1-18 21:54:58

side note: Slack tags are not generally a reliable way to reach me.


alexharsanyi
2022-1-18 22:16:05

Hi @laurent.orseau, sorry for not replying earlier, I went to bed last night and stopped reading messages. Anyway, to address some of your questions:

• you can use the pull request I linked to generate the plots you want. You don’t have to share them with me — if you tell me that you used it and it worked for you, that is all I need. • the pull request I linked to already uses the renderer2d approach you suggested, you can study the code, if you want to try your own implementation • the message I mentioned for creating the legend separately is this one: https://groups.google.com/g/racket-users/c/QLfNmsluVHM/m/fTVCiCEKAgAJ


alexharsanyi
2022-1-18 22:17:00

Finally, the issues that I linked to contain a lot of discutions and code references about this topic. I really don’t want to repeat the same arguments, sorry.


samdphillips
2022-1-19 07:00:00

@popa.bogdanp, are observables in gui-easy thread-safe? If I obs-update! from a different thread than the gui it won’t cause problems?


popa.bogdanp
2022-1-19 07:56:50

Yup, they are