Пример #1
0
func ExampleHeatMap() {
	m := unitGrid{mat64.NewDense(3, 4, []float64{
		1, 2, 3, 4,
		5, 6, 7, 8,
		9, 10, 11, 12,
	})}
	h := NewHeatMap(m, palette.Heat(12, 1))

	p, err := plot.New()
	if err != nil {
		log.Panic(err)
	}
	p.Title.Text = "Heat map"

	p.Add(h)

	p.X.Padding = 0
	p.Y.Padding = 0
	p.X.Max = 3.5
	p.Y.Max = 2.5

	err = p.Save(100, 100, "testdata/heatMap.png")
	if err != nil {
		log.Panic(err)
	}
}
Пример #2
0
func TestHeatMapWithContour(t *testing.T) {
	if !*visualDebug {
		return
	}
	m := unitGrid{mat64.NewDense(3, 4, []float64{
		2, 1, 4, 3,
		6, 7, 2, 5,
		9, 10, 11, 12,
	})}
	h := NewHeatMap(m, palette.Heat(12, 1))

	levels := []float64{1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5, 11.5}
	c := NewContour(m, levels, palette.Rainbow(10, palette.Blue, palette.Red, 1, 1, 1))
	c.LineStyles[0].Width *= 5

	plt, _ := plot.New()

	plt.Add(h)
	plt.Add(c)
	plt.Add(NewGlyphBoxes())

	plt.X.Padding = 0
	plt.Y.Padding = 0
	plt.X.Max = 3.5
	plt.Y.Max = 2.5
	plt.Save(7, 7, "heat.svg")
}
Пример #3
0
func TestIssue179(t *testing.T) {
	scatter, err := plotter.NewScatter(plotter.XYs{{1, 1}, {0, 1}, {0, 0}})
	if err != nil {
		log.Fatal(err)
	}
	p, err := plot.New()
	if err != nil {
		log.Fatal(err)
	}
	p.Add(scatter)
	p.HideAxes()

	c := vgimg.JpegCanvas{Canvas: vgimg.New(5.08*vg.Centimeter, 5.08*vg.Centimeter)}
	p.Draw(draw.New(c))
	b := bytes.NewBuffer([]byte{})
	if _, err = c.WriteTo(b); err != nil {
		t.Error(err)
	}

	f, err := os.Open(filepath.Join("testdata", "issue179.jpg"))
	if err != nil {
		t.Error(err)
	}
	defer f.Close()

	want, err := ioutil.ReadAll(f)
	if err != nil {
		t.Error(err)
	}
	if !bytes.Equal(b.Bytes(), want) {
		t.Error("Image mismatch")
	}
}
Пример #4
0
func TestComplexContours(t *testing.T) {
	if !*visualDebug {
		return
	}
	for _, n := range []float64{0, 1, 2, 4, 8, 16, 32} {
		data := make([]float64, 6400)
		for i := range data {
			r := float64(i/80) - 40
			c := float64(i%80) - 40

			data[i] = rand.NormFloat64()*n + math.Hypot(r, c)
		}

		m := unitGrid{mat64.NewDense(80, 80, data)}

		levels := []float64{-1, 3, 7, 9, 13, 15, 19, 23, 27, 31}
		c := NewContour(m, levels, palette.Rainbow(10, palette.Blue, palette.Red, 1, 1, 1))

		plt, _ := plot.New()
		plt.Add(c)

		plt.X.Padding = 0
		plt.Y.Padding = 0
		plt.X.Max = 79.5
		plt.Y.Max = 79.5
		plt.Save(7, 7, fmt.Sprintf("complex_contour-%v.svg", n))
	}
}
Пример #5
0
// Draw the plot logo.
func Example() {
	p, err := plot.New()
	if err != nil {
		log.Panic(err)
	}

	DefaultLineStyle.Width = vg.Points(1)
	DefaultGlyphStyle.Radius = vg.Points(3)

	p.Y.Tick.Marker = plot.ConstantTicks([]plot.Tick{
		{0, "0"}, {0.25, ""}, {0.5, "0.5"}, {0.75, ""}, {1, "1"},
	})
	p.X.Tick.Marker = plot.ConstantTicks([]plot.Tick{
		{0, "0"}, {0.25, ""}, {0.5, "0.5"}, {0.75, ""}, {1, "1"},
	})

	pts := XYs{{0, 0}, {0, 1}, {0.5, 1}, {0.5, 0.6}, {0, 0.6}}
	line, err := NewLine(pts)
	if err != nil {
		log.Panic(err)
	}
	scatter, err := NewScatter(pts)
	if err != nil {
		log.Panic(err)
	}
	p.Add(line, scatter)

	pts = XYs{{1, 0}, {0.75, 0}, {0.75, 0.75}}
	line, err = NewLine(pts)
	if err != nil {
		log.Panic(err)
	}
	scatter, err = NewScatter(pts)
	if err != nil {
		log.Panic(err)
	}
	p.Add(line, scatter)

	pts = XYs{{0.5, 0.5}, {1, 0.5}}
	line, err = NewLine(pts)
	if err != nil {
		log.Panic(err)
	}
	scatter, err = NewScatter(pts)
	if err != nil {
		log.Panic(err)
	}
	p.Add(line, scatter)

	err = p.Save(100, 100, "testdata/plotLogo.png")
	if err != nil {
		log.Panic(err)
	}
}
Пример #6
0
func main() {
	var levels []float64
	for l := 100.5; l < volcano.Matrix.(*mat64.Dense).Max(); l += 5 {
		levels = append(levels, l)
	}
	c := plotter.NewContour(volcano, levels, palette.Rainbow(len(levels), (palette.Yellow+palette.Red)/2, palette.Blue, 1, 1, 1))
	quarterStyle := draw.LineStyle{
		Color:  color.Black,
		Width:  vg.Points(0.5),
		Dashes: []vg.Length{0.2, 0.4},
	}
	halfStyle := draw.LineStyle{
		Color:  color.Black,
		Width:  vg.Points(0.5),
		Dashes: []vg.Length{5, 2, 1, 2},
	}
	c.LineStyles = append(c.LineStyles, quarterStyle, halfStyle, quarterStyle)

	h := plotter.NewHeatMap(volcano, palette.Heat(len(levels)*2, 1))

	p, err := plot.New()
	if err != nil {
		panic(err)
	}
	p.Title.Text = "Maunga Whau Volcano"

	p.Add(h)
	p.Add(c)

	p.X.Padding = 0
	p.Y.Padding = 0
	_, p.X.Max, _, p.Y.Max = h.DataRange()

	name := "example_volcano"

	for _, ext := range []string{
		".eps",
		".pdf",
		".svg",
		".png",
		".tiff",
		".jpg",
	} {
		if err := p.Save(4, 4, name+ext); err != nil {
			panic(err)
		}
	}
}
Пример #7
0
func lines(w vg.Length) (*plot.Plot, error) {
	p, err := plot.New()
	if err != nil {
		return nil, err
	}

	pts := plotter.XYs{{0, 0}, {0, 1}, {1, 0}, {1, 1}}
	line, err := plotter.NewLine(pts)
	line.Width = w
	if err != nil {
		return nil, err
	}
	p.Add(line)

	return p, nil
}
Пример #8
0
func main() {
	rand.Seed(0) // The default random seed is 1.

	p, err := plot.New()
	if err != nil {
		panic(err)
	}

	p.Title.Text = "Plotutil example"
	p.X.Label.Text = "X"
	p.Y.Label.Text = "Y"

	err = plotutil.AddLinePoints(
		p,
		"First", randomPoints(15),
	)
	if err != nil {
		panic(err)
	}

	cnvs, err := vgx11.New(4*96, 4*96, "Example")
	if err != nil {
		panic(err)
	}

	p.Draw(draw.New(cnvs))
	cnvs.Paint()
	time.Sleep(5 * time.Second)

	err = plotutil.AddLinePoints(
		p,
		"Second", randomPoints(15),
		"Third", randomPoints(15),
	)
	if err != nil {
		panic(err)
	}

	p.Draw(draw.New(cnvs))
	cnvs.Paint()
	time.Sleep(10 * time.Second)

	// Save the plot to a PNG file.
	//        if err := p.Save(4, 4, "points.png"); err != nil {
	//                panic(err)
	//        }
}
Пример #9
0
// An example of making a stacked area chart.
func Example_stackedAreaChart() *plot.Plot {
	p, err := plot.New()
	if err != nil {
		panic(err)
	}

	p.Title.Text = "Example: Software Version Comparison"
	p.X.Label.Text = "Date"
	p.Y.Label.Text = "Users (in thousands)"

	p.Legend.Top = true
	p.Legend.Left = true

	vals := []plotter.Values{
		plotter.Values{0.02, 0.015, 0, 0, 0, 0, 0},
		plotter.Values{0, 0.48, 0.36, 0.34, 0.32, 0.32, 0.28},
		plotter.Values{0, 0, 0.87, 1.4, 0.64, 0.32, 0.28},
		plotter.Values{0, 0, 0, 1.26, 0.34, 0.12, 0.09},
		plotter.Values{0, 0, 0, 0, 2.48, 2.68, 2.13},
		plotter.Values{0, 0, 0, 0, 0, 1.32, 0.54},
		plotter.Values{0, 0, 0, 0, 0, 0.68, 5.67},
	}

	err = plotutil.AddStackedAreaPlots(p, plotter.Values{2007, 2008, 2009, 2010, 2011, 2012, 2013},
		"Version 3.0",
		stackValues{vs: vals[0:7]},
		"Version 2.1",
		stackValues{vs: vals[0:6]},
		"Version 2.0.1",
		stackValues{vs: vals[0:5]},
		"Version 2.0",
		stackValues{vs: vals[0:4]},
		"Version 1.1",
		stackValues{vs: vals[0:3]},
		"Version 1.0",
		stackValues{vs: vals[0:2]},
		"Beta",
		stackValues{vs: vals[0:1]},
	)

	if err != nil {
		panic(err)
	}

	return p
}
Пример #10
0
func TestIssue179(t *testing.T) {
	t.Skip("github.com/BurntSushi/xgbutil#30 issue not fixed")

	if !hasX11() {
		t.Skip("no X11 environment")
	}

	scatter, err := plotter.NewScatter(plotter.XYs{{1, 1}, {0, 1}, {0, 0}})
	if err != nil {
		t.Fatalf("error: %v\n", err)
	}
	p, err := plot.New()
	if err != nil {
		t.Fatalf("error: %v\n", err)
	}
	p.Add(scatter)
	p.HideAxes()

	c, err := New(5.08*vg.Centimeter, 5.08*vg.Centimeter, "test issue179")
	if err != nil {
		t.Fatalf("error: %v\n", err)
	}

	p.Draw(draw.New(c))
	b := bytes.NewBuffer([]byte{})
	err = jpeg.Encode(b, c.ximg, nil)
	if err != nil {
		t.Error(err)
	}

	f, err := os.Open(filepath.Join("..", "vgimg", "testdata", "issue179.jpg"))
	if err != nil {
		t.Error(err)
	}
	defer f.Close()

	want, err := ioutil.ReadAll(f)
	if err != nil {
		t.Error(err)
	}
	if !bytes.Equal(b.Bytes(), want) {
		t.Error("Image mismatch")
	}
}
Пример #11
0
func ExampleErrorPoints() {
	// Get some random data.
	n, m := 5, 10
	pts := make([]plotter.XYer, n)
	for i := range pts {
		xys := make(plotter.XYs, m)
		pts[i] = xys
		center := float64(i)
		for j := range xys {
			xys[j].X = center + (rand.Float64() - 0.5)
			xys[j].Y = center + (rand.Float64() - 0.5)
		}
	}

	plt, err := plot.New()
	if err != nil {
		panic(err)
	}

	mean95, err := NewErrorPoints(MeanAndConf95, pts...)
	if err != nil {
		panic(err)
	}
	medMinMax, err := NewErrorPoints(MedianAndMinMax, pts...)
	if err != nil {
		panic(err)
	}
	err = AddLinePoints(plt,
		"mean and 95% confidence", mean95,
		"median and minimum and maximum", medMinMax)
	if err != nil {
		panic(err)
	}
	if err := AddErrorBars(plt, mean95, medMinMax); err != nil {
		panic(err)
	}
	if err := AddScatters(plt, pts[0], pts[1], pts[2], pts[3], pts[4]); err != nil {
		panic(err)
	}

	plt.Save(4, 4, "centroids.png")
}
Пример #12
0
func ExampleBubbles() {
	// randomTriples returns some random x, y, z triples
	// with some interesting kind of trend.
	randomTriples := func(n int) XYZs {
		data := make(XYZs, n)
		for i := range data {
			if i == 0 {
				data[i].X = rand.Float64()
			} else {
				data[i].X = data[i-1].X + 2*rand.Float64()
			}
			data[i].Y = data[i].X + 10*rand.Float64()
			data[i].Z = data[i].X
		}
		return data
	}

	n := 10
	bubbleData := randomTriples(n)

	p, err := plot.New()
	if err != nil {
		log.Panic(err)
	}
	p.Title.Text = "Bubbles"
	p.X.Label.Text = "X"
	p.Y.Label.Text = "Y"

	bs, err := NewBubbles(bubbleData, vg.Points(1), vg.Points(20))
	if err != nil {
		log.Panic(err)
	}
	bs.Color = color.RGBA{R: 196, B: 128, A: 255}
	p.Add(bs)

	err = p.Save(200, 200, "testdata/bubbles.png")
	if err != nil {
		log.Panic(err)
	}
}
Пример #13
0
// ExampleFunction draws some functions.
func ExampleFunction() {
	quad := NewFunction(func(x float64) float64 { return x * x })
	quad.Color = color.RGBA{B: 255, A: 255}

	exp := NewFunction(func(x float64) float64 { return math.Pow(2, x) })
	exp.Dashes = []vg.Length{vg.Points(2), vg.Points(2)}
	exp.Width = vg.Points(2)
	exp.Color = color.RGBA{G: 255, A: 255}

	sin := NewFunction(func(x float64) float64 { return 10*math.Sin(x) + 50 })
	sin.Dashes = []vg.Length{vg.Points(4), vg.Points(5)}
	sin.Width = vg.Points(4)
	sin.Color = color.RGBA{R: 255, A: 255}

	p, err := plot.New()
	if err != nil {
		log.Panic(err)
	}

	p.Title.Text = "Functions"
	p.X.Label.Text = "X"
	p.Y.Label.Text = "Y"

	p.Add(quad, exp, sin)
	p.Legend.Add("x^2", quad)
	p.Legend.Add("2^x", exp)
	p.Legend.Add("10*sin(x)+50", sin)
	p.Legend.ThumbnailWidth = 0.5 * vg.Inch

	p.X.Min = 0
	p.X.Max = 10
	p.Y.Min = 0
	p.Y.Max = 100

	err = p.Save(200, 200, "testdata/functions.png")
	if err != nil {
		log.Panic(err)
	}
}
Пример #14
0
// An example of making a histogram.
func ExampleHistogram() {
	// stdNorm returns the probability of drawing a
	// value from a standard normal distribution.
	stdNorm := func(x float64) float64 {
		const sigma = 1.0
		const mu = 0.0
		const root2π = 2.50662827459517818309
		return 1.0 / (sigma * root2π) * math.Exp(-((x-mu)*(x-mu))/(2*sigma*sigma))
	}

	n := 10000
	vals := make(Values, n)
	for i := 0; i < n; i++ {
		vals[i] = rand.NormFloat64()
	}

	p, err := plot.New()
	if err != nil {
		log.Panic(err)
	}
	p.Title.Text = "Histogram"
	h, err := NewHist(vals, 16)
	if err != nil {
		log.Panic(err)
	}
	h.Normalize(1)
	p.Add(h)

	// The normal distribution function
	norm := NewFunction(stdNorm)
	norm.Color = color.RGBA{R: 255, A: 255}
	norm.Width = vg.Points(2)
	p.Add(norm)

	err = p.Save(200, 200, "testdata/histogram.png")
	if err != nil {
		log.Panic(err)
	}
}
Пример #15
0
func ExampleBarChart() {
	// Create the plot values and labels.
	values := Values{0.5, 10, 20, 30}
	verticalLabels := []string{"A", "B", "C", "D"}
	horizontalLabels := []string{"Label A", "Label B", "Label C", "Label D"}

	// Create a vertical BarChart
	p1, err := plot.New()
	if err != nil {
		log.Panic(err)
	}
	verticalBarChart, err := NewBarChart(values, 0.5*vg.Centimeter)
	if err != nil {
		log.Panic(err)
	}
	p1.Add(verticalBarChart)
	p1.NominalX(verticalLabels...)
	err = p1.Save(100, 100, "testdata/verticalBarChart.png")
	if err != nil {
		log.Panic(err)
	}

	// Create a horizontal BarChart
	p2, err := plot.New()
	if err != nil {
		log.Panic(err)
	}
	horizontalBarChart, err := NewBarChart(values, 0.5*vg.Centimeter)
	horizontalBarChart.Horizontal = true // Specify a horizontal BarChart.
	if err != nil {
		log.Panic(err)
	}
	p2.Add(horizontalBarChart)
	p2.NominalY(horizontalLabels...)
	err = p2.Save(100, 100, "testdata/horizontalBarChart.png")
	if err != nil {
		log.Panic(err)
	}

	// Now, make a different type of BarChart.
	groupA := Values{20, 35, 30, 35, 27}
	groupB := Values{25, 32, 34, 20, 25}
	groupC := Values{12, 28, 15, 21, 8}
	groupD := Values{30, 42, 6, 9, 12}

	p, err := plot.New()
	if err != nil {
		log.Panic(err)
	}
	p.Title.Text = "Bar chart"
	p.Y.Label.Text = "Heights"

	w := vg.Points(8)

	barsA, err := NewBarChart(groupA, w)
	if err != nil {
		log.Panic(err)
	}
	barsA.Color = color.RGBA{R: 255, A: 255}
	barsA.Offset = -w / 2

	barsB, err := NewBarChart(groupB, w)
	if err != nil {
		log.Panic(err)
	}
	barsB.Color = color.RGBA{R: 196, G: 196, A: 255}
	barsB.Offset = w / 2

	barsC, err := NewBarChart(groupC, w)
	if err != nil {
		log.Panic(err)
	}
	barsC.XMin = 6
	barsC.Color = color.RGBA{B: 255, A: 255}
	barsC.Offset = -w / 2

	barsD, err := NewBarChart(groupD, w)
	if err != nil {
		log.Panic(err)
	}
	barsD.Color = color.RGBA{B: 255, R: 255, A: 255}
	barsD.XMin = 6
	barsD.Offset = w / 2

	p.Add(barsA, barsB, barsC, barsD)
	p.Legend.Add("A", barsA)
	p.Legend.Add("B", barsB)
	p.Legend.Add("C", barsC)
	p.Legend.Add("D", barsD)
	p.Legend.Top = true
	p.NominalX("Zero", "One", "Two", "Three", "Four", "",
		"Six", "Seven", "Eight", "Nine", "Ten")

	p.Add(NewGlyphBoxes())
	err = p.Save(300, 250, "testdata/barChart2.png")
	if err != nil {
		log.Panic(err)
	}

	// Now, make a stacked BarChart.
	p, err = plot.New()
	if err != nil {
		log.Panic(err)
	}
	p.Title.Text = "Bar chart"
	p.Y.Label.Text = "Heights"

	w = vg.Points(15)

	barsA, err = NewBarChart(groupA, w)
	if err != nil {
		log.Panic(err)
	}
	barsA.Color = color.RGBA{R: 255, A: 255}
	barsA.Offset = -w / 2

	barsB, err = NewBarChart(groupB, w)
	if err != nil {
		log.Panic(err)
	}
	barsB.Color = color.RGBA{R: 196, G: 196, A: 255}
	barsB.StackOn(barsA)

	barsC, err = NewBarChart(groupC, w)
	if err != nil {
		log.Panic(err)
	}
	barsC.Offset = w / 2
	barsC.Color = color.RGBA{B: 255, A: 255}

	barsD, err = NewBarChart(groupD, w)
	if err != nil {
		log.Panic(err)
	}
	barsD.StackOn(barsC)
	barsD.Color = color.RGBA{B: 255, R: 255, A: 255}

	p.Add(barsA, barsB, barsC, barsD)
	p.Legend.Add("A", barsA)
	p.Legend.Add("B", barsB)
	p.Legend.Add("C", barsC)
	p.Legend.Add("D", barsD)
	p.Legend.Top = true
	p.NominalX("Zero", "One", "Two", "Three", "Four", "",
		"Six", "Seven", "Eight", "Nine", "Ten")

	p.Add(NewGlyphBoxes())
	err = p.Save(250, 250, "testdata/stackedBarChart.png")
	if err != nil {
		log.Panic(err)
	}
}
Пример #16
0
// ExampleErrors draws points and error bars.
func ExampleErrors() {
	randomError := func(n int) Errors {
		err := make(Errors, n)
		for i := range err {
			err[i].Low = rand.Float64()
			err[i].High = rand.Float64()
		}
		return err
	}
	// randomPoints returns some random x, y points
	// with some interesting kind of trend.
	randomPoints := func(n int) XYs {
		pts := make(XYs, n)
		for i := range pts {
			if i == 0 {
				pts[i].X = rand.Float64()
			} else {
				pts[i].X = pts[i-1].X + rand.Float64()
			}
			pts[i].Y = pts[i].X + 10*rand.Float64()
		}
		return pts
	}

	type errPoints struct {
		XYs
		YErrors
		XErrors
	}

	n := 15
	data := errPoints{
		XYs:     randomPoints(n),
		YErrors: YErrors(randomError(n)),
		XErrors: XErrors(randomError(n)),
	}

	p, err := plot.New()
	if err != nil {
		log.Panic(err)
	}
	scatter, err := NewScatter(data)
	if err != nil {
		log.Panic(err)
	}
	scatter.Shape = draw.CrossGlyph{}
	xerrs, err := NewXErrorBars(data)
	if err != nil {
		log.Panic(err)
	}
	yerrs, err := NewYErrorBars(data)
	if err != nil {
		log.Panic(err)
	}
	p.Add(scatter, xerrs, yerrs)

	err = p.Save(200, 200, "testdata/errorBars.png")
	if err != nil {
		log.Panic(err)
	}
}
Пример #17
0
func ExampleQuartPlot() {
	// Create the example data.
	n := 100
	uniform := make(Values, n)
	normal := make(Values, n)
	expon := make(Values, n)
	for i := 0; i < n; i++ {
		uniform[i] = rand.Float64()
		normal[i] = rand.NormFloat64()
		expon[i] = rand.ExpFloat64()
	}

	// Create the QuartPlots
	qp1, err := NewQuartPlot(0, uniform)
	if err != nil {
		log.Panic(err)
	}
	qp2, err := NewQuartPlot(1, normal)
	if err != nil {
		log.Panic(err)
	}
	qp3, err := NewQuartPlot(2, expon)
	if err != nil {
		log.Panic(err)
	}

	// Create a vertical plot
	p1, err := plot.New()
	if err != nil {
		log.Panic(err)
	}
	p1.Title.Text = "Quartile Plot"
	p1.Y.Label.Text = "plotter.Values"
	p1.Add(qp1, qp2, qp3)

	// Set the X axis of the plot to nominal with
	// the given names for x=0, x=1 and x=2.
	p1.NominalX("Uniform\nDistribution", "Normal\nDistribution",
		"Exponential\nDistribution")

	err = p1.Save(200, 200, "testdata/verticalQuartPlot.png")
	if err != nil {
		log.Panic(err)
	}

	// Create a horizontal plot
	qp1.Horizontal = true
	qp2.Horizontal = true
	qp3.Horizontal = true

	p2, err := plot.New()
	if err != nil {
		log.Panic(err)
	}
	p2.Title.Text = "Quartile Plot"
	p2.X.Label.Text = "plotter.Values"
	p2.Add(qp1, qp2, qp3)

	// Set the Y axis of the plot to nominal with
	// the given names for y=0, y=1 and y=2.
	p2.NominalY("Uniform\nDistribution", "Normal\nDistribution",
		"Exponential\nDistribution")

	err = p2.Save(200, 200, "testdata/horizontalQuartPlot.png")
	if err != nil {
		log.Panic(err)
	}

	// Now, create a grouped quartile plot.

	p3, err := plot.New()
	if err != nil {
		log.Panic(err)
	}
	p3.Title.Text = "Box Plot"
	p3.Y.Label.Text = "plotter.Values"

	w := vg.Points(10)
	for x := 0.0; x < 3.0; x++ {
		b0, err := NewQuartPlot(x, uniform)
		if err != nil {
			log.Panic(err)
		}
		b0.Offset = -w
		b1, err := NewQuartPlot(x, normal)
		if err != nil {
			log.Panic(err)
		}
		b2, err := NewQuartPlot(x, expon)
		if err != nil {
			log.Panic(err)
		}
		b2.Offset = w
		p3.Add(b0, b1, b2)
	}
	p3.Add(NewGlyphBoxes())

	p3.NominalX("Group 0", "Group 1", "Group 2")

	err = p3.Save(200, 200, "testdata/groupedQuartPlot.png")
	if err != nil {
		log.Panic(err)
	}
}
Пример #18
0
func TestPersistency(t *testing.T) {
	// Get some random points
	rand.Seed(0) // The default random seed is 1.
	n := 15
	scatterData := randomPoints(n)
	lineData := randomPoints(n)
	linePointsData := randomPoints(n)

	p, err := plot.New()
	if err != nil {
		t.Fatalf("error creating plot: %v\n", err)
	}

	p.Title.Text = "Plot Example"
	p.X.Label.Text = "X"
	p.Y.Label.Text = "Y"
	// Use a custom tick marker function that computes the default
	// tick marks and re-labels the major ticks with commas.
	p.Y.Tick.Marker = commaTicks{}

	// Draw a grid behind the data
	p.Add(plotter.NewGrid())
	// Make a scatter plotter and set its style.
	s, err := plotter.NewScatter(scatterData)
	if err != nil {
		panic(err)
	}
	s.GlyphStyle.Color = color.RGBA{R: 255, B: 128, A: 255}

	// Make a line plotter and set its style.
	l, err := plotter.NewLine(lineData)
	if err != nil {
		panic(err)
	}
	l.LineStyle.Width = vg.Points(1)
	l.LineStyle.Dashes = []vg.Length{vg.Points(5), vg.Points(5)}
	l.LineStyle.Color = color.RGBA{B: 255, A: 255}

	// Make a line plotter with points and set its style.
	lpLine, lpPoints, err := plotter.NewLinePoints(linePointsData)
	if err != nil {
		panic(err)
	}
	lpLine.Color = color.RGBA{G: 255, A: 255}
	lpPoints.Shape = draw.PyramidGlyph{}
	lpPoints.Color = color.RGBA{R: 255, A: 255}

	// Add the plotters to the plot, with a legend
	// entry for each
	p.Add(s, l, lpLine, lpPoints)
	p.Legend.Add("scatter", s)
	p.Legend.Add("line", l)
	p.Legend.Add("line points", lpLine, lpPoints)

	// Save the plot to a PNG file.
	err = p.Save(4, 4, "test-persistency.png")
	if err != nil {
		t.Fatalf("error saving to PNG: %v\n", err)
	}
	defer os.Remove("test-persistency.png")

	buf := new(bytes.Buffer)
	enc := gob.NewEncoder(buf)
	err = enc.Encode(p)
	if err != nil {
		t.Fatalf("error gob-encoding plot: %v\n", err)
	}

	// TODO(sbinet): impl. BinaryMarshal for plot.Plot and vg.Font
	// {
	// 	dec := gob.NewDecoder(buf)
	// 	var p plot.Plot
	// 	err = dec.Decode(&p)
	// 	if err != nil {
	// 		t.Fatalf("error gob-decoding plot: %v\n", err)
	// 	}
	// 	// Save the plot to a PNG file.
	// 	err = p.Save(4, 4, "test-persistency-readback.png")
	// 	if err != nil {
	// 		t.Fatalf("error saving to PNG: %v\n", err)
	// 	}
	//  defer os.Remove("test-persistency-readback.png")
	// }

}
Пример #19
0
func ExampleBoxPlot() {
	// Create the sample data.
	n := 100
	uniform := make(ValueLabels, n)
	normal := make(ValueLabels, n)
	expon := make(ValueLabels, n)
	for i := 0; i < n; i++ {
		uniform[i].Value = rand.Float64()
		uniform[i].Label = fmt.Sprintf("%4.4f", uniform[i].Value)
		normal[i].Value = rand.NormFloat64()
		normal[i].Label = fmt.Sprintf("%4.4f", normal[i].Value)
		expon[i].Value = rand.ExpFloat64()
		expon[i].Label = fmt.Sprintf("%4.4f", expon[i].Value)
	}

	// Make boxes for our data and add them to the plot.
	uniBox, err := NewBoxPlot(vg.Points(20), 0, uniform)
	if err != nil {
		log.Panic(err)
	}
	normBox, err := NewBoxPlot(vg.Points(20), 1, normal)
	if err != nil {
		log.Panic(err)
	}
	expBox, err := NewBoxPlot(vg.Points(20), 2, expon)
	if err != nil {
		log.Panic(err)
	}

	// Make a vertical box plot.
	uniLabels, err := uniBox.OutsideLabels(uniform)
	if err != nil {
		log.Panic(err)
	}
	normLabels, err := normBox.OutsideLabels(normal)
	if err != nil {
		log.Panic(err)
	}
	expLabels, err := expBox.OutsideLabels(expon)
	if err != nil {
		log.Panic(err)
	}

	p1, err := plot.New()
	if err != nil {
		log.Panic(err)
	}
	p1.Title.Text = "Vertical Box Plot"
	p1.Y.Label.Text = "plotter.Values"
	p1.Add(uniBox, uniLabels, normBox, normLabels, expBox, expLabels)

	// Set the X axis of the plot to nominal with
	// the given names for x=0, x=1 and x=2.
	p1.NominalX("Uniform\nDistribution", "Normal\nDistribution",
		"Exponential\nDistribution")

	err = p1.Save(200, 200, "testdata/verticalBoxPlot.png")
	if err != nil {
		log.Panic(err)
	}

	// Now, make the same plot but horizontal.
	normBox.Horizontal = true
	expBox.Horizontal = true
	uniBox.Horizontal = true
	// We can use the same plotters but the labels need to be recreated.
	uniLabels, err = uniBox.OutsideLabels(uniform)
	if err != nil {
		log.Panic(err)
	}
	normLabels, err = normBox.OutsideLabels(normal)
	if err != nil {
		log.Panic(err)
	}
	expLabels, err = expBox.OutsideLabels(expon)
	if err != nil {
		log.Panic(err)
	}

	p2, err := plot.New()
	if err != nil {
		log.Panic(err)
	}
	p2.Title.Text = "Horizontal Box Plot"
	p2.X.Label.Text = "plotter.Values"

	p2.Add(uniBox, uniLabels, normBox, normLabels, expBox, expLabels)

	// Set the Y axis of the plot to nominal with
	// the given names for y=0, y=1 and y=2.
	p2.NominalY("Uniform\nDistribution", "Normal\nDistribution",
		"Exponential\nDistribution")

	err = p2.Save(200, 200, "testdata/horizontalBoxPlot.png")
	if err != nil {
		log.Panic(err)
	}

	// Now, make a grouped box plot.
	p3, err := plot.New()
	if err != nil {
		log.Panic(err)
	}
	p3.Title.Text = "Box Plot"
	p3.Y.Label.Text = "plotter.Values"

	w := vg.Points(20)
	for x := 0.0; x < 3.0; x++ {
		b0, err := NewBoxPlot(w, x, uniform)
		if err != nil {
			log.Panic(err)
		}
		b0.Offset = -w - vg.Points(3)
		b1, err := NewBoxPlot(w, x, normal)
		if err != nil {
			log.Panic(err)
		}
		b2, err := NewBoxPlot(w, x, expon)
		if err != nil {
			log.Panic(err)
		}
		b2.Offset = w + vg.Points(3)
		p3.Add(b0, b1, b2)
	}
	// Add a GlyphBox plotter for debugging.
	p3.Add(NewGlyphBoxes())

	// Set the X axis of the plot to nominal with
	// the given names for x=0, x=1 and x=2.
	p3.NominalX("Group 0", "Group 1", "Group 2")
	err = p3.Save(300, 300, "testdata/groupedBoxPlot.png")
	if err != nil {
		log.Panic(err)
	}
}
Пример #20
0
// ExampleScatter draws some scatter points, a line,
// and a line with points.
func ExampleScatter() {
	// randomPoints returns some random x, y points
	// with some interesting kind of trend.
	randomPoints := func(n int) XYs {
		pts := make(XYs, n)
		for i := range pts {
			if i == 0 {
				pts[i].X = rand.Float64()
			} else {
				pts[i].X = pts[i-1].X + rand.Float64()
			}
			pts[i].Y = pts[i].X + 10*rand.Float64()
		}
		return pts
	}

	n := 15
	scatterData := randomPoints(n)
	lineData := randomPoints(n)
	linePointsData := randomPoints(n)

	p, err := plot.New()
	if err != nil {
		log.Panic(err)
	}
	p.Title.Text = "Points Example"
	p.X.Label.Text = "X"
	p.Y.Label.Text = "Y"
	p.Add(NewGrid())

	s, err := NewScatter(scatterData)
	if err != nil {
		log.Panic(err)
	}
	s.GlyphStyle.Color = color.RGBA{R: 255, B: 128, A: 255}
	s.GlyphStyle.Radius = vg.Points(3)

	l, err := NewLine(lineData)
	if err != nil {
		log.Panic(err)
	}
	l.LineStyle.Width = vg.Points(1)
	l.LineStyle.Dashes = []vg.Length{vg.Points(5), vg.Points(5)}
	l.LineStyle.Color = color.RGBA{B: 255, A: 255}

	lpLine, lpPoints, err := NewLinePoints(linePointsData)
	if err != nil {
		log.Panic(err)
	}
	lpLine.Color = color.RGBA{G: 255, A: 255}
	lpPoints.Shape = draw.CircleGlyph{}
	lpPoints.Color = color.RGBA{R: 255, A: 255}

	p.Add(s, l, lpLine, lpPoints)
	p.Legend.Add("scatter", s)
	p.Legend.Add("line", l)
	p.Legend.Add("line points", lpLine, lpPoints)

	err = p.Save(200, 200, "testdata/scatter.png")
	if err != nil {
		log.Panic(err)
	}
}