Skip to content

Commit f213627

Browse files
committed
Add density plots (see #1)
1 parent 82d0221 commit f213627

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
A Nim wrapper for matplotlib.
44

5-
# Features
5+
## Features
66

77
* Line plots
88
* Scatter plots
9-
* Histograms
9+
* Histograms (and KDE denisty plots)
1010
* Line segments
1111
* Axis (horizontal and vertical) lines
1212
* Font customisation
@@ -16,14 +16,14 @@ A Nim wrapper for matplotlib.
1616
* Horizontal and vertical limits
1717
* Custom markers
1818

19-
# Examples
19+
## Examples
2020

2121
Examples can be found [here](docs/README.md).
2222

23-
# Contributing
23+
## Contributing
2424

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
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

docs/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,19 @@ figure3.save "docs/hist_bins.png"
5757

5858
![](hist_bins.png)
5959

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+
6073
### line segments
6174

6275
With default values:

docs/density1.png

24.2 KB
Loading

src/matplotnim.nim

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ proc save*(figure: Figure, dest: string) =
5757
let script_str = script.join("\n")
5858
# get the temporary file
5959
var (file, name) = mkstemp(suffix = ".py")
60+
echo name
6061
writeFile(name, script_str)
6162
echo name
6263
discard execShellCmd fmt"/usr/local/bin/python3 {name}"
@@ -146,6 +147,32 @@ method render[A](this: Histogram[A]): string =
146147
proc newHistogram*[A](x: seq[A]): Histogram[A] =
147148
Histogram[A](bins: 0, x: x)
148149

150+
type Density[A] = ref object of Plot
151+
x*:seq[A]
152+
steps*: int
153+
method render[A](this: Density[A]): string =
154+
let xs = makeList(this.x)
155+
156+
var s: seq[string] = @[]
157+
s.add("from scipy import stats")
158+
s.add("import numpy as np")
159+
s.add(fmt"_data = {xs}")
160+
let x_max = foldr(this.x, max(a, b))
161+
let x_min = foldr(this.x, min(a, b))
162+
163+
let steps = abs(x_max - x_min) / float(this.steps)
164+
s.add("_density = stats.kde.gaussian_kde(_data)")
165+
echo fmt"x_max = {x_max}, x_min = {x_min}, steps = {steps}"
166+
s.add(fmt"_x = np.arange({x_min}, {x_max}, {steps})")
167+
168+
s.add("plt.plot(_x, _density(_x))")
169+
170+
return s.join("\n")
171+
172+
proc newDensity*[A](x: seq[A]): Density[A] =
173+
Density[A](steps: 100, x: x)
174+
175+
149176
# Line segments
150177
type Line[A,B] = ref object of Plot
151178
p0*: tuple[x: A, y: B]

0 commit comments

Comments
 (0)