From fff00122aa2cfd291d30ea079a4a4141039ba4e2 Mon Sep 17 00:00:00 2001 From: Kai O'Reilly Date: Wed, 15 Jan 2025 10:26:11 -0800 Subject: [PATCH] add lab.NewPlotFrom helper function --- docs/content/compression-tutorial.md | 3 +-- lab/plot.go | 9 ++++++++- yaegilab/gui/cogentcore_org-lab-lab.go | 1 + yaegilab/gui/cogentcore_org-lab-plot.go | 6 +++--- 4 files changed, 13 insertions(+), 6 deletions(-) diff --git a/docs/content/compression-tutorial.md b/docs/content/compression-tutorial.md index 18d9abdc..f160c028 100644 --- a/docs/content/compression-tutorial.md +++ b/docs/content/compression-tutorial.md @@ -77,7 +77,6 @@ From **a2**, we can compute and plot **x2**, the approximation of **x** based on ```Goal # x2 := U @ a2 -plt := lab.NewPlot(b) -*plt = *fig1 +plt := lab.NewPlotFrom(fig1, b) plt.Add(plots.NewLine(plot.NewY(x2))) ``` diff --git a/lab/plot.go b/lab/plot.go index 8416db19..6c2e7c61 100644 --- a/lab/plot.go +++ b/lab/plot.go @@ -12,7 +12,7 @@ import ( // NewPlot is a simple helper function that does [plot.New] and [plotcore.NewPlot], // only returning the [plot.Plot] for convenient use in lab plots. See [NewPlotWidget] -// for a version that also returns the [plotcore.Plot]. +// for a version that also returns the [plotcore.Plot]. See also [NewPlotFrom]. func NewPlot(parent ...tree.Node) *plot.Plot { plt, _ := NewPlotWidget(parent...) return plt @@ -26,3 +26,10 @@ func NewPlotWidget(parent ...tree.Node) (*plot.Plot, *plotcore.Plot) { pw := plotcore.NewPlot(parent...).SetPlot(plt) return plt, pw } + +// NewPlotFrom is a version of [NewPlot] that copies plot data from the given starting plot. +func NewPlotFrom(from *plot.Plot, parent ...tree.Node) *plot.Plot { + plt := NewPlot(parent...) + *plt = *from + return plt +} diff --git a/yaegilab/gui/cogentcore_org-lab-lab.go b/yaegilab/gui/cogentcore_org-lab-lab.go index 961396ff..16709d8b 100644 --- a/yaegilab/gui/cogentcore_org-lab-lab.go +++ b/yaegilab/gui/cogentcore_org-lab-lab.go @@ -22,6 +22,7 @@ func init() { "NewDiffBrowserDirs": reflect.ValueOf(lab.NewDiffBrowserDirs), "NewFileNode": reflect.ValueOf(lab.NewFileNode), "NewPlot": reflect.ValueOf(lab.NewPlot), + "NewPlotFrom": reflect.ValueOf(lab.NewPlotFrom), "NewPlotWidget": reflect.ValueOf(lab.NewPlotWidget), "NewTabs": reflect.ValueOf(lab.NewTabs), "PromptOKCancel": reflect.ValueOf(lab.PromptOKCancel), diff --git a/yaegilab/gui/cogentcore_org-lab-plot.go b/yaegilab/gui/cogentcore_org-lab-plot.go index 62b0e773..dc96a68a 100644 --- a/yaegilab/gui/cogentcore_org-lab-plot.go +++ b/yaegilab/gui/cogentcore_org-lab-plot.go @@ -153,15 +153,15 @@ func (W _cogentcore_org_lab_plot_Normalizer) Normalize(min float64, max float64, // _cogentcore_org_lab_plot_Plotter is an interface wrapper for Plotter type type _cogentcore_org_lab_plot_Plotter struct { IValue interface{} - WApplyStyle func(plotStyle *plot.PlotStyle) + WApplyStyle func(plotStyle *plot.PlotStyle, idx int) WData func() (data plot.Data, pixX []float32, pixY []float32) WPlot func(pt *plot.Plot) WStylers func() *plot.Stylers WUpdateRange func(plt *plot.Plot, xr *minmax.F64, yr *minmax.F64, zr *minmax.F64) } -func (W _cogentcore_org_lab_plot_Plotter) ApplyStyle(plotStyle *plot.PlotStyle) { - W.WApplyStyle(plotStyle) +func (W _cogentcore_org_lab_plot_Plotter) ApplyStyle(plotStyle *plot.PlotStyle, idx int) { + W.WApplyStyle(plotStyle, idx) } func (W _cogentcore_org_lab_plot_Plotter) Data() (data plot.Data, pixX []float32, pixY []float32) { return W.WData()