diff --git a/examples/data_analysis_example/main.v b/examples/data_analysis_example/main.v index 6d6a3a530..cbfa53eee 100644 --- a/examples/data_analysis_example/main.v +++ b/examples/data_analysis_example/main.v @@ -62,7 +62,7 @@ fn main() { ])! // Visualize data in a 3D scatter plot - mut plt_3d := plot.new_plot() + mut plt_3d := plot.Plot.new() x1 := data.x.get_col(0) x2 := data.x.get_col(1) @@ -131,7 +131,7 @@ fn main() { stat.update() // Visualize statistics in a bar chart - mut plt_bars := plot.new_plot() + mut plt_bars := plot.Plot.new() plt_bars.bar( x: []string{len: stat.mean_x.len, init: 'Class ${index}'} diff --git a/examples/fft_plot_example/main.v b/examples/fft_plot_example/main.v index 4059fb8dc..51ba8881d 100644 --- a/examples/fft_plot_example/main.v +++ b/examples/fft_plot_example/main.v @@ -22,7 +22,7 @@ fn main() { spectrum := signal.clone() // Create a scatter plot for the signal and its spectrum - mut plt := plot.new_plot() + mut plt := plot.Plot.new() // Add a scatter plot for the original signal plt.scatter( diff --git a/examples/plot_annotated_pie_chart/main.v b/examples/plot_annotated_pie_chart/main.v index f9a83aa67..53f9f740d 100644 --- a/examples/plot_annotated_pie_chart/main.v +++ b/examples/plot_annotated_pie_chart/main.v @@ -6,7 +6,7 @@ fn main() { labels := ['Apples', 'Bananas', 'Cherries', 'Grapes'] values := [25.0, 30, 15, 30] - mut plt := plot.new_plot() + mut plt := plot.Plot.new() plt.pie( labels: labels values: values diff --git a/examples/plot_bar/main.v b/examples/plot_bar/main.v index 2cf02e1e1..8ae905e9f 100644 --- a/examples/plot_bar/main.v +++ b/examples/plot_bar/main.v @@ -3,7 +3,7 @@ module main import vsl.plot fn main() { - mut plt := plot.new_plot() + mut plt := plot.Plot.new() plt.bar( x: ['China', 'India', 'USA', 'Indonesia', 'Pakistan'] diff --git a/examples/plot_basic_heatmap/main.v b/examples/plot_basic_heatmap/main.v index 7beaf676b..c0eb735f8 100644 --- a/examples/plot_basic_heatmap/main.v +++ b/examples/plot_basic_heatmap/main.v @@ -6,7 +6,7 @@ z := [[1.0, 0, 30, 50, 1], [20.0, 1, 60, 80, 30], [30.0, 60, 1, -10, 20]] x := ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] y := ['Morning', 'Afternoon', 'Evening'] -mut plt := plot.new_plot() +mut plt := plot.Plot.new() plt.heatmap( x: x diff --git a/examples/plot_bubble_chart/main.v b/examples/plot_bubble_chart/main.v index 8294ec9cc..68ad2a5aa 100644 --- a/examples/plot_bubble_chart/main.v +++ b/examples/plot_bubble_chart/main.v @@ -7,7 +7,7 @@ fn main() { y := []f64{len: 100, init: f64(index) * 0.1} size := []f64{len: 10, init: f64(index) * 10.0} - mut plt := plot.new_plot() + mut plt := plot.Plot.new() plt.scatter( x: x y: y diff --git a/examples/plot_grouped_bar_chart/main.v b/examples/plot_grouped_bar_chart/main.v index 1c2447f4d..d0fcdd116 100644 --- a/examples/plot_grouped_bar_chart/main.v +++ b/examples/plot_grouped_bar_chart/main.v @@ -7,7 +7,7 @@ fn main() { values1 := [10.0, 15, 7, 12] values2 := [8.0, 14, 5, 10] - mut plt := plot.new_plot() + mut plt := plot.Plot.new() plt.bar( x: categories y: values1 diff --git a/examples/plot_heatmap_golden_ratio/main.v b/examples/plot_heatmap_golden_ratio/main.v index f7e8da879..e8329e3ef 100644 --- a/examples/plot_heatmap_golden_ratio/main.v +++ b/examples/plot_heatmap_golden_ratio/main.v @@ -24,7 +24,7 @@ z := [[13.0, 3, 3, 5], [13.0, 2, 1, 5], [13.0, 10, 11, 12], [13.0, 8, 8, 8]] // x := r*math.cos(theta) // y := r*math.sin(theta) -mut plt := plot.new_plot() +mut plt := plot.Plot.new() plt.heatmap( x: xe diff --git a/examples/plot_histogram/main.v b/examples/plot_histogram/main.v index 1aa4c0105..f25995c33 100644 --- a/examples/plot_histogram/main.v +++ b/examples/plot_histogram/main.v @@ -9,7 +9,7 @@ mut x1 := []f64{cap: 1000} for _ in 1 .. 1000 { x1 << rand.f64n(100) or { 0 } } -mut plt := plot.new_plot() +mut plt := plot.Plot.new() plt.histogram( x: x1 xbins: { diff --git a/examples/plot_line_plot_with_areas/main.v b/examples/plot_line_plot_with_areas/main.v index 66ac1e562..6af4f913a 100644 --- a/examples/plot_line_plot_with_areas/main.v +++ b/examples/plot_line_plot_with_areas/main.v @@ -8,7 +8,7 @@ fn main() { y1 := x.map(math.sin(it)) y2 := x.map(math.cos(it)) - mut plt := plot.new_plot() + mut plt := plot.Plot.new() plt.scatter( x: x y: y1 diff --git a/examples/plot_pie/main.v b/examples/plot_pie/main.v index e896db4ff..43f5f29da 100644 --- a/examples/plot_pie/main.v +++ b/examples/plot_pie/main.v @@ -3,7 +3,7 @@ module main import vsl.plot fn main() { - mut plt := plot.new_plot() + mut plt := plot.Plot.new() plt.pie( labels: ['Nitrogen', 'Oxygen', 'Argon', 'Other'] diff --git a/examples/plot_ripple_surface/main.v b/examples/plot_ripple_surface/main.v index 15baf79e2..442d4018e 100644 --- a/examples/plot_ripple_surface/main.v +++ b/examples/plot_ripple_surface/main.v @@ -18,7 +18,7 @@ fn main() { z << row } - mut plt := plot.new_plot() + mut plt := plot.Plot.new() plt.surface( x: x y: y diff --git a/examples/plot_saddle_surface/main.v b/examples/plot_saddle_surface/main.v index 8df68b528..372662d74 100644 --- a/examples/plot_saddle_surface/main.v +++ b/examples/plot_saddle_surface/main.v @@ -17,7 +17,7 @@ fn main() { z << row } - mut plt := plot.new_plot() + mut plt := plot.Plot.new() plt.surface( x: x y: y diff --git a/examples/plot_scatter/main.v b/examples/plot_scatter/main.v index 31e459c38..2027eb36f 100644 --- a/examples/plot_scatter/main.v +++ b/examples/plot_scatter/main.v @@ -20,7 +20,7 @@ y := [ ] x := util.arange(y.len).map(f64(it)) -mut plt := plot.new_plot() +mut plt := plot.Plot.new() plt.scatter( x: x y: y diff --git a/examples/plot_scatter3d_1/main.v b/examples/plot_scatter3d_1/main.v index 6749d083a..1173d3771 100644 --- a/examples/plot_scatter3d_1/main.v +++ b/examples/plot_scatter3d_1/main.v @@ -22,7 +22,7 @@ fn main() { x := util.arange(y.len).map(f64(it)) z := util.arange(y.len).map(util.arange(y.len).map(f64(it * it))) - mut plt := plot.new_plot() + mut plt := plot.Plot.new() plt.scatter3d( x: x y: y diff --git a/examples/plot_scatter3d_2/main.v b/examples/plot_scatter3d_2/main.v index 27be00dc6..9eda8acf2 100644 --- a/examples/plot_scatter3d_2/main.v +++ b/examples/plot_scatter3d_2/main.v @@ -15,7 +15,7 @@ fn main() { z << val } - mut plt := plot.new_plot() + mut plt := plot.Plot.new() plt.scatter3d( x: x y: y diff --git a/examples/plot_scatter3d_easing/main.v b/examples/plot_scatter3d_easing/main.v index 96c43851e..7f6fa2d4e 100644 --- a/examples/plot_scatter3d_easing/main.v +++ b/examples/plot_scatter3d_easing/main.v @@ -15,7 +15,7 @@ fn main() { z_values := easings.animate(easings.bounce_ease_in_out, 0.0, 1.0, frames) // Create the Scatter3D plot - mut plt := plot.new_plot() + mut plt := plot.Plot.new() plt.scatter3d( name: 'Easing Scatter3D' x: x_values diff --git a/examples/plot_scatter_colorscale/main.v b/examples/plot_scatter_colorscale/main.v index 5935b6b9d..0cecfbc35 100644 --- a/examples/plot_scatter_colorscale/main.v +++ b/examples/plot_scatter_colorscale/main.v @@ -20,7 +20,7 @@ y := [ ] x := util.arange(y.len).map(f64(it)) -mut plt := plot.new_plot() +mut plt := plot.Plot.new() plt.scatter( x: x y: y diff --git a/examples/plot_scatter_easing/main.v b/examples/plot_scatter_easing/main.v index da454e1c1..79c855ed6 100644 --- a/examples/plot_scatter_easing/main.v +++ b/examples/plot_scatter_easing/main.v @@ -14,7 +14,7 @@ fn main() { y_values := easings.animate(easings.elastic_ease_out, 0.0, 1.0, frames) // Create the Scatter plot - mut plt := plot.new_plot() + mut plt := plot.Plot.new() plt.scatter( name: 'Easing Scatter' x: x_values diff --git a/examples/plot_scatter_with_bars/main.v b/examples/plot_scatter_with_bars/main.v index ce5c21ca2..a5660344d 100644 --- a/examples/plot_scatter_with_bars/main.v +++ b/examples/plot_scatter_with_bars/main.v @@ -7,7 +7,7 @@ fn main() { x := util.lin_space(1.0, 10.0, 10) y := x.map(it * it) - mut plt := plot.new_plot() + mut plt := plot.Plot.new() plt.scatter( x: x y: y diff --git a/examples/plot_scatter_with_histogram/main.v b/examples/plot_scatter_with_histogram/main.v index 2e83ed99b..96a8264ae 100644 --- a/examples/plot_scatter_with_histogram/main.v +++ b/examples/plot_scatter_with_histogram/main.v @@ -11,7 +11,7 @@ fn main() { x << rand.f64n(100) or { 0 } } - mut plt := plot.new_plot() + mut plt := plot.Plot.new() plt.scatter( x: x y: x diff --git a/examples/plot_scatter_with_regression/main.v b/examples/plot_scatter_with_regression/main.v index d1a3b4b9b..47c15a8db 100644 --- a/examples/plot_scatter_with_regression/main.v +++ b/examples/plot_scatter_with_regression/main.v @@ -6,7 +6,7 @@ fn main() { x := []f64{len: 100, init: f64(index) * 0.1} y := [1.2, 2.1, 3.0, 4.5, 5.8, 7.0, 8.2, 9.1, 10.5, 11.8] - mut plt := plot.new_plot() + mut plt := plot.Plot.new() plt.scatter( x: x y: y diff --git a/examples/plot_shaded_area_sin/main.v b/examples/plot_shaded_area_sin/main.v index b571e028d..83d0044f1 100644 --- a/examples/plot_shaded_area_sin/main.v +++ b/examples/plot_shaded_area_sin/main.v @@ -7,7 +7,7 @@ fn main() { x := []f64{len: 100, init: f64(index) * 0.1} y := x.map(math.sin(it)) - mut plt := plot.new_plot() + mut plt := plot.Plot.new() plt.scatter( x: x y: y diff --git a/examples/plot_sin_cos_surface/main.v b/examples/plot_sin_cos_surface/main.v index 62a6a2ffd..4d8ab3cbd 100644 --- a/examples/plot_sin_cos_surface/main.v +++ b/examples/plot_sin_cos_surface/main.v @@ -17,7 +17,7 @@ fn main() { z << row } - mut plt := plot.new_plot() + mut plt := plot.Plot.new() plt.surface( x: x y: y diff --git a/examples/plot_surface/main.v b/examples/plot_surface/main.v index bad46f9b3..f383fa627 100644 --- a/examples/plot_surface/main.v +++ b/examples/plot_surface/main.v @@ -17,7 +17,7 @@ fn main() { z << row } - mut plt := plot.new_plot() + mut plt := plot.Plot.new() plt.surface( x: x y: y diff --git a/examples/plot_surface_easing/main.v b/examples/plot_surface_easing/main.v index 069e635ec..b6f7ebe8a 100644 --- a/examples/plot_surface_easing/main.v +++ b/examples/plot_surface_easing/main.v @@ -13,7 +13,7 @@ fn main() { z_values := easings.animate(easings.bounce_ease_in_out, 0.0, 1.0, frames) // Create the Surface plot - mut plt := plot.new_plot() + mut plt := plot.Plot.new() plt.surface( name: 'Easing Surface' x: x_values diff --git a/examples/roots_bisection_solver/main.v b/examples/roots_bisection_solver/main.v index e38053632..142368f0c 100644 --- a/examples/roots_bisection_solver/main.v +++ b/examples/roots_bisection_solver/main.v @@ -34,7 +34,7 @@ assert float64.soclose(result.x, expected, solver.epsabs) println('x = ${result.x}') -mut plt := plot.new_plot() +mut plt := plot.Plot.new() x := util.lin_space(0.0, 3.0, 100) y := x.map(f_cos(it, []f64{})) diff --git a/ml/kmeans.v b/ml/kmeans.v index 941a1951b..8716cc096 100644 --- a/ml/kmeans.v +++ b/ml/kmeans.v @@ -149,7 +149,7 @@ pub fn (o &Kmeans) str() string { // plot method for visualizing the clustering pub fn (o &Kmeans) plot() ! { - mut plt := plot.new_plot() + mut plt := plot.Plot.new() plt.layout( title: 'K-means Clustering' ) diff --git a/ml/knn.v b/ml/knn.v index 384607f20..62a1239bf 100644 --- a/ml/knn.v +++ b/ml/knn.v @@ -206,7 +206,7 @@ pub fn (o &KNN) str() string { // plot method for visualizing the KNN model pub fn (o &KNN) plot() ! { - mut plt := plot.new_plot() + mut plt := plot.Plot.new() plt.layout( title: 'K-Nearest Neighbors' ) diff --git a/ml/linreg.v b/ml/linreg.v index 0c0c4c37c..be38899e4 100644 --- a/ml/linreg.v +++ b/ml/linreg.v @@ -163,7 +163,7 @@ pub fn (o &LinReg) plot() ! { y_values := x_values.map(o.predict([it])) // Rest of the code for plotting the graph - mut plt := plot.new_plot() + mut plt := plot.Plot.new() plt.layout( title: 'Linear Regression Example' ) diff --git a/plot/plot.v b/plot/plot.v index 9d8ef8467..de630c3e9 100644 --- a/plot/plot.v +++ b/plot/plot.v @@ -10,55 +10,75 @@ pub mut: layout Layout } -pub fn new_plot() Plot { +pub fn Plot.new() Plot { return Plot{} } -pub fn (mut p Plot) scatter(trace ScatterTrace) Plot { +// add_trace adds a trace to the plot +[inline] +fn (mut p Plot) add_trace[T](trace T) Plot { p.traces << trace return p } +// scatter adds a scatter trace to the plot +[inline] +pub fn (mut p Plot) scatter(trace ScatterTrace) Plot { + return p.add_trace(trace) +} + +// pie adds a pie trace to the plot +[inline] pub fn (mut p Plot) pie(trace PieTrace) Plot { - p.traces << trace - return p + return p.add_trace(trace) } +// heatmap adds a heatmap trace to the plot +[inline] pub fn (mut p Plot) heatmap(trace HeatmapTrace) Plot { - p.traces << trace - return p + return p.add_trace(trace) } +// surface adds a surface trace to the plot +[inline] pub fn (mut p Plot) surface(trace SurfaceTrace) Plot { - p.traces << trace - return p + return p.add_trace(trace) } +// scatter3d adds a scatter3d trace to the plot. +// If the z value is a 2D array, it is flattened +// to a 1D array +[inline] pub fn (mut p Plot) scatter3d(trace Scatter3DTrace) Plot { mut next_trace := trace z := next_trace.z if z is [][]f64 { next_trace.z = arrays.flatten(z) } - p.traces << next_trace - return p + return p.add_trace(next_trace) } +// bar adds a bar trace to the plot +[inline] pub fn (mut p Plot) bar(trace BarTrace) Plot { - p.traces << trace - return p + return p.add_trace(trace) } +// histogram adds a histogram trace to the plot +[inline] pub fn (mut p Plot) histogram(trace HistogramTrace) Plot { - p.traces << trace - return p + return p.add_trace(trace) } +// box adds a box trace to the plot +[inline] pub fn (mut p Plot) annotation(annotation Annotation) Plot { p.layout.annotations << annotation return p } +// layout sets the layout of the plot +[inline] pub fn (mut p Plot) layout(layout Layout) Plot { mut next_layout := layout // Ensure that the layout range is specified correctly diff --git a/plot/plot_test.v b/plot/plot_test.v index a0e7b7bf6..e2da42fcb 100644 --- a/plot/plot_test.v +++ b/plot/plot_test.v @@ -1,7 +1,7 @@ module plot fn test_bar() { - mut plt := new_plot() + mut plt := Plot.new() plt.bar( x: ['China', 'India', 'USA', 'Indonesia', 'Pakistan']