Skip to content

Commit 426953a

Browse files
committed
Add histograms
1 parent 61eedc0 commit 426953a

File tree

5 files changed

+52
-2
lines changed

5 files changed

+52
-2
lines changed

docs/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,25 @@ figure2.save("docs/marker.png")
3434
```
3535

3636
![plot](marker.png)
37+
38+
### histograms
39+
40+
With default values:
41+
42+
```nim
43+
let samples = rnorm(1000, 0.0, 2.0)
44+
let figure3 = newFigure()
45+
let hist = newHistogram[float] samples
46+
figure3.add hist
47+
```
48+
49+
![plot](hist_default.png)
50+
51+
With custom number of bins:
52+
53+
```nim
54+
hist.bins = 200
55+
figure3.save "docs/hist_bins.png"
56+
```
57+
58+
![plot](hist_bins.png)

docs/examples.nim

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import "../src/matplotnim"
2+
import "../../nim-science/science/Distributions"
23

34
let x = @[1, 2, 3, 4]
45
let y = @[5.5, 7.6, 11.1, 6.5]
@@ -18,4 +19,13 @@ figure2.add sp
1819
figure2.save("docs/scatterplot_default.png")
1920

2021
sp.marker = "*"
21-
figure2.save("docs/marker.png")
22+
figure2.save("docs/marker.png")
23+
24+
let samples = rnorm(1000, 0.0, 2.0)
25+
let figure3 =newFigure()
26+
let hist = newHistogram[float] samples
27+
figure3.add hist
28+
figure3.save "docs/hist_default.png"
29+
30+
hist.bins = 200
31+
figure3.save "docs/hist_bins.png"

docs/hist_bins.png

9.78 KB
Loading

docs/hist_default.png

8.33 KB
Loading

src/matplotnim.nim

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,22 @@ method render[A,B](this: ScatterPlot[A,B]): string =
9494

9595
proc newScatterPlot*[A,B](x: seq[A], y: seq[B]): ScatterPlot[A, B] =
9696
ScatterPlot[A, B](colour: "", x: x, y: y)
97-
97+
98+
# Histogram
99+
type Histogram[A] = ref object of Plot
100+
x*:seq[A]
101+
bins*: int
102+
method render[A](this: Histogram[A]): string =
103+
let xs = makeList(this.x)
104+
var options: seq[string] = @[]
105+
if this.bins>0:
106+
options.add fmt"bins={this.bins}"
107+
if len(options)>0:
108+
let optstr = options.join(",")
109+
return fmt"plt.hist({xs},{optstr})"
110+
else:
111+
return fmt"plt.hist({xs})"
112+
113+
proc newHistogram*[A](x: seq[A]): Histogram[A] =
114+
Histogram[A](bins: 0, x: x)
115+

0 commit comments

Comments
 (0)