Skip to content

Commit

Permalink
1 basic plotting example with axis titles and 3 more ones using scrip…
Browse files Browse the repository at this point in the history
…t mode (#205)
  • Loading branch information
fermarsan committed Jun 9, 2024
1 parent b66e4d9 commit 91a3e7b
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
40 changes: 40 additions & 0 deletions examples/plot_line_axis_titles/main.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module main

import math
import vsl.plot

fn main() {
x := []f64{len: 100, init: f64(index) * 0.1}
y1 := x.map(math.sin(it))
y2 := x.map(math.cos(it))

mut plt := plot.Plot.new()
plt.scatter(
x: x
y: y1
mode: 'lines'
line: plot.Line{
color: '#FF0000'
}
name: 'sin(x)'
)
plt.scatter(
x: x
y: y2
mode: 'lines'
line: plot.Line{
color: '#0000FF'
}
name: 'cos(x)'
)
plt.layout(
title: 'Line Plot with axis titles'
xaxis: plot.Axis {
title: plot.AxisTitle { 'x' }
}
yaxis: plot.Axis {
title: plot.AxisTitle { 'f(x)' }
}
)
plt.show()!
}
30 changes: 30 additions & 0 deletions examples/script_mode_ac_signal/main.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import vsl.plot
import vsl.util
import math


f := 60.0 // Hz
beta := 0.5 // rad
a0 := 10 // V

t := util.lin_space(0, 2*(1/f), 150) // 2 periods
y := t.map(math.sin(2*math.pi*f*it + beta)) // AC signal: y = A₀·sin(2πft + β)

mut plt := plot.Plot.new()

plt.scatter(
x: t
y: y
)

plt.layout(
title: 'AC signal (60Hz)'
xaxis: plot.Axis {
title: plot.AxisTitle { 'time [s]' }
}
yaxis: plot.Axis {
title: plot.AxisTitle { 'amplitude [V]' }
}
)

plt.show()!
16 changes: 16 additions & 0 deletions examples/script_mode_simple_plot/main.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import vsl.plot
import vsl.util
import math


t := util.lin_space(-math.pi, math.pi, 50)
y := []f64{len: t.len, init: math.sin(t[index])}

mut plt := plot.Plot.new()

plt.scatter(
x: t
y: y
)

plt.show()!
44 changes: 44 additions & 0 deletions examples/script_mode_three_phase_signal/main.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import vsl.plot
import vsl.util
import math


f := 60.0 // Hz
a0 := 100 // V

t := util.lin_space(0, 2*(1/f), 100) // 2 periods
y1 := t.map(a0*math.sin(2*math.pi*f*it)) // y₁ = A₀·sin(2πft)
y2 := t.map(a0*math.sin(2*math.pi*f*it + 2*math.pi/3)) // y₂ = A₀·sin(2πft + 2π/3)
y3 := t.map(a0*math.sin(2*math.pi*f*it + 4*math.pi/3)) // y₃ = A₀·sin(2πft + 4π/3)

mut plt := plot.Plot.new()

plt.scatter(
x: t
y: y1
name: 'y1(t)'
)

plt.scatter(
x: t
y: y2
name: 'y2(t)'
)

plt.scatter(
x: t
y: y3
name: 'y3(t)'
)

plt.layout(
title: 'Three-phase system (60Hz)'
xaxis: plot.Axis {
title: plot.AxisTitle { 'time [s]' }
}
yaxis: plot.Axis {
title: plot.AxisTitle { 'amplitude [V]' }
}
)

plt.show()!

0 comments on commit 91a3e7b

Please sign in to comment.