Jupyter Tips 4

Continuation of Jupyter's Tips 3

what is this

From the 15 figures below, 3 pictures will be displayed at random [^ 1].

[^ 1]: See "09 Finke's Ambiguous Parts" in the book "The Complete Ideas".

As a preparation, first do the following:

jupyter_notebook


from IPython.display import display, SVG
import numpy as np, IPython.core.getipython
ss = """\
1/e50 50 50 50 url(#g) black 0.5
1/e50 50 50 50 url(#g)/r0 0 100 50 white/e 50 50 50 10 #E0E0E0 black 0.5
0/pM1,34_L35,10_L99,10_L66,34_L99,10_L99,75_L66,99 #E0E0E0 black 0.5/r1 34 65 65 #C0C0C0 black 0.5
0/e50 82 50 17 #E0E0E0 black 0.5/pM0,80_L50,0_L100,80 #E0E0E0 black 0.5
0/e90 50 10 30 #E0E0E0 black 0.5/pM90,20_L10,20_L10,80_L90,80 #E0E0E0 black 0.5/e10 50 10 30 #C0C0C0 black 0.5
0/pM10,70_C10,60_30,30_50,30_C70,40_50,60_70,70_C90,70_90,40_90,30 none black
0/pM10,70_C10,60_30,30_50,30_C70,40_50,60_70,70_C90,70_90,40_90,30 none black 10\
/pM10,69.2_C10,60_30,30_50,30_C70,40_50,60_70,70_C90,70_90,40_90,30.8 none white 8.4
0/pM0,70_L30,30_L100,30_L70,70_Z #E0E0E0 black 0.8
0/pM20,20_L20,80_L80,80 none black 10/pM20,21_L20,80_L79,80 none white 8.4
0/pM1,50_L80,25_L99,25_L25,50_M99,25_L99,50_L25,75_L25,50 #E0E0E0 black 0.5/r1 50 25 25 #C0C0C0 black 0.5
0/pM25,40_C50,-15_99,40_67,67_C60,70_50,80_50,98 none black 0.8/c 25 40 1.2/c 50 98 1.2
0/e20 39.5 16 24 gray black 0.5/e24 40 16 24 #C0C0C0 black 0.5/e24 40 2 3 #555/l24 40 84 60 gray 2\
/e80 59.5 16 24 gray black 0.5/e84 60 16 24 #C0C0C0 black 0.5/e84 60 2 3 #555
0/l10 10 90 90 black 10/l10.5 10.5 89.5 89.5 white 8.4/l10 90 90 10 black 10/l10.5 89.5 89.5 10.5 white 8.4
0/c50 50 40 none black 10/c50 50 40 none white 8.4
0/pM10,70_A40,40_0_0_1_90,70 none black 10/pM10,69.2_A40,39.2_0_0_1_90,69.2 none white 8.4""".splitlines()
tt = {
      'p':'path d fill stroke stroke-width'.split(),
      'l':'line x1 y1 x2 y2 stroke stroke-width'.split(),
      'c':'circle cx cy r fill stroke stroke-width'.split(),
      'e':'ellipse cx cy rx ry fill stroke stroke-width'.split(),
      'r':'rect x y width height fill stroke stroke-width'.split(),
}
def make_svg(ids, ss=ss, tt=tt):
    rs, n = [], len(ids)
    for i, j in enumerate(ids):
        rr = ['<g transform="translate(%d,0)">'%(i*120)]
        vv = ss[j].split('/')
        if vv[0] == '1':
            rr.append("""\
                <defs><radialGradient id="g" gradientUnits="userSpaceOnUse"
                  cx="50" cy="50" r="50" fx="20" fy="50">
                <stop offset="0%" stop-color="white" />
                <stop offset="100%" stop-color="gray" />
                </radialGradient></defs>""")
        for s in vv[1:]:
            uu = tt[s[0]]
            rr.append('<%s %s />'%(uu[0],' '.join('%s="%s"'%(u,
                a.replace('_',' ')) for u, a in zip(uu[1:], s[1:].split()))))
        rs.append('\n'.join(rr+['</g>']))
    return SVG('<svg width="%d" height="100">%s</svg>'%(n*120,'\n'.join(rs)))
def F_impl(s):
    return make_svg(np.random.choice(range(15), int(s) if s else 3, False))
ip = IPython.core.getipython.get_ipython()
if ip:
    ip.register_magic_function(F_impl, magic_name='F')

Try to run

jupyter_notebook


F

image

jupyter_notebook


F

image

jupyter_notebook


for i in range(1,4):
    svg = %F {i}
    display(svg)

image

You can eval it first by enclosing it in "{}". You can also save the result to a variable and use it.

that's all

Recommended Posts

Jupyter Tips 4
Jupyter Tips 5
Jupyter Tips 3
Jupyter Tips 2
python tips
Jupyter begins
Jupyter nbextensions
numpy tips
python tips
Scapy Tips
Jupyter Notebook: 4 banal tips and tricks
Jupyter installation
Jupyter tricks
Python Tips
Python tips
Python Conda Tips
Jupyter installation error
Jupyter Notebook memo
Introducing Jupyter Notebook
Jupyter study notes_006
Jupyter Lab begins
Jupyter process management
Django's personal tips
Python debugging tips
Python click tips
Powerful Jupyter Notebook
[pandas] GroupBy Tips
Unexpectedly (?) Python tips
Golang on jupyter
Tensorflow personal tips
Bash in Jupyter
Django Template Tips
Jupyter on AWS
fabric Writing Tips
Jupyter Study Note_002
Jupyter notebook password
Build Jupyter Hub
Jupyter Notebook memo
Jupyter study notes_008
LTTng Tracing Tips
Jupyter study notes_004
jupyter qtconsole config
Jupyter study notes_001
Jupyter Study Note_003
Jupyter Study Note_007
Choregraphe Tips Collection
Jupyter Study Note_005
[Tips] Save / copy the graph displayed in Jupyter Lab