Skip to content

Commit c386234

Browse files
committed
Add multi plot grid support
1 parent b0df382 commit c386234

File tree

4 files changed

+60
-9
lines changed

4 files changed

+60
-9
lines changed

docs/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,22 @@ figure6.dpi = 180
146146
```
147147

148148
![plot](custom_size.png)
149+
150+
### side-by-side plots
151+
152+
```nim
153+
let figure8 = newFigure()
154+
figure8.grid = (rows: 1, cols: 2)
155+
figure8.size = (8.0, 4.0)
156+
figure8.add newLinePlot[int,float](x, y)
157+
figure8.subplot
158+
figure8.add lp6
159+
figure8.add hl6
160+
for i in 0..6:
161+
let vl6 = newVerticalLine(PI * float(i) * 50.0)
162+
vl6.linestyle = "-."
163+
vl6.colour = "blue"
164+
figure8.add vl6
165+
```
166+
167+
![plot](side_by_side.png)

docs/examples.nim

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,19 @@ figure7.save("docs/annotation.png")
8989
# dpi
9090
figure6.size = (20.0, 2.0)
9191
figure6.dpi = 180
92-
figure6.save("docs/custom_size.png")
92+
figure6.save("docs/custom_size.png")
93+
94+
# multiple plots
95+
let figure8 = newFigure()
96+
figure8.grid = (rows: 1, cols: 2)
97+
figure8.size = (8.0, 4.0)
98+
figure8.add newLinePlot[int,float](x, y)
99+
figure8.subplot
100+
figure8.add lp6
101+
figure8.add hl6
102+
for i in 0..6:
103+
let vl6 = newVerticalLine(PI * float(i) * 50.0)
104+
vl6.linestyle = "-."
105+
vl6.colour = "blue"
106+
figure8.add vl6
107+
figure8.save("docs/side_by_side.png")

docs/side_by_side.png

37.1 KB
Loading

src/matplotnim.nim

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ type
1818
python*: string
1919
script*: seq[string]
2020
latex*: bool
21-
plots*: seq[Plot]
21+
plots*: seq[seq[Plot]]
2222
font*: tuple[family: string, style: string]
2323
dpi*: int
2424
size*: tuple[w: float, h: float]
25+
grid*: tuple[rows: int, cols: int]
26+
index: int
2527

2628
proc save*(figure: Figure, dest: string) =
2729
var script = newSeq[string]()
@@ -39,8 +41,15 @@ proc save*(figure: Figure, dest: string) =
3941
script.add &"fig = plt.figure(figsize=({figure.size.w},{figure.size.h}))"
4042
else:
4143
script.add "fig = plt.figure()"
42-
for p in figure.plots:
43-
script.add p.render
44+
for i in 0..<len(figure.plots):
45+
echo &"i = {i}"
46+
echo &"plt.subplot({figure.grid.rows}, {figure.grid.cols}, {i+1})"
47+
script.add &"plt.subplot({figure.grid.rows}, {figure.grid.cols}, {i+1})"
48+
let plots = figure.plots[i]
49+
echo &"#plots = {len(plots)}"
50+
for j in 0..<len(plots):
51+
echo &"j = {j}"
52+
script.add plots[j].render
4453
if figure.dpi > 0:
4554
script.add fmt"plt.savefig('{dest}', format='png', transparent=False, dpi={figure.dpi})"
4655
else:
@@ -51,20 +60,28 @@ proc save*(figure: Figure, dest: string) =
5160
writeFile(name, script_str)
5261
echo name
5362
discard execShellCmd fmt"/usr/local/bin/python3 {name}"
54-
55-
5663

5764
proc newFigure*(): Figure =
5865
Figure(python: "/usr/local/bin/python3",
5966
script: newSeq[string](),
6067
latex: false,
6168
font: ("", ""),
6269
dpi: 0,
63-
size: (0.0, 0.0))
70+
size: (0.0, 0.0),
71+
grid: (1, 1),
72+
index: 0,
73+
plots: @[])
6474

6575
proc add*(figure: Figure, plot: Plot) =
66-
figure.plots.add plot
67-
76+
if len(figure.plots)==0:
77+
figure.plots.add @[]
78+
echo &"Add plot to {figure.index}"
79+
figure.plots[figure.index].add plot
80+
81+
proc subplot*(figure: Figure) =
82+
figure.plots.add @[]
83+
figure.index += 1
84+
6885
# Line plots
6986
type LinePlot[A, B] = ref object of Plot
7087
linestyle*: string

0 commit comments

Comments
 (0)