add org matplotlib example

This commit is contained in:
2022-12-04 21:32:53 +01:00
parent 382206928c
commit c7bfa7d9f3
3 changed files with 3376 additions and 2 deletions

File diff suppressed because it is too large Load Diff

After

Width:  |  Height:  |  Size: 50 KiB

File diff suppressed because it is too large Load Diff

After

Width:  |  Height:  |  Size: 52 KiB

View File

@@ -359,7 +359,7 @@ print(result)
|---------+-------|
** Figures
*** svg
*** Include svg
#+NAME: fig-svg
#+CAPTION: svg figure
#+ATTR_ORG: :width 100
@@ -367,7 +367,7 @@ print(result)
[[/usr/share/icons/hicolor/scalable/apps/emacs.svg]]
See figure ref:fig-svg-import as a reference.
*** png
*** Include png
#+NAME: fig-png
#+CAPTION: png figure
#+ATTR_ORG: :width 100
@@ -414,6 +414,78 @@ See figure ref:fig-svg-import as a reference.
[[file:test-tikz.svg]]
#+end_figure
*** python using matplotlib
SRC block header =:prologue (mpl-prologue)= for non-latex exports (.svg
exports) to loads my.mplstyle including kpfonts. Other style via
optional argument, e.g. =:prologue (mpl-prologue "my-tex")= rendering
via latex including kpfonts.
SRC block header =:var fname=(mpl-var "<file_name>")= to export figure
as .pgf for latex and .svg otherwise, like HTML.
**** using my.mplstyle
#+HEADER: :prologue (mpl-prologue)
#+HEADER: :var fname=(mpl-var "img/figure-matplotlib")
#+begin_src python :results output file
import math as m
import matplotlib as mpl
n = 30
t = [i/n*m.pi for i in range(n+1)]
fig, ax = plt.subplots()
[ax.plot(t, [m.sin(i+j/12*m.pi) for i in t], color=f'C{j}', label=f'$C_{j}$') for j in range(10)]
ax.legend()
ax.set_title(r'Functions: $\sin(x + \pi \, i/12)$')
ax.set_xlabel(r'$x ~ \times \pi$')
ax.set_ylabel('$y$')
ax.set_xticks([0, m.pi/4, m.pi/2, m.pi*3/4, m.pi])
ax.set_xticklabels([0.00, 0.25, 0.50, 0.75, 1.00])
ax.xaxis.set_minor_locator(mpl.ticker.AutoMinorLocator(3))
ax.grid()
ax.grid(which='minor', linestyle=':')
fig.savefig(fname)
plt.close()
print(fname, end='')
#+end_src
#+ATTR_ORG: :width 500
#+RESULTS:
[[file:img/figure-matplotlib.svg]]
**** using my-tex.mplstyle
#+HEADER: :prologue (mpl-prologue "my-tex")
#+HEADER: :var fname=(mpl-var "img-matplotlib-tex")
#+begin_src python :results output file
import math as m
import matplotlib as mpl
n = 30
t = [i/n*m.pi for i in range(n+1)]
fig, ax = plt.subplots()
[ax.plot(t, [m.sin(i+j/12*m.pi) for i in t], color=f'C{j}', label=f'$C_{j}$') for j in range(10)]
ax.legend()
ax.set_title(r'Functions: $\sin(x + \pi \, i/12)$')
ax.set_xlabel(r'$x ~ \times \pi$')
ax.set_ylabel('$y$')
ax.set_xticks([0, m.pi/4, m.pi/2, m.pi*3/4, m.pi])
ax.set_xticklabels([0.00, 0.25, 0.50, 0.75, 1.00])
ax.xaxis.set_minor_locator(mpl.ticker.AutoMinorLocator(3))
ax.grid()
ax.grid(which='minor', linestyle=':')
fig.savefig(fname)
plt.close()
print(fname, end='')
#+end_src
#+ATTR_ORG: :width 500
#+RESULTS:
[[file:img-matplotlib-tex.svg]]
** Example / Verbatim
#+BEGIN_EXAMPLE
verbatime example