Skip to content

Commit

Permalink
Updated plotly usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ulises-jeremias committed Oct 17, 2023
1 parent 02a0a10 commit d2f4bd3
Show file tree
Hide file tree
Showing 32 changed files with 66 additions and 46 deletions.
4 changes: 2 additions & 2 deletions examples/data_analysis_example/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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}'}
Expand Down
2 changes: 1 addition & 1 deletion examples/fft_plot_example/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_annotated_pie_chart/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_bar/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_basic_heatmap/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_bubble_chart/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_grouped_bar_chart/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_heatmap_golden_ratio/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_histogram/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_line_plot_with_areas/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_pie/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_ripple_surface/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn main() {
z << row
}

mut plt := plot.new_plot()
mut plt := plot.Plot.new()
plt.surface(
x: x
y: y
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_saddle_surface/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {
z << row
}

mut plt := plot.new_plot()
mut plt := plot.Plot.new()
plt.surface(
x: x
y: y
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_scatter/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_scatter3d_1/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_scatter3d_2/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main() {
z << val
}

mut plt := plot.new_plot()
mut plt := plot.Plot.new()
plt.scatter3d(
x: x
y: y
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_scatter3d_easing/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_scatter_colorscale/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_scatter_easing/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_scatter_with_bars/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_scatter_with_histogram/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_scatter_with_regression/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_shaded_area_sin/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_sin_cos_surface/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {
z << row
}

mut plt := plot.new_plot()
mut plt := plot.Plot.new()
plt.surface(
x: x
y: y
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_surface/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn main() {
z << row
}

mut plt := plot.new_plot()
mut plt := plot.Plot.new()
plt.surface(
x: x
y: y
Expand Down
2 changes: 1 addition & 1 deletion examples/plot_surface_easing/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/roots_bisection_solver/main.v
Original file line number Diff line number Diff line change
Expand Up @@ -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{}))
Expand Down
2 changes: 1 addition & 1 deletion ml/kmeans.v
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
Expand Down
2 changes: 1 addition & 1 deletion ml/knn.v
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
Expand Down
2 changes: 1 addition & 1 deletion ml/linreg.v
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
Expand Down
48 changes: 34 additions & 14 deletions plot/plot.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion plot/plot_test.v
Original file line number Diff line number Diff line change
@@ -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']
Expand Down

0 comments on commit d2f4bd3

Please sign in to comment.