s c h e m a t i c s : c o o k b o o k

/ Cookbook.PlotExample

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Drawing and saving a PLoT? graph as an image

Problem

You may have plotted out a graph with the PLoT? package, and now want to save it off as an image.

Solution

The output of PLoT?'s PLOT is a subclass of an image-snip%, so all we really need to do is get its bitmap, and save that off to disk. For example:

(module performance mzscheme
  (provide (all-defined))
  (require (lib "plot.ss" "plot")
           (lib "list.ss")
           (lib "pregexp.ss")
           (lib "string.ss")
           (lib "class.ss"))
  
  (define data #<<EOF
300000:   113   184
301000:   113   184
302000:   112   184
303000:   118   187
304000:   117   185
305000:   113   187
306000:   113   187
307000:   113   186
308000:   113   208
309000:   125   195
310000:   114   188
311000:   107   202
EOF
    )
  (define extracted-data
    (map (lambda (line)
           (map string->number (rest (pregexp-match #px"(\\d+):\\s*(\\d+)\\s*(\\d+)" line))))
         (regexp-split "\n" data)))
  
  (define points1 
    (points (map (lambda (l) (vector (first l) (second l))) extracted-data)
            (sym 'circle) (color 'red)))
  (define points2 
    (points (map (lambda (l) (vector (first l) (third l))) extracted-data)
            (sym 'square) (color 'green)))
  
  
  ;; this is an image-snip
  (define graph (plot (mix points1 points2)
        (x-min 300000)
        (x-max 320000)
        (y-min 0)
        (y-max 300)))
  
  (define bitmap (send graph get-bitmap))

  (define (save filename)
    (send bitmap save-file filename 'png)))

Since these things are instances of objects, we need to know a little rudimentary OOP manipulation from class.ss.

Discussion

See image-snip% documentation for information on image-snips, and bitmap% save-file documentation for the SAVE-FILE documentation.


Comments about this recipe

Contributors

-- DannyYoo - 14 Dec 2006

CookbookForm
TopicType: Recipe
ParentTopic:
TopicOrder: 999

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest