You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Plot.Save() makes it easy to save a plot to a file. However, often one wants to plot directly to an image.Image or an io.Writer. This is possible. The trick is to create your own draw.Canvas. The following examples illustrate.
Drawing to an image.Image
package main
import (
"image""image/png""os""gonum.org/v1/plot""gonum.org/v1/plot/plotter""gonum.org/v1/plot/vg/draw""gonum.org/v1/plot/vg/vgimg"
)
constdpi=96funcmain() {
p, err:=plot.New()
iferr!=nil {
panic(err)
}
l, err:=plotter.NewLine(plotter.XYs{{0, 0}, {1, 1}, {2, 2}})
iferr!=nil {
panic(err)
}
p.Add(l)
// Draw the plot to an in-memory image.img:=image.NewRGBA(image.Rect(0, 0, 3*dpi, 3*dpi))
c:=vgimg.NewWith(vgimg.UseImage(img))
p.Draw(draw.New(c))
// Save the image.f, err:=os.Create("test.png")
iferr!=nil {
panic(err)
}
iferr:=png.Encode(f, c.Image()); err!=nil {
panic(err)
}
iferr:=f.Close(); err!=nil {
panic(err)
}
}
Writing a plot to an io.Writer
package main
import (
"os""gonum.org/v1/plot""gonum.org/v1/plot/plotter""gonum.org/v1/plot/vg""gonum.org/v1/plot/vg/draw""gonum.org/v1/plot/vg/vgsvg"
)
constdpi=96funcmain() {
p, err:=plot.New()
iferr!=nil {
panic(err)
}
l, err:=plotter.NewLine(plotter.XYs{{0, 0}, {1, 1}, {2, 2}})
iferr!=nil {
panic(err)
}
p.Add(l)
// Create a Canvas for writing SVG images.c:=vgsvg.New(3*vg.Inch, 3*vg.Inch)
// Draw to the Canvas.p.Draw(draw.New(c))
// Write the Canvas to a io.Writer (in this case, os.Stdout).if_, err:=c.WriteTo(os.Stdout); err!=nil {
panic(err)
}
}