go-hep.org/x/hep@v0.38.1/hplot/hstack_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 "math/rand/v2" 11 12 "go-hep.org/x/hep/hbook" 13 "go-hep.org/x/hep/hplot" 14 "gonum.org/v1/gonum/stat/distuv" 15 "gonum.org/v1/plot" 16 "gonum.org/v1/plot/vg" 17 "gonum.org/v1/plot/vg/draw" 18 ) 19 20 func ExampleHStack() { 21 h1 := hbook.NewH1D(100, -10, 10) 22 h2 := hbook.NewH1D(100, -10, 10) 23 h3 := hbook.NewH1D(100, -10, 10) 24 25 const seed = 1234 26 fillH1(h1, 10000, -2, 1, seed) 27 fillH1(h2, 10000, +3, 3, seed) 28 fillH1(h3, 10000, +4, 1, seed) 29 30 colors := []color.Color{ 31 color.NRGBA{122, 195, 106, 150}, 32 color.NRGBA{90, 155, 212, 150}, 33 color.NRGBA{250, 167, 91, 150}, 34 } 35 36 hh1 := hplot.NewH1D(h1) 37 hh1.FillColor = colors[0] 38 hh1.LineStyle.Color = color.Black 39 40 hh2 := hplot.NewH1D(h2) 41 hh2.FillColor = colors[1] 42 hh2.LineStyle.Width = 0 43 44 hh3 := hplot.NewH1D(h3) 45 hh3.FillColor = colors[2] 46 hh3.LineStyle.Color = color.Black 47 48 hs := []*hplot.H1D{hh1, hh2, hh3} 49 50 tp := hplot.NewTiledPlot(draw.Tiles{Cols: 1, Rows: 3}) 51 tp.Align = true 52 53 { 54 p := tp.Plots[0] 55 p.Title.Text = "Histograms" 56 p.Y.Label.Text = "Y" 57 p.Add(hh1, hh2, hh3, hplot.NewGrid()) 58 p.Legend.Add("h1", hh1) 59 p.Legend.Add("h2", hh2) 60 p.Legend.Add("h3", hh3) 61 p.Legend.Top = true 62 p.Legend.Left = true 63 } 64 65 { 66 p := tp.Plot(0, 1) 67 p.Title.Text = "HStack - stack: OFF" 68 p.Y.Label.Text = "Y" 69 hstack := hplot.NewHStack(hs) 70 hstack.Stack = hplot.HStackOff 71 p.Add(hstack, hplot.NewGrid()) 72 p.Legend.Add("h1", hs[0]) 73 p.Legend.Add("h2", hs[1]) 74 p.Legend.Add("h3", hs[2]) 75 p.Legend.Top = true 76 p.Legend.Left = true 77 } 78 79 { 80 p := tp.Plot(0, 2) 81 p.Title.Text = "Hstack - stack: ON" 82 p.X.Label.Text = "X" 83 p.Y.Label.Text = "Y" 84 hstack := hplot.NewHStack(hs, hplot.WithLogY(false)) 85 p.Add(hstack, hplot.NewGrid()) 86 p.Legend.Add("h1", hs[0]) 87 p.Legend.Add("h2", hs[1]) 88 p.Legend.Add("h3", hs[2]) 89 p.Legend.Top = true 90 p.Legend.Left = true 91 } 92 93 err := tp.Save(15*vg.Centimeter, 15*vg.Centimeter, "testdata/hstack.png") 94 if err != nil { 95 log.Fatalf("error: %+v", err) 96 } 97 98 } 99 100 func ExampleHStack_withBand() { 101 h1 := hbook.NewH1D(50, -8, 12) 102 h2 := hbook.NewH1D(50, -8, 12) 103 h3 := hbook.NewH1D(50, -8, 12) 104 105 const seed = 1234 106 fillH1(h1, 2000, -2, 1, seed) 107 fillH1(h2, 2000, +3, 3, seed) 108 fillH1(h3, 2000, +4, 1, seed) 109 110 colors := []color.Color{ 111 color.NRGBA{122, 195, 106, 150}, 112 color.NRGBA{90, 155, 212, 150}, 113 color.NRGBA{250, 167, 91, 150}, 114 } 115 116 hh1 := hplot.NewH1D(h1, hplot.WithBand(true)) 117 hh1.FillColor = colors[0] 118 hh1.LineStyle.Color = color.Black 119 hh1.Band.FillColor = color.NRGBA{G: 210, A: 200} 120 121 hh2 := hplot.NewH1D(h2, hplot.WithBand(false)) 122 hh2.FillColor = colors[1] 123 hh2.LineStyle.Width = 0 124 125 hh3 := hplot.NewH1D(h3, hplot.WithBand(true)) 126 hh3.FillColor = colors[2] 127 hh3.LineStyle.Color = color.Black 128 hh3.Band.FillColor = color.NRGBA{R: 220, A: 200} 129 130 hs := []*hplot.H1D{hh1, hh2, hh3} 131 132 hh4 := hplot.NewH1D(h1) 133 hh4.FillColor = colors[0] 134 hh4.LineStyle.Color = color.Black 135 136 hh5 := hplot.NewH1D(h2) 137 hh5.FillColor = colors[1] 138 hh5.LineStyle.Width = 0 139 140 hh6 := hplot.NewH1D(h3) 141 hh6.FillColor = colors[2] 142 hh6.LineStyle.Color = color.Black 143 144 hsHistoNoBand := []*hplot.H1D{hh4, hh5, hh6} 145 146 tp := hplot.NewTiledPlot(draw.Tiles{Cols: 2, Rows: 2}) 147 tp.Align = true 148 149 { 150 p := tp.Plot(0, 0) 151 p.Title.Text = "Histos With or Without Band, Stack: OFF" 152 p.Title.Padding = 10 153 p.X.Label.Text = "X" 154 p.Y.Label.Text = "Y" 155 hstack := hplot.NewHStack(hs, hplot.WithBand(true)) 156 hstack.Stack = hplot.HStackOff 157 p.Add(hstack, hplot.NewGrid()) 158 p.Legend.Add("h1", hs[0]) 159 p.Legend.Add("h2", hs[1]) 160 p.Legend.Add("h3", hs[2]) 161 p.Legend.Top = true 162 p.Legend.Left = true 163 } 164 165 { 166 p := tp.Plot(1, 0) 167 p.Title.Text = "Histos Without Band, Stack: OFF" 168 p.Title.Padding = 10 169 p.X.Label.Text = "X" 170 p.Y.Label.Text = "Y" 171 hstack := hplot.NewHStack(hsHistoNoBand, hplot.WithBand(true)) 172 hstack.Stack = hplot.HStackOff 173 hstack.Band.FillColor = color.NRGBA{R: 100, G: 100, B: 100, A: 200} 174 p.Add(hstack, hplot.NewGrid()) 175 p.Legend.Add("h1", hs[0]) 176 p.Legend.Add("h2", hs[1]) 177 p.Legend.Add("h3", hs[2]) 178 p.Legend.Top = true 179 p.Legend.Left = true 180 } 181 182 { 183 p := tp.Plot(0, 1) 184 p.Title.Text = "Histos With or Without Band, Stack: ON" 185 p.Title.Padding = 10 186 p.X.Label.Text = "X" 187 p.Y.Label.Text = "Y" 188 hstack := hplot.NewHStack(hs, hplot.WithBand(true)) 189 hstack.Band.FillColor = color.NRGBA{R: 100, G: 100, B: 100, A: 200} 190 p.Add(hstack, hplot.NewGrid()) 191 p.Legend.Add("h1", hs[0]) 192 p.Legend.Add("h2", hs[1]) 193 p.Legend.Add("h3", hs[2]) 194 p.Legend.Top = true 195 p.Legend.Left = true 196 } 197 198 { 199 p := tp.Plot(1, 1) 200 p.Title.Text = "Histos Without Band, Stack: ON" 201 p.Title.Padding = 10 202 p.X.Label.Text = "X" 203 p.Y.Label.Text = "Y" 204 hstack := hplot.NewHStack(hsHistoNoBand, hplot.WithBand(true)) 205 hstack.Band.FillColor = color.NRGBA{R: 100, G: 100, B: 100, A: 200} 206 p.Add(hstack, hplot.NewGrid()) 207 p.Legend.Add("h1", hs[0]) 208 p.Legend.Add("h2", hs[1]) 209 p.Legend.Add("h3", hs[2]) 210 p.Legend.Top = true 211 p.Legend.Left = true 212 } 213 214 err := tp.Save(25*vg.Centimeter, 15*vg.Centimeter, "testdata/hstack_band.png") 215 if err != nil { 216 log.Fatalf("error: %+v", err) 217 } 218 } 219 220 func ExampleHStack_withLogY() { 221 h1 := hbook.NewH1D(50, -8, 12) 222 h2 := hbook.NewH1D(50, -8, 12) 223 h3 := hbook.NewH1D(50, -8, 12) 224 225 const seed = 1234 226 fillH1(h1, 2000, -2, 1, seed) 227 fillH1(h2, 2000, +3, 3, seed) 228 fillH1(h3, 2000, +4, 1, seed) 229 230 colors := []color.Color{ 231 color.NRGBA{122, 195, 106, 150}, 232 color.NRGBA{90, 155, 212, 150}, 233 color.NRGBA{250, 167, 91, 150}, 234 } 235 logy := hplot.WithLogY(true) 236 237 hh1 := hplot.NewH1D(h1, hplot.WithBand(true), logy) 238 hh1.FillColor = colors[0] 239 hh1.LineStyle.Color = color.Black 240 hh1.Band.FillColor = color.NRGBA{G: 210, A: 200} 241 242 hh2 := hplot.NewH1D(h2, hplot.WithBand(false), logy) 243 hh2.FillColor = colors[1] 244 hh2.LineStyle.Width = 0 245 246 hh3 := hplot.NewH1D(h3, hplot.WithBand(true), logy) 247 hh3.FillColor = colors[2] 248 hh3.LineStyle.Color = color.Black 249 hh3.Band.FillColor = color.NRGBA{R: 220, A: 200} 250 251 hs := []*hplot.H1D{hh1, hh2, hh3} 252 253 hh4 := hplot.NewH1D(h1, logy) 254 hh4.FillColor = colors[0] 255 hh4.LineStyle.Color = color.Black 256 257 hh5 := hplot.NewH1D(h2, logy) 258 hh5.FillColor = colors[1] 259 hh5.LineStyle.Width = 0 260 261 hh6 := hplot.NewH1D(h3, logy) 262 hh6.FillColor = colors[2] 263 hh6.LineStyle.Color = color.Black 264 265 hsHistoNoBand := []*hplot.H1D{hh4, hh5, hh6} 266 267 tp := hplot.NewTiledPlot(draw.Tiles{Cols: 2, Rows: 2}) 268 tp.Align = true 269 270 { 271 p := tp.Plot(0, 0) 272 p.Title.Text = "Histos With or Without Band, Stack: OFF" 273 p.Title.Padding = 10 274 p.Y.Scale = plot.LogScale{} 275 p.Y.Tick.Marker = plot.LogTicks{} 276 p.X.Label.Text = "X" 277 p.Y.Label.Text = "Y" 278 hstack := hplot.NewHStack(hs, hplot.WithBand(true), logy) 279 hstack.Stack = hplot.HStackOff 280 p.Add(hstack, hplot.NewGrid()) 281 p.Legend.Add("h1", hs[0]) 282 p.Legend.Add("h2", hs[1]) 283 p.Legend.Add("h3", hs[2]) 284 p.Legend.Top = true 285 p.Legend.Left = true 286 } 287 288 { 289 p := tp.Plot(1, 0) 290 p.Title.Text = "Histos Without Band, Stack: OFF" 291 p.Title.Padding = 10 292 p.Y.Scale = plot.LogScale{} 293 p.Y.Tick.Marker = plot.LogTicks{} 294 p.X.Label.Text = "X" 295 p.Y.Label.Text = "Y" 296 hstack := hplot.NewHStack(hsHistoNoBand, hplot.WithBand(true), logy) 297 hstack.Stack = hplot.HStackOff 298 hstack.Band.FillColor = color.NRGBA{R: 100, G: 100, B: 100, A: 200} 299 p.Add(hstack, hplot.NewGrid()) 300 p.Legend.Add("h1", hs[0]) 301 p.Legend.Add("h2", hs[1]) 302 p.Legend.Add("h3", hs[2]) 303 p.Legend.Top = true 304 p.Legend.Left = true 305 } 306 307 { 308 p := tp.Plot(0, 1) 309 p.Title.Text = "Histos With or Without Band, Stack: ON" 310 p.Title.Padding = 10 311 p.Y.Scale = plot.LogScale{} 312 p.Y.Tick.Marker = plot.LogTicks{} 313 p.X.Label.Text = "X" 314 p.Y.Label.Text = "Y" 315 hstack := hplot.NewHStack(hs, hplot.WithBand(true), logy) 316 hstack.Band.FillColor = color.NRGBA{R: 100, G: 100, B: 100, A: 200} 317 p.Add(hstack, hplot.NewGrid()) 318 p.Legend.Add("h1", hs[0]) 319 p.Legend.Add("h2", hs[1]) 320 p.Legend.Add("h3", hs[2]) 321 p.Legend.Top = true 322 p.Legend.Left = true 323 } 324 325 { 326 p := tp.Plot(1, 1) 327 p.Title.Text = "Histos Without Band, Stack: ON" 328 p.Title.Padding = 10 329 p.Y.Scale = plot.LogScale{} 330 p.Y.Tick.Marker = plot.LogTicks{} 331 p.X.Label.Text = "X" 332 p.Y.Label.Text = "Y" 333 hstack := hplot.NewHStack(hsHistoNoBand, hplot.WithBand(true), logy) 334 hstack.Band.FillColor = color.NRGBA{R: 100, G: 100, B: 100, A: 200} 335 p.Add(hstack, hplot.NewGrid()) 336 p.Legend.Add("h1", hs[0]) 337 p.Legend.Add("h2", hs[1]) 338 p.Legend.Add("h3", hs[2]) 339 p.Legend.Top = true 340 p.Legend.Left = true 341 } 342 343 err := tp.Save(25*vg.Centimeter, 15*vg.Centimeter, "testdata/hstack_logy.png") 344 if err != nil { 345 log.Fatalf("error: %+v", err) 346 } 347 } 348 349 func fillH1(h *hbook.H1D, n int, mu, sigma float64, seed uint64) { 350 dist := distuv.Normal{ 351 Mu: mu, 352 Sigma: sigma, 353 Src: rand.New(rand.NewPCG(seed, seed)), 354 } 355 356 for range n { 357 v := dist.Rand() 358 h.Fill(v, 1) 359 } 360 }