go-hep.org/x/hep@v0.38.1/hplot/ticks_example_test.go (about)

     1  // Copyright ©2020 The go-hep 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 hplot_test
     6  
     7  import (
     8  	"image/color"
     9  	"log"
    10  	"time"
    11  
    12  	"git.sr.ht/~sbinet/epok"
    13  	"go-hep.org/x/hep/hplot"
    14  	"gonum.org/v1/plot/plotter"
    15  	"gonum.org/v1/plot/vg"
    16  	"gonum.org/v1/plot/vg/draw"
    17  )
    18  
    19  func ExampleTicks() {
    20  	tp := hplot.NewTiledPlot(draw.Tiles{Cols: 2, Rows: 8})
    21  	tp.Align = true
    22  
    23  	for i := range tp.Tiles.Rows {
    24  		for j := range tp.Tiles.Cols {
    25  			p := tp.Plot(j, i)
    26  			switch i {
    27  			case 0:
    28  				p.X.Min = 0
    29  				p.X.Max = 1
    30  				switch j {
    31  				case 0:
    32  					p.Title.Text = "hplot.Ticks"
    33  				default:
    34  					p.Title.Text = "plot.Ticks"
    35  				}
    36  			case 1:
    37  				p.X.Min = 0
    38  				p.X.Max = 10
    39  			case 2:
    40  				p.X.Min = 0
    41  				p.X.Max = 100
    42  			case 3:
    43  				p.X.Min = 0
    44  				p.X.Max = 1000
    45  			case 4:
    46  				p.X.Min = 0
    47  				p.X.Max = 10000
    48  			case 5:
    49  				p.X.Min = 0
    50  				p.X.Max = 10000
    51  			case 6:
    52  				p.X.Min = 0
    53  				p.X.Max = 1.2
    54  			case 7:
    55  				p.X.Min = 0
    56  				p.X.Max = 120
    57  			}
    58  			if j == 0 {
    59  				var (
    60  					n    = 10
    61  					xfmt = ""
    62  				)
    63  				switch i {
    64  				case 4:
    65  					n = 5
    66  				case 5:
    67  					n = 5
    68  					xfmt = "%g"
    69  				}
    70  				p.X.Tick.Marker = hplot.Ticks{N: n, Format: xfmt}
    71  			}
    72  			p.Add(hplot.NewGrid())
    73  		}
    74  	}
    75  
    76  	const sz = 20 * vg.Centimeter
    77  	err := tp.Save(sz, sz, "testdata/ticks.png")
    78  	if err != nil {
    79  		log.Fatalf("error: %+v\n", err)
    80  	}
    81  }
    82  
    83  func ExampleTicks_yearly() {
    84  	cnv := epok.UTCUnixTimeConverter{}
    85  
    86  	p := hplot.New()
    87  	p.Title.Text = "Time series (yearly)"
    88  	p.Y.Label.Text = "Goroutines"
    89  
    90  	p.Y.Min = 0
    91  	p.Y.Max = 4
    92  	p.X.AutoRescale = true
    93  	p.X.Tick.Marker = epok.Ticks{
    94  		Ruler: epok.Rules{
    95  			Major: epok.Rule{
    96  				Freq:  epok.Yearly,
    97  				Range: epok.Range{Step: 5},
    98  			},
    99  		},
   100  		Format:    "2006-01-02\n15:04:05",
   101  		Converter: cnv,
   102  	}
   103  
   104  	xysFrom := func(vs ...float64) plotter.XYs {
   105  		o := make(plotter.XYs, len(vs))
   106  		for i := range o {
   107  			o[i].X = vs[i]
   108  			o[i].Y = float64(i + 1)
   109  		}
   110  		return o
   111  	}
   112  	data := xysFrom(
   113  		cnv.FromTime(parse("2010-02-03 01:02:03")),
   114  		cnv.FromTime(parse("2011-03-04 11:22:33")),
   115  		cnv.FromTime(parse("2015-02-03 04:05:06")),
   116  		cnv.FromTime(parse("2020-02-03 07:08:09")),
   117  	)
   118  
   119  	line, pnts, err := hplot.NewLinePoints(data)
   120  	if err != nil {
   121  		log.Fatalf("could not create plotter: %+v", err)
   122  	}
   123  
   124  	line.Color = color.RGBA{B: 255, A: 255}
   125  	pnts.Shape = draw.CircleGlyph{}
   126  	pnts.Color = color.RGBA{R: 255, A: 255}
   127  
   128  	p.Add(line, pnts, hplot.NewGrid())
   129  
   130  	err = p.Save(20*vg.Centimeter, 10*vg.Centimeter, "testdata/timeseries_yearly.png")
   131  	if err != nil {
   132  		log.Fatalf("could not save plot: %+v", err)
   133  	}
   134  }
   135  
   136  func ExampleTicks_monthly() {
   137  	cnv := epok.UTCUnixTimeConverter{}
   138  
   139  	p := hplot.New()
   140  	p.Title.Text = "Time series (monthly)"
   141  	p.Y.Label.Text = "Goroutines"
   142  
   143  	p.Y.Min = 0
   144  	p.Y.Max = 4
   145  	p.X.AutoRescale = true
   146  	p.X.Tick.Marker = epok.Ticks{
   147  		Ruler: epok.Rules{
   148  			Major: epok.Rule{
   149  				Freq:  epok.Monthly,
   150  				Range: epok.RangeFrom(1, 13, 2),
   151  			},
   152  		},
   153  		Format:    "2006\nJan-02\n15:04:05",
   154  		Converter: cnv,
   155  	}
   156  
   157  	xysFrom := func(vs ...float64) plotter.XYs {
   158  		o := make(plotter.XYs, len(vs))
   159  		for i := range o {
   160  			o[i].X = vs[i]
   161  			o[i].Y = float64(i + 1)
   162  		}
   163  		return o
   164  	}
   165  	data := xysFrom(
   166  		cnv.FromTime(parse("2010-01-02 01:02:03")),
   167  		cnv.FromTime(parse("2010-02-01 01:02:03")),
   168  		cnv.FromTime(parse("2010-02-04 11:22:33")),
   169  		cnv.FromTime(parse("2010-03-04 01:02:03")),
   170  		cnv.FromTime(parse("2010-04-05 01:02:03")),
   171  		cnv.FromTime(parse("2010-04-05 01:02:03")),
   172  		cnv.FromTime(parse("2010-05-01 00:02:03")),
   173  		cnv.FromTime(parse("2010-05-04 04:04:04")),
   174  		cnv.FromTime(parse("2010-05-08 11:12:13")),
   175  		cnv.FromTime(parse("2010-06-15 01:02:03")),
   176  		cnv.FromTime(parse("2010-07-04 04:04:43")),
   177  		cnv.FromTime(parse("2010-07-14 14:17:09")),
   178  		cnv.FromTime(parse("2010-08-04 21:22:23")),
   179  		cnv.FromTime(parse("2010-08-15 11:12:13")),
   180  		cnv.FromTime(parse("2010-09-01 21:52:53")),
   181  		cnv.FromTime(parse("2010-10-25 01:19:23")),
   182  		cnv.FromTime(parse("2010-11-30 11:32:53")),
   183  		cnv.FromTime(parse("2010-12-24 23:59:59")),
   184  		cnv.FromTime(parse("2010-12-31 23:59:59")),
   185  		cnv.FromTime(parse("2011-01-12 01:02:03")),
   186  	)
   187  
   188  	line, pnts, err := hplot.NewLinePoints(data)
   189  	if err != nil {
   190  		log.Fatalf("could not create plotter: %+v", err)
   191  	}
   192  
   193  	line.Color = color.RGBA{B: 255, A: 255}
   194  	pnts.Shape = draw.CircleGlyph{}
   195  	pnts.Color = color.RGBA{R: 255, A: 255}
   196  
   197  	p.Add(line, pnts, hplot.NewGrid())
   198  
   199  	err = p.Save(20*vg.Centimeter, 10*vg.Centimeter, "testdata/timeseries_monthly.png")
   200  	if err != nil {
   201  		log.Fatalf("could not save plot: %+v", err)
   202  	}
   203  }
   204  
   205  func ExampleTicks_daily() {
   206  	cnv := epok.UTCUnixTimeConverter{}
   207  
   208  	p := hplot.New()
   209  	p.Title.Text = "Time series (daily)"
   210  	p.Y.Label.Text = "Goroutines"
   211  
   212  	p.Y.Min = 0
   213  	p.Y.Max = 4
   214  	p.X.AutoRescale = true
   215  	p.X.Tick.Marker = epok.Ticks{
   216  		Ruler: epok.Rules{
   217  			Major: epok.Rule{
   218  				Freq:  epok.Daily,
   219  				Range: epok.RangeFrom(1, 29, 14),
   220  			},
   221  			Minor: epok.Rule{
   222  				Freq:  epok.Daily,
   223  				Range: epok.RangeFrom(1, 32, 1),
   224  			},
   225  		},
   226  		Format:    "2006\nJan-02\n15:04:05",
   227  		Converter: cnv,
   228  	}
   229  
   230  	xysFrom := func(vs ...float64) plotter.XYs {
   231  		o := make(plotter.XYs, len(vs))
   232  		for i := range o {
   233  			o[i].X = vs[i]
   234  			o[i].Y = float64(i + 1)
   235  		}
   236  		return o
   237  	}
   238  	data := xysFrom(
   239  		cnv.FromTime(parse("2020-01-01 01:02:03")),
   240  		cnv.FromTime(parse("2020-01-02 02:03:04")),
   241  		cnv.FromTime(parse("2020-01-12 03:04:05")),
   242  		cnv.FromTime(parse("2020-01-22 04:05:06")),
   243  		cnv.FromTime(parse("2020-02-03 05:06:07")),
   244  		cnv.FromTime(parse("2020-02-13 06:07:08")),
   245  		cnv.FromTime(parse("2020-02-23 07:08:09")),
   246  		cnv.FromTime(parse("2020-03-01 00:00:00")),
   247  	)
   248  
   249  	line, pnts, err := hplot.NewLinePoints(data)
   250  	if err != nil {
   251  		log.Fatalf("could not create plotter: %+v", err)
   252  	}
   253  
   254  	line.Color = color.RGBA{B: 255, A: 255}
   255  	pnts.Shape = draw.CircleGlyph{}
   256  	pnts.Color = color.RGBA{R: 255, A: 255}
   257  
   258  	p.Add(line, pnts, hplot.NewGrid())
   259  
   260  	err = p.Save(20*vg.Centimeter, 10*vg.Centimeter, "testdata/timeseries_daily.png")
   261  	if err != nil {
   262  		log.Fatalf("could not save plot: %+v", err)
   263  	}
   264  }
   265  
   266  func parse(vs ...string) time.Time {
   267  	format := "2006-01-02 15:04:05"
   268  	if len(vs) > 1 {
   269  		format = vs[1]
   270  	}
   271  	t, err := time.Parse(format, vs[0])
   272  	if err != nil {
   273  		panic(err)
   274  	}
   275  	return t
   276  }