Skip to content

Commit 9e01405

Browse files
committed
Add vertical and horizontal lines
1 parent 1cb4002 commit 9e01405

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

docs/README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,26 @@ figure5.add lp5
9696
figure5.add newTitle("A plot with a title (in Courier New).")
9797
```
9898

99-
![plot](plot_title.png)
99+
![plot](plot_title.png)
100+
101+
### vertical and horizontal lines
102+
103+
```nim
104+
let figure6 = newFigure()
105+
let x6 = toSeq(0..1000)
106+
let y6 = x5.map(func(k:int):float = sin(float(k) / 50.0))
107+
let lp6 = newLinePlot(x5, y5)
108+
lp6.colour = "red"
109+
figure6.add lp6
110+
let hl6 = newHorizontalLine(0)
111+
hl6.linestyle = "--"
112+
hl6.colour = "black"
113+
figure6.add hl6
114+
for i in 0..6:
115+
let vl6 = newVerticalLine(PI * float(i) * 50.0)
116+
vl6.linestyle = "-."
117+
vl6.colour = "blue"
118+
figure6.add vl6
119+
```
120+
121+
![plot](plot_hv_lines.png)

docs/plot_hv_lines.png

32.1 KB
Loading

src/matplotnim.nim

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,44 @@ type Title = ref object of Plot
144144
title*: string
145145
method render(this: Title): string = fmt"plt.title(r'{this.title}')"
146146

147-
proc newTitle*(title: string): Title = Title(title: title)
147+
proc newTitle*(title: string): Title = Title(title: title)
148+
149+
# Horizontal line
150+
type HorizontalLine[A] = ref object of Plot
151+
y*: A
152+
linestyle*: string
153+
colour*: string
154+
method render[A](this: HorizontalLine[A]): string =
155+
var options: seq[string] = @[]
156+
if this.linestyle!="":
157+
options.add fmt"linestyle='{this.linestyle}'"
158+
if this.colour!="":
159+
options.add fmt"color='{this.colour}'"
160+
if len(options)>0:
161+
let optstr = options.join(",")
162+
return fmt"plt.axhline(y={this.y}, {optstr})"
163+
else:
164+
return fmt"plt.axhline(y={this.y})"
165+
166+
proc newHorizontalLine*[A](y: A): HorizontalLine[A] =
167+
HorizontalLine[A](y: y, linestyle: "", colour: "")
168+
169+
# Vertical line
170+
type VerticalLine[A] = ref object of Plot
171+
x*: A
172+
linestyle*: string
173+
colour*: string
174+
method render[A](this: VerticalLine[A]): string =
175+
var options: seq[string] = @[]
176+
if this.linestyle!="":
177+
options.add fmt"linestyle='{this.linestyle}'"
178+
if this.colour!="":
179+
options.add fmt"color='{this.colour}'"
180+
if len(options)>0:
181+
let optstr = options.join(",")
182+
return fmt"plt.axvline(x={this.x}, {optstr})"
183+
else:
184+
return fmt"plt.axvline(x={this.x})"
185+
186+
proc newVerticalLine*[A](x: A): VerticalLine[A] =
187+
VerticalLine[A](x: x, linestyle: "", colour: "")

0 commit comments

Comments
 (0)