Skip to content

Commit

Permalink
README.org shows graphical figures
Browse files Browse the repository at this point in the history
  • Loading branch information
dkogan committed Feb 20, 2024
1 parent 1a9066b commit a36dc2b
Show file tree
Hide file tree
Showing 8 changed files with 4,449 additions and 8 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
302 changes: 302 additions & 0 deletions Heat-map-pops-up-where-first-parabola-used-to-be.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 18 additions & 6 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@ import gnuplotlib as gp

x = np.arange(101) - 50
gp.plot(x**2)
[ basic parabola plot pops up ]
#+END_SRC
[[file:basic-parabola-plot-pops-up.svg]]
#+BEGIN_SRC python


g1 = gp.gnuplotlib(title = 'Parabola with error bars',
_with = 'xyerrorbars')
g1.plot( x**2 * 10, np.abs(x)/10, np.abs(x)*5,
legend = 'Parabola',
tuplesize = 4 )
[ parabola with x,y errobars pops up in a new window ]
#+END_SRC
[[file:parabola-with-x-y-errobars-pops-up-in-a-new-window.svg]]
#+BEGIN_SRC python


x,y = np.ogrid[-10:11,-10:11]
Expand All @@ -30,7 +34,9 @@ gp.plot( x**2 + y**2,
cmds = 'set view map',
_with = 'image',
tuplesize = 3)
[ Heat map pops up where first parabola used to be ]
#+END_SRC
[[file:Heat-map-pops-up-where-first-parabola-used-to-be.svg]]
#+BEGIN_SRC python


theta = np.linspace(0, 6*np.pi, 200)
Expand All @@ -39,22 +45,28 @@ g2 = gp.gnuplotlib(_3d = True)
g2.plot( np.cos(theta),
np.vstack((np.sin(theta), -np.sin(theta))),
z )
[ Two 3D spirals together in a new window ]
#+END_SRC
[[file:Two-3D-spirals-together-in-a-new-window.svg]]
#+BEGIN_SRC python


x = np.arange(1000)
gp.plot( (x*x, dict(histogram=1,
binwidth =10000)),
(x*x, dict(histogram='cumulative', y2=1)))
[ A density and cumulative histogram of x^2 are plotted on the same plot ]
#+END_SRC
[[file:A-density-and-cumulative-histogram-of-x-2-are-plotted-on-the-same-plot.svg]]
#+BEGIN_SRC python

gp.plot( (x*x, dict(histogram=1,
binwidth =10000)),
(x*x, dict(histogram='cumulative')),
_xmin=0, _xmax=1e6,
multiplot='title "multiplot histograms" layout 2,1',
_set='lmargin at screen 0.05')
[ Same histograms, but plotted on two separate plots ]
#+END_SRC
[[file:Same-histograms-but-plotted-on-two-separate-plots.svg]]
#+BEGIN_SRC python
#+END_SRC

* DESCRIPTION
Expand Down
1,136 changes: 1,136 additions & 0 deletions Same-histograms-but-plotted-on-two-separate-plots.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
887 changes: 887 additions & 0 deletions Two-3D-spirals-together-in-a-new-window.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
334 changes: 334 additions & 0 deletions basic-parabola-plot-pops-up.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
81 changes: 79 additions & 2 deletions extract_README.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,76 @@
- README.footer.org, copied verbatim
The main module name must be passed in as the first cmdline argument.
If we want to regenerate the figures linked in the README.org documentation,
pass "DOCUMENTATION-PLOTS" as the first argument
'''

import sys
import os.path

def generate_plots():
r'''Makes plots in the docstring of the gnuplotlib.py module'''

import numpy as np
import gnuplotlib as gp

x = np.arange(101) - 50
gp.plot(x**2,
hardcopy='basic-parabola-plot-pops-up.svg')

g1 = gp.gnuplotlib(title = 'Parabola with error bars',
_with = 'xyerrorbars',
hardcopy = 'parabola-with-x-y-errobars-pops-up-in-a-new-window.svg')
g1.plot( x**2 * 10, np.abs(x)/10, np.abs(x)*5,
legend = 'Parabola',
tuplesize = 4 )

x,y = np.ogrid[-10:11,-10:11]
gp.plot( x**2 + y**2,
title = 'Heat map',
unset = 'grid',
cmds = 'set view map',
_with = 'image',
tuplesize = 3,
hardcopy = 'Heat-map-pops-up-where-first-parabola-used-to-be.svg')

theta = np.linspace(0, 6*np.pi, 200)
z = np.linspace(0, 5, 200)
g2 = gp.gnuplotlib(_3d = True,
hardcopy = 'Two-3D-spirals-together-in-a-new-window.svg')
g2.plot( np.cos(theta),
np.vstack((np.sin(theta), -np.sin(theta))),
z )

x = np.arange(1000)
gp.plot( (x*x, dict(histogram=1,
binwidth =10000)),
(x*x, dict(histogram='cumulative', y2=1)),
hardcopy = 'A-density-and-cumulative-histogram-of-x-2-are-plotted-on-the-same-plot.svg' )

gp.plot( (x*x, dict(histogram=1,
binwidth =10000)),
(x*x, dict(histogram='cumulative')),
_xmin=0, _xmax=1e6,
multiplot='title "multiplot histograms" layout 2,1',
_set='lmargin at screen 0.05',
hardcopy = 'Same-histograms-but-plotted-on-two-separate-plots.svg')



try:
modname = sys.argv[1]
arg1 = sys.argv[1]
except:
raise Exception("Need main module name as the first cmdline arg")
raise Exception("Need main module name or 'DOCUMENTATION-PLOTS' as the first cmdline arg")

if arg1 == 'DOCUMENTATION-PLOTS':
generate_plots()
sys.exit(0)


modname = arg1
exec( 'import {} as mod'.format(modname) )

import inspect
Expand Down Expand Up @@ -86,6 +147,22 @@ def write(s, verbatim):
sio = StringIO(s)
for l in sio:

# if we have a figure made with DOCUMENTATION-PLOTS, place it
m = re.match(r'^ \[ (.*) \]$', l)
if m is not None:
tag = m.group(1)
tag = re.sub(r'[^a-zA-Z0-9_]+','-', tag)
plot_filename = f"{tag}.svg"
if os.path.isfile(plot_filename):
if in_quote is not None:
if in_quote == 'example': f.write('#+END_EXAMPLE\n')
else: f.write('#+END_SRC\n')
f.write(f"[[file:{plot_filename}]]\n")
if in_quote is not None:
if in_quote == 'example': f.write('#+BEGIN_EXAMPLE\n')
else: f.write('#+BEGIN_SRC python\n')
continue

# handle links
l = re.sub( r"([^ ]+) *\((https?://[^ ]+)\)",
r"[[\2][\1]]",
Expand Down
Loading

0 comments on commit a36dc2b

Please sign in to comment.