Example #1
0
// PlotDensity plots histogram in density values
//  sty -- style can be <nil>
func (o Histogram) PlotDensity(sty *plt.Sty, args string) {
	if sty == nil {
		sty = &plt.Sty{Fc: "#fbc175", Ec: "k", Lw: 1, Closed: true}
	}
	nstations := len(o.Stations)
	nsamples := 0
	for _, cnt := range o.Counts {
		nsamples += cnt
	}
	for i := 0; i < nstations-1; i++ {
		xi, xf := o.Stations[i], o.Stations[i+1]
		dx := xf - xi
		prob := float64(o.Counts[i]) / (float64(nsamples) * dx)
		plt.DrawPolyline([][]float64{{xi, 0.0}, {xf, 0.0}, {xf, prob}, {xi, prob}}, sty, args)
	}
}
Example #2
0
// Draw2d draws bins' grid
func (o *Bins) Draw2d(withtxt, withgrid, withentries, setup bool, selBins map[int]bool) {

	if withgrid {
		// horizontal lines
		x := []float64{o.Xi[0], o.Xi[0] + o.L[0] + o.S[0]}
		y := make([]float64, 2)
		for j := 0; j < o.N[1]+1; j++ {
			y[0] = o.Xi[1] + float64(j)*o.S[1]
			y[1] = y[0]
			plt.Plot(x, y, "'-', color='#4f3677', clip_on=0")
		}

		// vertical lines
		y[0] = o.Xi[1]
		y[1] = o.Xi[1] + o.L[1] + o.S[1]
		for i := 0; i < o.N[0]+1; i++ {
			x[0] = o.Xi[0] + float64(i)*o.S[0]
			x[1] = x[0]
			plt.Plot(x, y, "'k-', color='#4f3677', clip_on=0")
		}
	}

	// selected bins
	nxy := o.N[0] * o.N[1]
	for idx, _ := range selBins {
		i := idx % o.N[0] // indices representing bin
		j := (idx % nxy) / o.N[0]
		x := o.Xi[0] + float64(i)*o.S[0] // coordinates of bin corner
		y := o.Xi[1] + float64(j)*o.S[1]
		plt.DrawPolyline([][]float64{
			{x, y},
			{x + o.S[0], y},
			{x + o.S[0], y + o.S[1]},
			{x, y + o.S[1]},
		}, &plt.Sty{Fc: "#fbefdc", Ec: "#8e8371", Lw: 0.5, Closed: true}, "clip_on=0")
	}

	// plot items
	if withentries {
		for _, bin := range o.All {
			if bin == nil {
				continue
			}
			for _, entry := range bin.Entries {
				plt.PlotOne(entry.X[0], entry.X[1], "'r.', clip_on=0")
			}
		}
	}

	// labels
	if withtxt {
		for j := 0; j < o.N[1]; j++ {
			for i := 0; i < o.N[0]; i++ {
				idx := i + j*o.N[0]
				x := o.Xi[0] + float64(i)*o.S[0] + 0.02*o.S[0]
				y := o.Xi[1] + float64(j)*o.S[1] + 0.02*o.S[1]
				plt.Text(x, y, io.Sf("%d", idx), "size=7")
			}
		}
	}

	// setup
	if setup {
		plt.Equal()
		plt.AxisRange(o.Xi[0]-0.1, o.Xf[0]+o.S[0]+0.1, o.Xi[1]-0.1, o.Xf[1]+o.S[1]+0.1)
	}
}
Example #3
0
// PlotDiagMoment plots bending moment diagram
//  Input:
//   M        -- moment along stations
//   withtext -- show bending moment values
//   numfmt   -- number format for values. use "" to chose default one
//   tolM     -- tolerance to clip absolute values of M
//   sf       -- scaling factor
func (o *Beam) PlotDiagMoment(M []float64, withtext bool, numfmt string, tolM, sf float64) {

	// number of stations
	nstations := len(M)
	ds := 1.0 / float64(nstations-1)

	// nodes
	var xa, xb []float64
	var u []float64 // out-of-pane vector
	if o.Ndim == 2 {
		xa = []float64{o.X[0][0], o.X[1][0], 0}
		xb = []float64{o.X[0][1], o.X[1][1], 0}
		u = []float64{0, 0, 1}
	} else {
		chk.Panic("TODO: 3D beam diagram")
	}

	// unit vector along beam
	v := make([]float64, 3)
	sum := 0.0
	for j := 0; j < o.Ndim; j++ {
		v[j] = xb[j] - xa[j]
		sum += v[j] * v[j]
	}
	sum = math.Sqrt(sum)
	for j := 0; j < o.Ndim; j++ {
		v[j] /= sum
	}

	// unit normal
	n := make([]float64, 3)     // normal
	utl.CrossProduct3d(n, u, v) // n := u cross v

	// auxiliary vectors
	x := make([]float64, o.Ndim) // station
	m := make([]float64, o.Ndim) // vector pointing to other side
	c := make([]float64, o.Ndim) // centre
	imin, imax := utl.DblArgMinMax(M)

	// draw text function
	draw_text := func(mom float64) {
		if math.Abs(mom) > tolM {
			α := math.Atan2(-n[1], -n[0]) * 180.0 / math.Pi
			str := io.Sf("%g", mom)
			if numfmt != "" {
				str = io.Sf(numfmt, mom)
			} else {
				if len(str) > 10 {
					str = io.Sf("%.10f", mom) // truncate number
					str = io.Sf("%g", io.Atof(str))
				}
			}
			plt.Text(c[0], c[1], str, io.Sf("ha='center', size=7, rotation=%g, clip_on=0", α))
		}
	}

	// draw
	pts := utl.DblsAlloc(nstations, 2)
	xx, yy := make([]float64, 2), make([]float64, 2)
	for i := 0; i < nstations; i++ {

		// station
		s := float64(i) * ds
		for j := 0; j < o.Ndim; j++ {
			x[j] = (1.0-s)*o.X[j][0] + s*o.X[j][1]
		}

		// auxiliary vectors
		for j := 0; j < o.Ndim; j++ {
			m[j] = x[j] - sf*M[i]*n[j]
			c[j] = (x[j] + m[j]) / 2.0
		}

		// points on diagram
		pts[i][0], pts[i][1] = m[0], m[1]
		xx[0], xx[1] = x[0], m[0]
		yy[0], yy[1] = x[1], m[1]

		// draw
		clr, lw := "#919191", 1.0
		if i == imin || i == imax {
			lw = 2
			if M[i] < 0 {
				clr = "#9f0000"
			} else {
				clr = "#109f24"
			}
		}
		plt.Plot(xx, yy, io.Sf("'-', color='%s', lw=%g, clip_on=0", clr, lw))
		if withtext {
			if i == imin || i == imax { // draw text @ min/max
				draw_text(M[i])
			} else {
				if i == 0 || i == nstations-1 { // draw text @ extremities
					draw_text(M[i])
				}
			}
		}
	}

	// draw polyline
	plt.DrawPolyline(pts, &plt.Sty{Ec: "k", Fc: "none", Lw: 1}, "")
}