|
11 | 11 | - README.footer.org, copied verbatim
|
12 | 12 |
|
13 | 13 | The main module name must be passed in as the first cmdline argument.
|
| 14 | +
|
| 15 | +If we want to regenerate the figures linked in the README.org documentation, |
| 16 | +pass "DOCUMENTATION-PLOTS" as the first argument |
| 17 | +
|
14 | 18 | '''
|
15 | 19 |
|
16 | 20 | import sys
|
| 21 | +import os.path |
| 22 | + |
| 23 | +def generate_plots(): |
| 24 | + r'''Makes plots in the docstring of the gnuplotlib.py module''' |
| 25 | + |
| 26 | + import numpy as np |
| 27 | + import gnuplotlib as gp |
| 28 | + |
| 29 | + x = np.arange(101) - 50 |
| 30 | + gp.plot(x**2, |
| 31 | + hardcopy='basic-parabola-plot-pops-up.svg') |
| 32 | + |
| 33 | + g1 = gp.gnuplotlib(title = 'Parabola with error bars', |
| 34 | + _with = 'xyerrorbars', |
| 35 | + hardcopy = 'parabola-with-x-y-errobars-pops-up-in-a-new-window.svg') |
| 36 | + g1.plot( x**2 * 10, np.abs(x)/10, np.abs(x)*5, |
| 37 | + legend = 'Parabola', |
| 38 | + tuplesize = 4 ) |
| 39 | + |
| 40 | + x,y = np.ogrid[-10:11,-10:11] |
| 41 | + gp.plot( x**2 + y**2, |
| 42 | + title = 'Heat map', |
| 43 | + unset = 'grid', |
| 44 | + cmds = 'set view map', |
| 45 | + _with = 'image', |
| 46 | + tuplesize = 3, |
| 47 | + hardcopy = 'Heat-map-pops-up-where-first-parabola-used-to-be.svg') |
| 48 | + |
| 49 | + theta = np.linspace(0, 6*np.pi, 200) |
| 50 | + z = np.linspace(0, 5, 200) |
| 51 | + g2 = gp.gnuplotlib(_3d = True, |
| 52 | + hardcopy = 'Two-3D-spirals-together-in-a-new-window.svg') |
| 53 | + g2.plot( np.cos(theta), |
| 54 | + np.vstack((np.sin(theta), -np.sin(theta))), |
| 55 | + z ) |
| 56 | + |
| 57 | + x = np.arange(1000) |
| 58 | + gp.plot( (x*x, dict(histogram=1, |
| 59 | + binwidth =10000)), |
| 60 | + (x*x, dict(histogram='cumulative', y2=1)), |
| 61 | + hardcopy = 'A-density-and-cumulative-histogram-of-x-2-are-plotted-on-the-same-plot.svg' ) |
| 62 | + |
| 63 | + gp.plot( (x*x, dict(histogram=1, |
| 64 | + binwidth =10000)), |
| 65 | + (x*x, dict(histogram='cumulative')), |
| 66 | + _xmin=0, _xmax=1e6, |
| 67 | + multiplot='title "multiplot histograms" layout 2,1', |
| 68 | + _set='lmargin at screen 0.05', |
| 69 | + hardcopy = 'Same-histograms-but-plotted-on-two-separate-plots.svg') |
| 70 | + |
| 71 | + |
17 | 72 |
|
18 | 73 | try:
|
19 |
| - modname = sys.argv[1] |
| 74 | + arg1 = sys.argv[1] |
20 | 75 | except:
|
21 |
| - raise Exception("Need main module name as the first cmdline arg") |
| 76 | + raise Exception("Need main module name or 'DOCUMENTATION-PLOTS' as the first cmdline arg") |
22 | 77 |
|
| 78 | +if arg1 == 'DOCUMENTATION-PLOTS': |
| 79 | + generate_plots() |
| 80 | + sys.exit(0) |
| 81 | + |
| 82 | + |
| 83 | +modname = arg1 |
23 | 84 | exec( 'import {} as mod'.format(modname) )
|
24 | 85 |
|
25 | 86 | import inspect
|
@@ -86,6 +147,22 @@ def write(s, verbatim):
|
86 | 147 | sio = StringIO(s)
|
87 | 148 | for l in sio:
|
88 | 149 |
|
| 150 | + # if we have a figure made with DOCUMENTATION-PLOTS, place it |
| 151 | + m = re.match(r'^ \[ (.*) \]$', l) |
| 152 | + if m is not None: |
| 153 | + tag = m.group(1) |
| 154 | + tag = re.sub(r'[^a-zA-Z0-9_]+','-', tag) |
| 155 | + plot_filename = f"{tag}.svg" |
| 156 | + if os.path.isfile(plot_filename): |
| 157 | + if in_quote is not None: |
| 158 | + if in_quote == 'example': f.write('#+END_EXAMPLE\n') |
| 159 | + else: f.write('#+END_SRC\n') |
| 160 | + f.write(f"[[file:{plot_filename}]]\n") |
| 161 | + if in_quote is not None: |
| 162 | + if in_quote == 'example': f.write('#+BEGIN_EXAMPLE\n') |
| 163 | + else: f.write('#+BEGIN_SRC python\n') |
| 164 | + continue |
| 165 | + |
89 | 166 | # handle links
|
90 | 167 | l = re.sub( r"([^ ]+) *\((https?://[^ ]+)\)",
|
91 | 168 | r"[[\2][\1]]",
|
|
0 commit comments