...

Source file src/gonum.org/v1/plot/plotter/invertedaxis_example_test.go

Documentation: gonum.org/v1/plot/plotter

     1  // Copyright ©2018 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package plotter_test
     6  
     7  import (
     8  	"image/color"
     9  	"log"
    10  	"math"
    11  
    12  	"gonum.org/v1/plot"
    13  	"gonum.org/v1/plot/plotter"
    14  	"gonum.org/v1/plot/vg"
    15  )
    16  
    17  func Example_invertedScale() {
    18  	// This example is nearly identical to the LogScale, other than
    19  	// both the X and Y axes are inverted. InvertedScale expects to act
    20  	// on another Normalizer - which should allow for more flexibility
    21  	p := plot.New()
    22  	p.Title.Text = "Example of inverted axes"
    23  	p.Y.Scale = plot.InvertedScale{Normalizer: plot.LogScale{}}
    24  	p.X.Scale = plot.InvertedScale{Normalizer: plot.LinearScale{}}
    25  	p.Y.Tick.Marker = plot.LogTicks{Prec: -1}
    26  	p.X.Label.Text = "x"
    27  	p.Y.Label.Text = "f(x)"
    28  
    29  	f := plotter.NewFunction(math.Exp)
    30  	f.XMin = 0.2
    31  	f.XMax = 10
    32  
    33  	f.Color = color.RGBA{R: 255, A: 255}
    34  
    35  	p.Add(f, plotter.NewGrid())
    36  	p.Legend.Add("exp(x)", f)
    37  
    38  	// Neither .Min nor .Max for the X and Y axes are 'swapped'.
    39  	// The minimal value is retained in .Min, and the maximal
    40  	// value stays in .Max.
    41  	p.X.Min = f.XMin
    42  	p.X.Max = f.XMax
    43  	p.Y.Min = math.Exp(f.XMin)
    44  	p.Y.Max = math.Exp(f.XMax)
    45  
    46  	err := p.Save(10*vg.Centimeter, 10*vg.Centimeter, "testdata/invertedlogscale.png")
    47  	if err != nil {
    48  		log.Panic(err)
    49  	}
    50  }
    51  

View as plain text