本文整理汇总了Golang中github.com/gonum/plot/vg/vgimg.PngCanvas.WriteTo方法的典型用法代码示例。如果您正苦于以下问题:Golang PngCanvas.WriteTo方法的具体用法?Golang PngCanvas.WriteTo怎么用?Golang PngCanvas.WriteTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/gonum/plot/vg/vgimg.PngCanvas
的用法示例。
在下文中一共展示了PngCanvas.WriteTo方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: PlotGraph
func PlotGraph(title string, timestamps []int64, w io.Writer) error {
p, err := plot.New()
if err != nil {
return err
}
p.Title.Text = title
p.X.Label.Text = "Time"
p.Y.Label.Text = "Number of stars"
p.Y.Min = 0
points := make(plotter.XYs, len(timestamps))
for i, timestamp := range timestamps {
points[i].X = float64(timestamp)
points[i].Y = float64(i + 1)
}
plotutil.AddLinePoints(p, "Stars", points)
c := vgimg.New(4*vg.Inch, 4*vg.Inch)
cpng := vgimg.PngCanvas{c}
p.Draw(draw.New(cpng))
if _, err := cpng.WriteTo(w); err != nil {
return err
}
return nil
}
示例2: TestTextGrobs
func TestTextGrobs(t *testing.T) {
// Output
file, err := os.Create("text.png")
if err != nil {
t.Fatalf("%", err)
}
pngCanvas := vgimg.PngCanvas{Canvas: vgimg.New(10*vg.Inch, 8*vg.Inch)}
vg.Initialize(pngCanvas)
allVP := Viewport{
X0: 0,
Y0: 0,
Width: 10 * vg.Inch,
Height: 8 * vg.Inch,
Canvas: pngCanvas,
}
bg := GrobRect{xmin: 0, ymin: 0, xmax: 1, ymax: 1, fill: BuiltinColors["gray60"]}
bg.Draw(allVP)
gridVP := allVP.Sub(0.1, 0.1, 0.35, 0.35)
drawTextGrid(gridVP, 0)
gridVP = allVP.Sub(0.55, 0.1, 0.35, 0.35)
drawTextGrid(gridVP, 45./180*math.Pi)
gridVP = allVP.Sub(0.1, 0.55, 0.35, 0.35)
drawTextGrid(gridVP, 135./180*math.Pi)
gridVP = allVP.Sub(0.55, 0.55, 0.35, 0.35)
drawTextGrid(gridVP, 90./180*math.Pi)
pngCanvas.WriteTo(file)
file.Close()
}
示例3: WritePNG
// WritePNG renders plot to a png file of size width x height.
func (p *Plot) WritePNG(filename string, width, height vg.Length) error {
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
canvas := vgimg.PngCanvas{Canvas: vgimg.New(width, height)}
canvas.Translate(-width/2, -height/2)
p.DumpTo(canvas, width, height)
canvas.WriteTo(file)
return nil
}
示例4: TestIndividualSteps
//.........这里部分代码省略.........
}
data = panel.Layers[2].Data
if data.N != 20 {
t.Errorf("Got %d data in label df.", panel.Layers[2].Data.N)
}
// StatBin produces bins
if fields := panel.Layers[3].Data.FieldNames(); !same(fields, []string{"x", "count", "ncount", "density", "ndensity"}) {
t.Errorf("Layer 3 %q has fields %v", panel.Layers[3].Name, fields)
}
data = panel.Layers[3].Data
if data.N != 11 {
t.Errorf("Got %d data in binned df.", panel.Layers[3].Data.N)
}
// 4. Wireing
panel.WireStatToGeom()
for a, s := range panel.Scales {
fmt.Printf("====== Scale %s %q ========\n", a, s.Name)
fmt.Printf("%s\n", s.String())
}
// 5. Test Construct ConstructGeoms. This shouldn't change much as
// GeomABLine doesn't reparametrize and we don't do position adjustments.
panel.ConstructGeoms()
// 6. FinalizeScales
panel.FinalizeScales()
// Only x and y are set up
if sx, ok := panel.Scales["x"]; !ok {
t.Errorf("Missing x scale")
} else {
if sx.Pos == nil {
t.Errorf("Missing Pos for x scale.")
}
if sx.DomainMin > 1.62 || sx.DomainMax < 1.95 {
t.Errorf("Bad training: %f %f", sx.DomainMin, sx.DomainMax)
}
}
fmt.Printf("%s\n", panel.Scales["x"])
fmt.Printf("%s\n", panel.Scales["y"])
// 7. Render Geoms to Grobs using scales (Step7).
panel.RenderGeoms()
fmt.Println("Layer 0, raw data")
for _, grob := range panel.Layers[0].Grobs {
fmt.Println(" ", grob.String())
}
fmt.Println("Layer 1, linear regression")
for _, grob := range panel.Layers[1].Grobs {
fmt.Println(" ", grob.String())
}
fmt.Println("Layer 2, labels")
for _, grob := range panel.Layers[2].Grobs {
fmt.Println(" ", grob.String())
}
fmt.Println("Layer 3, histogram")
for _, grob := range panel.Layers[3].Grobs {
fmt.Println(" ", grob.String())
}
// Output
pngCanvas := vgimg.PngCanvas{Canvas: vgimg.New(800, 600)}
pngCanvas.Translate(-400, -300)
vp := Viewport{
X0: 50,
Y0: 50,
Width: 700,
Height: 500,
Canvas: pngCanvas,
}
file, err := os.Create("example.png")
if err != nil {
t.Fatalf("%", err)
}
panel.Draw(vp, true, true)
if false {
fmt.Println("Layer 0, raw data")
for _, grob := range panel.Layers[0].Grobs {
grob.Draw(vp)
}
fmt.Println("Layer 1, linear regression")
for _, grob := range panel.Layers[1].Grobs {
grob.Draw(vp)
}
fmt.Println("Layer 2, labels")
for _, grob := range panel.Layers[2].Grobs {
grob.Draw(vp)
}
fmt.Println("Layer 3, histogram")
for _, grob := range panel.Layers[3].Grobs {
grob.Draw(vp)
}
}
pngCanvas.WriteTo(file)
file.Close()
}
示例5: drawPng
func drawPng(b *bytes.Buffer, p *plot.Plot, width, height float64) {
w, h := vg.Inches(width), vg.Inches(height)
c := vgimg.PngCanvas{Canvas: vgimg.New(w, h)}
p.Draw(plot.MakeDrawArea(c))
c.WriteTo(b)
}
示例6: run
//.........这里部分代码省略.........
)
p.X.Label.Text = "Iteration number"
p.Y.Label.Text = "Atomic mass of nuclei"
for i, n := range engine.Population {
line, err := plotter.NewLine(table[i])
if err != nil {
log.Fatalf(
"error adding data points for nucleus %v: %v\n",
n, err,
)
}
line.LineStyle.Color = col(n)
line.LineStyle.Width = vg.Points(1)
p.Add(line)
p.Legend.Add(label(n), line)
}
p.Add(plotter.NewGrid())
p.Legend.Top = true
p.Legend.XOffs = -1 * vg.Centimeter
figX := 25 * vg.Centimeter
figY := figX / vg.Length(math.Phi)
// Create a Canvas for writing SVG images.
canvas := vgsvg.New(figX, figY)
// Draw to the Canvas.
p.Draw(draw.New(canvas))
outsvg := new(bytes.Buffer)
_, err = canvas.WriteTo(outsvg)
if err != nil {
log.Printf("error svg: %v\n", err)
return
}
err = websocket.JSON.Send(c.ws, plotReply{
ID: id, Err: err, SVG: outsvg.String(), Stage: "plot-done",
})
if err != nil {
log.Printf("error sending data: %v\n", err)
return
}
pngcanvas := vgimg.PngCanvas{Canvas: vgimg.New(figX, figY)}
p.Draw(draw.New(pngcanvas))
outpng := new(bytes.Buffer)
_, err = pngcanvas.WriteTo(outpng)
if err != nil {
log.Printf("error png: %v\n", err)
return
}
href := filepath.Join(dir, fmt.Sprintf("output-%d.zip", id))
zipf, err := os.Create(href)
if err != nil {
log.Printf("error creating zip file: %v\n", err)
}
defer zipf.Close()
zipw := zip.NewWriter(zipf)
defer zipw.Close()
示例7: TestGraphicGrobs
//.........这里部分代码省略.........
size: 10,
color: BuiltinColors["black"],
vjust: 0.5,
hjust: 0.5,
}
points = append(points, g)
x += 3 * 0.021
}
for _, grob := range points {
grob.Draw(innerVP)
}
// Draw lines with different styles and widths.
lines := []Grob{}
x, y = 0.1, 0.45
for lt := SolidLine; lt <= TwodashLine; lt++ {
x = 0.1
for size := 1; size < 8; size += 2 {
g := GrobLine{
x0: x,
y0: y,
x1: x + 0.18,
y1: y,
size: float64(size),
linetype: lt,
color: BuiltinColors["black"],
}
lines = append(lines, g)
x += 0.22
}
y += 0.04
}
for _, grob := range lines {
grob.Draw(innerVP)
}
// Draw rectangles
rectVP := innerVP.Sub(0.1, 0.7, 0.4, 0.3)
rect := []Grob{}
bgr := GrobRect{xmin: 0, ymin: 0, xmax: 1, ymax: 1, fill: BuiltinColors["gray40"]}
bgr.Draw(rectVP)
x, y = 0.0, 0.0
w, h := 0.5, 0.5
for _, col := range cols {
g := GrobRect{
xmin: x,
ymin: y,
xmax: x + w,
ymax: y + h,
fill: BuiltinColors[col],
}
rect = append(rect, g)
x += w
y += h
w /= 2
h /= 2
}
for _, grob := range rect {
grob.Draw(rectVP)
}
// Draw path
pathVP := innerVP.Sub(0.55, 0.7, 0.4, 0.3)
bgp := GrobRect{xmin: 0, ymin: 0, xmax: 1, ymax: 1, fill: BuiltinColors["gray"]}
bgp.Draw(pathVP)
sin := make([]struct{ x, y float64 }, 50)
for i := range sin {
k := float64(i) / float64(len(sin)-1)
x = k * 2 * math.Pi
println(float64(i)/float64(len(sin)), x, math.Sin(x))
y = 0.4 * math.Sin(x)
sin[i].x = k
sin[i].y = 0.55 + y
}
g := GrobPath{
points: sin,
size: 4,
linetype: SolidLine,
color: BuiltinColors["white"],
}
g.Draw(pathVP)
cos := make([]struct{ x, y float64 }, 25)
for i := range cos {
k := float64(i) / float64(len(cos)-1)
x = k * 4 * math.Pi
y = 0.3 * math.Cos(x)
cos[i].x = k
cos[i].y = 0.45 + y
}
g = GrobPath{
points: cos,
size: 2,
linetype: DottedLine,
color: BuiltinColors["green"],
}
g.Draw(pathVP)
pngCanvas.WriteTo(file)
file.Close()
}