plot library: legend outside plot?

216 views
Skip to first unread message

Dmitry Pavlov

unread,
Nov 8, 2018, 10:18:14 AM11/8/18
to Racket Users
Hello,

Is it possible to render a plot with the legend outside the plotting
area, like gnuplot does with "set key outside" option?

I see only (plot-legend-anchor) parameter for placement of the legend in
different places inside the plot area.

Best regards,

Dmitry


Alex Harsanyi

unread,
Nov 9, 2018, 8:16:48 AM11/9/18
to Racket Users

It is not currently possible to draw the legend outside the plot area.  The code that draws the legend is at the link below, however, this code assumes that the position will be inside the plot area and will not reserve extra space outside this area:


Alex.

Alex Harsanyi

unread,
Nov 10, 2018, 6:45:15 PM11/10/18
to Racket Users

If you are willing to do some programming, you can write your own function to create the legend, as below:

    #lang racket
    (require plot pict racket/draw)

    ;; I could not find how to draw styled lines in pict, so I wrote my own
    ;; function....
    (define (hline1 w h color style)
      (define (draw dc x y)
        (define pen (send the-pen-list find-or-create-pen color h style))
        (define old-pen (send dc get-pen))
        (send dc set-pen pen)
        (send dc draw-line x (+ y (/ h 2)) (+ x w) (+ y (/ h 2)))
        (send dc set-pen old-pen))
      (dc draw w h))

    (define (legend)
      (define picts
        (list
         (hline1 30 2 "red" 'long-dash) (text "sin")
         (hline1 30 4 "blue" 'short-dash) (text "cos")))
      (frame
       (inset
        (vc-append
         15
         (text "Legend" 'default 14)
         (table 2 picts cc-superimpose cc-superimpose 10 10))
        5)))

    (define (the-plot)
      (plot-pict (list
           (function sin #:color "red" #:width 2 #:style 'long-dash)
           (function cos #:color "blue" #:width 4 #:style 'short-dash))
          #:x-min -5 #:x-max 5))

    (ht-append (legend) (the-plot))

Which produces:

loutside.png

You can than write the pict to a bitmap using `pict->bitmap`.  The code above will not produce interactive plot snips -- it is possible to do that as well, in my own application I have a floating legend that I can drag around the plot so I can see what is underneath it, but implementing that is a bit more complex.

The obvious disadvantage of this code is that you have to manually manage the legend entries or write higher level wrappers for the plot functions to generate both legend entries and plot renderers.  The big advantage is that you can style the legend however you want.

Best Regards,
Alex.

Dmitry Pavlov

unread,
Nov 11, 2018, 3:32:09 AM11/11/18
to Alex Harsanyi, Racket Users

Alex,

Thank you! I did not know about plot-pict.

Best regards,

Dmitry

--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages