Skip to content

Commit d038e1e

Browse files
committed
Add scatter plot
1 parent b457f2b commit d038e1e

File tree

5 files changed

+76
-11
lines changed

5 files changed

+76
-11
lines changed

docs/README.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,21 @@
77
```nim
88
let figure = newFigure()
99
figure.add newLinePlot[int,float](@[1, 2, 3, 4], @[5.5, 7.6, 11.1, 6.5])
10-
figure.save("docs/lineplot_default.png")
1110
```
1211

13-
![plot](lineplot_default.png)
12+
![plot](lineplot_default.png)
13+
14+
### line, scatter with options
15+
16+
```nim
17+
let figure2 = newFigure()
18+
let lp = newLinePlot[int,float](x, y)
19+
lp.linestyle = "--"
20+
lp.colour = "red"
21+
figure2.add lp
22+
let sp = newScatterPlot[int,float](x, y)
23+
sp.colour = "green"
24+
figure2.add sp
25+
```
26+
27+
![plot](scatterplot_default.png)

docs/examples.nim

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
import "../src/matplotnim"
22

3+
let x = @[1, 2, 3, 4]
4+
let y = @[5.5, 7.6, 11.1, 6.5]
5+
36
let figure = newFigure()
4-
figure.add newLinePlot[int,float](@[1, 2, 3, 4], @[5.5, 7.6, 11.1, 6.5])
5-
figure.save("docs/lineplot_default.png")
7+
figure.add newLinePlot[int,float](x, y)
8+
figure.save("docs/lineplot_default.png")
9+
10+
let figure2 = newFigure()
11+
let lp = newLinePlot[int,float](x, y)
12+
lp.linestyle = "--"
13+
lp.colour = "red"
14+
figure2.add lp
15+
let sp = newScatterPlot[int,float](x, y)
16+
sp.colour = "green"
17+
figure2.add sp
18+
figure2.save("docs/scatterplot_default.png")

docs/scatterplot_default.png

16.1 KB
Loading

matplotnim.nimble

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# Package
22

33
version = "0.1.0"
4-
author = "Rui Vieira"
5-
description = "A Nim wrapper for Python\'s matplotlib"
4+
author = "Rui Vieira"
5+
description = "Nim wrapper for matplotlib"
66
license = "Apache-2.0"
77
srcDir = "src"
88

99

1010
# Dependencies
1111

1212
requires "nim >= 0.19.4"
13-
require "tempfile"
13+
requires "tempfile"

src/matplotnim.nim

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import strformat
44
import tempfile
55
import os
66

7+
func numberToStr[T](data: seq[T]): seq[string] = data.map(proc(k:T):string = $k)
8+
9+
func makeList[T](data: seq[T]): string =
10+
let str = numberToStr(data).join(",")
11+
return fmt"[{str}]"
12+
713
type Plot = ref object of RootObj
814
method render(this: Plot): string {.base.} = ""
915

@@ -43,14 +49,46 @@ proc newFigure*(): Figure =
4349
proc add*(figure: Figure, plot: Plot) =
4450
figure.plots.add plot
4551

52+
# Line plots
4653
type LinePlot[A, B] = ref object of Plot
4754
linestyle*: string
55+
colour*: string
4856
x*: seq[A]
4957
y*: seq[B]
5058
method render[A,B](this: LinePlot[A,B]): string =
51-
let xs = this.x.map(proc(k:A):string = $k).join(",")
52-
let ys = this.y.map(proc(k:B):string = $k).join(",")
53-
return fmt"plt.plot([{xs}],[{ys}])"
59+
let xs = makeList(this.x)
60+
let ys = makeList(this.y)
61+
var options: seq[string] = @[]
62+
if this.linestyle!="":
63+
options.add fmt"linestyle='{this.linestyle}'"
64+
if this.colour!="":
65+
options.add fmt"color='{this.colour}'"
66+
if len(options)>0:
67+
let optstr = options.join(",")
68+
return fmt"plt.plot({xs},{ys},{optstr})"
69+
else:
70+
return fmt"plt.plot({xs},{ys})"
5471

5572
proc newLinePlot*[A,B](x: seq[A], y: seq[B]): LinePlot[A, B] =
56-
LinePlot[A, B](linestyle: "-", x: x, y: y)
73+
LinePlot[A, B](linestyle: "", colour: "", x: x, y: y)
74+
75+
# Scatter plots
76+
type ScatterPlot[A,B] = ref object of Plot
77+
colour*: string
78+
x*: seq[A]
79+
y*: seq[B]
80+
method render[A,B](this: ScatterPlot[A,B]): string =
81+
let xs = makeList(this.x)
82+
let ys = makeList(this.y)
83+
var options: seq[string] = @[]
84+
if this.colour!="":
85+
options.add fmt"color='{this.colour}'"
86+
if len(options)>0:
87+
let optstr = options.join(",")
88+
return fmt"plt.scatter({xs},{ys},{optstr})"
89+
else:
90+
return fmt"plt.scatter({xs},{ys})"
91+
92+
proc newScatterPlot*[A,B](x: seq[A], y: seq[B]): ScatterPlot[A, B] =
93+
ScatterPlot[A, B](colour: "", x: x, y: y)
94+

0 commit comments

Comments
 (0)