Skip to content

Commit 1cb4002

Browse files
committed
Add font and title support
1 parent 0831ee4 commit 1cb4002

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

docs/README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,21 @@ line.colour = "red"
7979
line.linestyle = "--"
8080
```
8181

82-
![plot](line_segment_colour.png)
82+
![plot](line_segment_colour.png)
83+
84+
### titles and fonts
85+
86+
A plot with a title and custom font ("courier new" in this case):
87+
88+
```nim
89+
let figure5 = newFigure()
90+
let x5 = toSeq(0..1000)
91+
let y5 = x5.map(func(k:int):float = sin(float(k) / 50.0))
92+
figure5.font = ("monospace", "Courier New")
93+
let lp5 = newLinePlot(x5, y5)
94+
lp5.colour = "red"
95+
figure5.add lp5
96+
figure5.add newTitle("A plot with a title (in Courier New).")
97+
```
98+
99+
![plot](plot_title.png)

docs/plot_title.png

30 KB
Loading

src/matplotnim.nim

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type
1919
script*: seq[string]
2020
latex*: bool
2121
plots*: seq[Plot]
22+
font*: tuple[family: string, style: string]
2223

2324
proc save*(figure: Figure, dest: string) =
2425
var script = newSeq[string]()
@@ -27,7 +28,8 @@ proc save*(figure: Figure, dest: string) =
2728
script.add "from matplotlib import rc"
2829
script.add "import matplotlib.pyplot as plt"
2930
script.add "from matplotlib.lines import Line2D"
30-
# script.add "rc('font', **{'family': '#{@font.family}', 'serif': '#{@font.styles.to_s}'})"
31+
if figure.font.family!="":
32+
script.add "rc('font', **{'family': '" & figure.font.family & "', 'serif': '" & figure.font.style & "'})"
3133
let latex_str = if figure.latex: "True" else: "False"
3234
script.add fmt"rc('text', usetex={latex_str})"
3335
script.add "matplotlib.rcParams['text.latex.unicode']=True"
@@ -44,7 +46,10 @@ proc save*(figure: Figure, dest: string) =
4446

4547

4648
proc newFigure*(): Figure =
47-
Figure(python: "/usr/local/bin/python3", script: newSeq[string](), latex: false)
49+
Figure(python: "/usr/local/bin/python3",
50+
script: newSeq[string](),
51+
latex: false,
52+
font: ("", ""))
4853

4954
proc add*(figure: Figure, plot: Plot) =
5055
figure.plots.add plot
@@ -132,4 +137,11 @@ method render[A,B](this: Line[A,B]): string =
132137
return fmt"ax = plt.gca(){'\n'}ax.add_line(Line2D([{this.p0.x},{this.p1.x}],[{this.p0.y},{this.p1.y}]))"
133138

134139
proc newLine*[A,B](ps: tuple[x: A, y: B], pe: tuple[x: A, y: B]): Line[A, B] =
135-
Line[A,B](p0: ps, p1: pe, linestyle: "", colour: "")
140+
Line[A,B](p0: ps, p1: pe, linestyle: "", colour: "")
141+
142+
# Title
143+
type Title = ref object of Plot
144+
title*: string
145+
method render(this: Title): string = fmt"plt.title(r'{this.title}')"
146+
147+
proc newTitle*(title: string): Title = Title(title: title)

0 commit comments

Comments
 (0)