Skip to content

Commit 69f7a4c

Browse files
committed
Use nimpy
1 parent c16d60e commit 69f7a4c

18 files changed

+78
-425
lines changed

README.md

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,53 @@
1+
[![CI](https://github.com/ruivieira/matplotnim/actions/workflows/test.yml/badge.svg)](https://github.com/ruivieira/matplotnim/actions/workflows/test.yml) [![builds.sr.ht status](https://builds.sr.ht/~ruivieira/matplotnim/commits/.build.yml.svg)](https://builds.sr.ht/~ruivieira/matplotnim/commits/.build.yml?)
2+
13
# matplotnim
24

35
A Nim wrapper for matplotlib.
46

7+
## 🔮 v0.2.0 and the Future
8+
9+
Most of `v0.1.0` is now deprecated and redundant with the creation of
10+
the great [`nimpy`](https://github.com/yglukhov/nimpy).
11+
With `nimpy` (almost) all of the constructs of `matplotlib` can now
12+
be called directly.
13+
14+
Nevertheless, I will keep this package for two reasons:
15+
16+
1. Hopefully it will serve as a learning resource, with examples, on how to call
17+
`matplotlib` using `nimpy`
18+
2. Some complex plots can be very verbose (not Nim's fault, by the way) and this
19+
will be a package aiming at providing premade templates for such plots.
20+
521
## Features
622

7-
* Line plots
8-
* Scatter plots
9-
* Histograms (and KDE denisty plots)
10-
* Line segments
11-
* Axis (horizontal and vertical) lines
12-
* Font customisation
13-
* Annotations
14-
* Custom size and DPI
15-
* Sub-plots
16-
* Horizontal and vertical limits
17-
* Custom markers
23+
- Line plots
24+
- Scatter plots
25+
- Histograms (and KDE denisty plots)
26+
- Line segments
27+
- Axis (horizontal and vertical) lines
28+
- Font customisation
29+
- Annotations
30+
- Custom size and DPI
31+
- Sub-plots
32+
- Horizontal and vertical limits
33+
- Custom markers
1834

1935
## Examples
2036

2137
Examples can be found [here](docs/README.md).
2238

2339
## Contributing
2440

25-
* Fork it (https://github.com/ruivieira/matplotnim)
26-
* Create your feature branch (`git checkout -b my-new-feature`)
27-
* Commit your changes (`git commit -am 'Add some feature'`)
28-
* Push to the branch (`git push origin my-new-feature`)
29-
* Create a new Pull Request
41+
- Fork it (https://github.com/ruivieira/matplotnim)
42+
- Create your feature branch (`git checkout -b my-new-feature`)
43+
- Commit your changes (`git commit -am 'Add some feature'`)
44+
- Push to the branch (`git push origin my-new-feature`)
45+
- Create a new Pull Request
46+
47+
## mailing lists
48+
49+
- Announcements: [https://lists.sr.ht/~ruivieira/nim-announce](https://lists.sr.ht/~ruivieira/nim-announce)
50+
- Discussion: [https://lists.sr.ht/~ruivieira/nim-discuss](https://lists.sr.ht/~ruivieira/nim-discuss)
51+
- Development: [https://lists.sr.ht/~ruivieira/nim-devel](https://lists.sr.ht/~ruivieira/nim-devel)
52+
53+
Please prefix the subject with `[matplotnim]`.

docs/README.md

Lines changed: 36 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,31 @@
55
### line, scatter with defaults
66

77
```nim
8-
let figure = newFigure()
9-
figure.add newLinePlot[int,float](@[1, 2, 3, 4], @[5.5, 7.6, 11.1, 6.5])
8+
let x = @[1, 2, 3, 4]
9+
let y = @[5.5, 7.6, 11.1, 6.5]
10+
11+
discard plt.figure()
12+
discard plt.plot(x, y)
1013
```
1114

1215
![](lineplot_default.png)
1316

1417
### line, scatter with options
1518

1619
```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
20+
discard plt.figure()
21+
discard plt.plot(x, y, linestyle="--", c="red")
22+
discard plt.scatter(x, y, c="green")
2523
```
2624

2725
![](scatterplot_default.png)
2826

2927
Custom markers:
3028

3129
```nim
32-
sp.marker = "*"
33-
figure2.save("docs/marker.png")
30+
discard plt.figure()
31+
discard plt.plot(x, y, linestyle="--", c="red")
32+
discard plt.scatter(x, y, c="green", marker="*")
3433
```
3534

3635
![](marker.png)
@@ -41,110 +40,70 @@ With default values:
4140

4241
```nim
4342
let samples = rnorm(1000, 0.0, 2.0)
44-
let figure3 = newFigure()
45-
let hist = newHistogram[float] samples
46-
figure3.add hist
43+
discard plt.figure()
44+
discard plt.hist(samples)
4745
```
4846

4947
![](hist_default.png)
5048

5149
With custom number of bins:
5250

5351
```nim
54-
hist.bins = 200
55-
figure3.save "docs/hist_bins.png"
52+
discard plt.figure()
53+
discard plt.hist(samples, bins=200)
5654
```
5755

5856
![](hist_bins.png)
5957

60-
You can also create KDE density plots.
61-
For instance:
62-
63-
```nim
64-
let x = rnorm(1000, 0.0, 2.0)
65-
let figure = newFigure()
66-
let dp = newDensity(x)
67-
figure.add dp
68-
figure.save("docs/density.png")
69-
```
70-
71-
![](density1.png)
72-
7358
### line segments
7459

7560
With default values:
7661

7762
```nim
78-
let figure4 = newFigure()
63+
discard plt.figure()
7964
let x4 = toSeq(0..100)
8065
let y4 = x4.map(proc(k:int):float = float(k).pow(2.0))
81-
figure4.add newLinePlot(x4, y4)
82-
figure4.add newLine((2, 4.0),(70, 70.0.pow(2.0)))
66+
discard plt.plot(x4, y4)
67+
discard plt.plot((2, 70.0),(4.0, 70.0.pow(2.0)))
8368
```
8469

8570
![](line_segment.png)
8671

8772
Customised:
8873

8974
```nim
90-
lp4.colour = "black"
91-
line.colour = "red"
92-
line.linestyle = "--"
75+
discard plt.figure()
76+
discard plt.plot(x4, y4, c="black")
77+
discard plt.plot((2, 70.0),(4.0, 70.0.pow(2.0)), c="red", linestyle="--")
9378
```
9479

9580
![](line_segment_colour.png)
9681

97-
### titles and fonts
98-
99-
A plot with a title and custom font ("courier new" in this case):
82+
### vertical and horizontal lines
10083

10184
```nim
102-
let figure5 = newFigure()
85+
discard plt.figure()
10386
let x5 = toSeq(0..1000)
10487
let y5 = x5.map(func(k:int):float = sin(float(k) / 50.0))
105-
figure5.font = ("monospace", "Courier New")
106-
let lp5 = newLinePlot(x5, y5)
107-
lp5.colour = "red"
108-
figure5.add lp5
109-
figure5.add newTitle("A plot with a title (in Courier New).")
110-
```
111-
112-
![](plot_title.png)
113-
114-
### vertical and horizontal lines
115-
116-
```nim
117-
let figure6 = newFigure()
118-
let x6 = toSeq(0..1000)
119-
let y6 = x5.map(func(k:int):float = sin(float(k) / 50.0))
120-
let lp6 = newLinePlot(x5, y5)
121-
lp6.colour = "red"
122-
figure6.add lp6
123-
let hl6 = newHorizontalLine(0)
124-
hl6.linestyle = "--"
125-
hl6.colour = "black"
126-
figure6.add hl6
88+
discard plt.plot(x5, y5, c="red")
89+
discard plt.axhline(y=0, linestyle="--", c="black")
12790
for i in 0..6:
128-
let vl6 = newVerticalLine(PI * float(i) * 50.0)
129-
vl6.linestyle = "-."
130-
vl6.colour = "blue"
131-
figure6.add vl6
91+
discard plt.axvline(x=PI * float(i) * 50.0, linestyle="-.", c="blue")
13292
```
13393

13494
![](plot_hv_lines.png)
13595

13696
### annotations
13797

13898
```nim
139-
let figure7 = newFigure()
140-
figure7.latex = true
99+
discard plt.figure()
100+
# figure7.latex = true
141101
let x7 = @[1, 2, 3, 4]
142102
let y7 = @[5.5, 7.6, 11.1, 6.5]
143-
let lp7 = newScatterPlot(x7, y7)
144-
figure7.add lp7
103+
discard plt.scatter(x7, y7)
104+
var ax = plt.gca()
145105
for i in 0..2:
146-
let ann7 = newAnnotation(float(x7[i]) + 0.1, y7[i] + 0.1, &"$p_{i}$")
147-
figure7.add ann7
106+
discard ax.annotate(&"$p_{i}$", xy=(float(x7[i]) + 0.1, y7[i] + 0.1))
148107
```
149108

150109
![](annotation.png)
@@ -154,64 +113,11 @@ for i in 0..2:
154113
### custom plot size
155114

156115
```nim
157-
figure6.size = (20.0, 2.0)
158-
figure6.dpi = 180
159-
```
160-
161-
![](custom_size.png)
162-
163-
### side-by-side plots
164-
165-
```nim
166-
let figure8 = newFigure()
167-
figure8.grid = (rows: 1, cols: 2)
168-
figure8.size = (8.0, 4.0)
169-
figure8.add newLinePlot[int,float](x, y)
170-
figure8.subplot
171-
figure8.add lp6
172-
figure8.add hl6
116+
discard plt.figure(figsize=(20.0, 2.0), dpi=180)
117+
discard plt.plot(x5, y5, c="red")
118+
discard plt.axhline(y=0, linestyle="--", c="black")
173119
for i in 0..6:
174-
let vl6 = newVerticalLine(PI * float(i) * 50.0)
175-
vl6.linestyle = "-."
176-
vl6.colour = "blue"
177-
figure8.add vl6
120+
discard plt.axvline(x=PI * float(i) * 50.0, linestyle="-.", c="blue")
178121
```
179122

180-
![](side_by_side.png)
181-
182-
### simple grid
183-
184-
```nim
185-
let figure9 = newFigure()
186-
figure9.size = (8.0, 4.0)
187-
figure9.grid = (3, 3)
188-
let x9 = toSeq(0..<300)
189-
for n in 0..<9:
190-
if n > 0:
191-
figure9.subplot
192-
let fn = proc(d:int):float= sin(float(n)*float(d))
193-
let lp = newLinePlot(x9, x9.map(fn))
194-
lp.colour = "red"
195-
figure9.add lp
196-
```
197-
198-
![](grid.png)
199-
200-
### horizontal and vertical limits
201-
202-
```nim
203-
let x10 = rnorm(1000, 0.0, 2.0)
204-
let y10 = rnorm(1000, 0.0, 2.0)
205-
let figure10 = newFigure()
206-
figure10.size = (8.0, 4.0)
207-
figure10.grid = (1, 2)
208-
let sp10 = newScatterPlot(x10, y10)
209-
figure10.add newXLimit(0.0, 1.0)
210-
figure10.add sp10
211-
figure10.subplot
212-
figure10.add newYLimit(-1.0, 0.0)
213-
figure10.add sp10
214-
figure10.save("docs/limits.png")
215-
```
216-
217-
![](limits.png)
123+
![](custom_size.png)

docs/annotation.png

1.41 KB
Loading

docs/custom_size.png

-36.2 KB
Loading

docs/density1.png

-7.48 KB
Loading

docs/grid.png

-11.1 KB
Loading

docs/hist_bins.png

-4.75 KB
Loading

docs/hist_default.png

-3.56 KB
Loading

docs/limits.png

-14.3 KB
Loading

docs/line_segment.png

-2.75 KB
Loading

0 commit comments

Comments
 (0)