Graphics, in particular plotting data

Alan BRASLAU Service de physique de l’état condensé, CEA/Saclay, France

Bassange – 20/09/2011 Why pay taxes?

3D graphics are sexy!

Graphics, in particular plotting data 2 1. External graphics Produced using your favorite external program… Usage: \externalfigure [filename][option=val] file formats: , mps, jpg jpeg, jp2, png, jbig jbig2 jb2, | | | svg svgz, eps, ai, gif, tif tiff, mov, flv, mp4, (but not avi), | | , cld, … automatically converted using external programs such as ghostscript, imageMagick, graphicsMagick, , etc. (if available). advantage: use of your tried and true external programs that you know and love… disadvantage: poor control of style, fonts, font size, colors, line weights, etc.; possible/probable inconsistencies between figures.

Graphics, in particular plotting data 3 2. TEX-friendly graphics A. ‘external’ programs (or macros) METAPOST

PSTricks, PiCTeX, TikZ, matplotlib (python), asymptote, R, gnuplot, …

More or less well integrated in ConTEXt; more or less TEX-friendly. METAPOST B. MetaFun: graphics well integrated in ConTEXt Usage: \startMPcode … \stopMPcode

\startuseMPgraphic{label} … \stopuseMPgraphic

Graphics, in particular plotting data 4 http://www.pragma-ade.com/general/manuals/metafun-p.pdf Also available on paper, bound, for 49.00 € from www.h2o-books.com 375 pages.

mkiv

Hans Hagen metafun

Graphics, in particular plotting data 5 texlive/2011/texmf-dist/doc//base/mpman.pdf

METAPOST

a user’s manual

John D. Hobby and the MetaPost development team

documented version: 1.504 April 5, 2011

Graphics, in particular plotting data 6 TikZ (vs. MetaFun) TikZ (built upon PGF) is a METAPOST-inspired graphics language. More and more popular with LATEX users, it has always functioned well under

ConTEXt. Its great strength is a

& & PGF Z k very well written tutorial and TikZ PGF manual; it is rich in features

Manual for Version . .

and has many extras (such as Manual for Version pgfplots, pgfgantt, tikz-3dplot,

\begin{tikzpicture} \nodeshadowed [at={(-5,8 )},yslant=0.05] and more). coordinate front at Huge Ti textcolor orange emph k Z \ ( ) (0,0); {\ \ { }{\ { }} }; \coordinate (horizon) at (0,.31\paperheight); \nodeshadowed [at={( 0,8.3)}] \coordinate (bottom) at (0,-.6\paperheight); {\huge \textcolor{green!50!black!50}{\&}}; \coordinate (sky) at (0,.57\paperheight); \nodeshadowed [at={( 5,8 )},yslant=-0.05] \coordinate (left) at (-.51\paperwidth,0); {\Huge \textsc{PGF}}; \coordinate (right) at (.51\paperwidth,0); \nodeshadowed [at={( 0,5 )}] {Manual for Version \pgftypesetversion}; \shade [bottom color=white, top color=blue!30!black!50] \foreach \where in {-9cm,9cm}{ ([yshift=-5mm]horizon - | left) \nodeshadowed [at={(\where,5cm)}] { \tikz rectangle (sky - | right); \draw [green!20!black, rotate=90, l-system={rule set={F -> FF-[-F+F]+[+F-F]}, However, its syntax is more \shade [bottom color=black!70!green!25, axiom=F, order=4,step=2pt, top color=black!70!green!10] randomize step percent=50, angle=30, (front - | left) -- (horizon - | left) randomize angle percent=5}] l-system; }} decorate [decoration=random steps]{ -- (horizon - | right)} \foreach \i in {0.5,0.6,...,2} -- (front - | right) -- cycle; \fill in the spirit of L T X then [white,opacity=\i/2, A \shade [top color=black!70!green!25, decoration=Koch snowflake, E bottom color=black!25] shift=(horizon),shift={(rand∗11,rnd∗7)}, ([yshift=-5mm-1pt]front - | left) scale=\i,double copy shadow={ rectangle ([yshift=1pt]front - | right); opacity=0.2,shadow xshift=0pt, shadow yshift=3∗\i pt,fill=white,draw=none}] \fill [black!25] decorate { ConT Xt (although this (bottom - | left) decorate { E rectangle ([yshift=-5mm]front - | right); decorate { (0,0)- ++(60:1) -- ++(-60:1) -- cycle \def\nodeshadowed[#1]#2;{ } } }; \node[scale=2,above,#1]{ \global\setbox\mybox=\hbox{#2} \node (left text) ... judgment is purely subjective \copy\mybox}; \node (right text) ... \node[scale=2,above,#1,yscale=-1, scope fading=south,opacity=0.4]{\box\mybox}; \fill [decorate,decoration={footprints,foot of=gnome}, } opacity=.5,brown](rand∗8,-rnd∗10) to [out=rand∗180,in=rand∗180] (rand∗8,-rnd∗10); and personal). \end{tikzpicture}

Graphics, in particular plotting data 7 TikZ has a loyal following TikZ remains active (on the ConTEXt mailing list as well). Bugs in TikZ (introduced as ConTEXt evolves) get fixed quickly. However,

The core use of TikZ is LATEX, and so it must remain compatible with the many flavors of LATEX. Calculations can be very slow. For example: “PGFPLOTS’ three dimensional routines are slow. There are reasons for this and some of them may vanish in future versions. … Besides the speed limitations, three dimensional plots reach memory limits easily. Therefore, the plot complexity of three dimensional plots is limited to relatively coarse resolutions.” These limitations remain true for all TikZ graphics. (Could TikZ evolve towards lualatex, or must it remain compatible with all LATEX motors?)

Graphics, in particular plotting data 8 MetaFun is in the core of ConTEXt So, why do we ever consider using anything else? METAPOST has a steep learning-curve; METAPOST, although pretty well-documented, is perhaps lacking a good tutorial; MetaFun and METAPOST have been extended through many higher-level macro packages (such as graph and the chart module, just to name two that I sometimes use). We need more, for example, convenient ways to manipulate common graphical representations (such as diagrams, trees, etc.).

Graphics, in particular plotting data 9 Drawing diagrams Suggestion/question: could the \framed macro be extended to be used in a way that is similar to what is called a node in TikZ: 1. absolute or relative placement on a canvas; 2. enclosure (and typeset) inside an arbitrary, closed path; 3. connected by various lines or arrows (using anchor points?)

How do we get \startCANVAS from here \frame [start][frame=circle,width=3cm, backgroundcolor=green,foregroundcolor=white] {How do we get from here} \frame [stop][frame=hexagon,width=3cm, location=start.lrt, backgroundcolor=red,foregroundcolor=white] {to here?} \drawarrow [from=start,to=stop] to here? \stopCANVAS

Graphics, in particular plotting data 10 What about plotting data? METAPOST graph macros crystalline helium (John D. Hobby) (100) (101) 1.001 (102) \usemodule [graph] (110) 0 (112)

Q

2D (‘xy’) plotting of data / 1

Q

0.999 METAPOST (< 2.0) uses fixed point arithmetic. Much (100) (101) of the complication of the 1.001 (102) (110) graph macros arises from (112)

0 handling floating-point d 1 numbers. /

d limitations in plotting → very small and vary large 0.999 quantities (as well as very 0 0.5 1 1.5 small increments). T (K)

Graphics, in particular plotting data 11 Mlog arithmetic x 216 The graph macros represent numbers in “Mlog” form, that is as µ · 24 where µ = e2− : − “A number x in Mlog form represents exp(x) if x is an even multiple of ε and exp(x) if x is an odd multiple of ε, 16 − where ε = 2− [ 1.53e-5] is the basic unit for METAPOST’s ≈ fixpoint numbers. Such numbers can represent values large as 3.8877e+55 or as small as 1.604e-28 (anything less than that is treated as zero).”

METAPOST 2.0 promises arbitrary precision, floating-point arithmetic. The graph macros can be rewritten (and greatly simplified). What about using lua? Think about the possibilities (easily achieved…).

Graphics, in particular plotting data 12 Limitations in handling real data Currently, all but the most trivial manipulations of numerical data must be processed outside of METAPOST. One of my favorite tools is awk (or gawk which adds a few useful extensions). Awk is a very flexible, yet simple and robust interpreted language, using C-like syntax. In many ways, it shares much in common with lua. Another route, quite popular today, is python. Python is at the basis of the matplotlib package, and this solution is found to be quite satisfactory by many. Matplotlib uses a MATLAB-like syntax.

Graphics, in particular plotting data 13 What is needed: 2D plots barcharts/histograms piecharts polar diagrams ternary diagrams contour plots 3D plots 3D surfaces (In general, a 3D extension to METAPOST would be desirable.)

Graphics, in particular plotting data 14 3D METAPOST A 3D METAPOST macro package was developed by Denis Roegel. It is distributed with TEXlive under mp3d (and on CTAN, of course).

\startMPcode input 3dgeom ; drawing_scale := 10cm ; vardef def_C(expr inst) = new_obj_points(inst,9) ; set_C_points(inst) ; enddef ; vardef set_C_points(expr inst) = set_point(1)(0,0,0) ; set_point(2)(0,0,1) ; set_point(3)(0,1,0) ; set_point(4)(0,1,1) ; set_point(5)(1,0,0) ; set_point(6)(1,0,1) ; set_point(7)(1,1,0) ; set_point(8)(1,1,1) ; set_point(9)(.5,.5.,.5) ; enddef ; vardef draw_C(expr inst) = draw_lines(1,2,4,3,1) ; draw_lines(5,6,8,7,5) ; draw_line(1,5) ; draw_line(2,6) ; draw_line(3,7) ; draw_line(4,8) ; enddef ; assign_obj("cube","C"); for i:=0 upto 10: if (i > 0) : picture pic ; pic := currentpicture ; currentpicture := nullpicture ; draw pic xshifted 1.65cm ; fi set_point_(Obs)(20*cosd(7.2*i),20*sind(7.2*i),6) ; Obs_phi := 90 ; Obs_dist := 2 ; point_of_view_obj("cube",8,Obs_phi) ; draw_obj("cube") ; endfor; \stopMPcode

Graphics, in particular plotting data 15 Examples Cubic crystal lattices

c bcc fcc polyhedra – 3dpoly

Graphics, in particular plotting data 16 3d, 3dgeom, 3dpoly Handles vectors and polyhedra; Spheres, cylinders, cones, etc., planned, but not yet implemented; Could objects be defined in terms of a few basic 3D primitives rather then simply as a collection of indices? Handles different projection systems or perspectives (linear, parallel, oblique); A unique, global triplet of arrays stores the constituent points of all objects. The allocation (and freeing) of storage is a bit awkward. This could (easily?) be improved with some internal housekeeping. Documentation? version 1.34, August 2003. Will development continue, or is the project frozen?

Graphics, in particular plotting data 17 Another approach — Urs Oswald http://www.ursoswald.ch/metapost/tutorial.html z z

y y

x x “In this example, the MetaPost type color is «abused» to represent 3D vectors. Indeed, a color object is, apart from its possible use as an argument to the withcolor command, just a triplet of numbers. It can be multiplied with and divided by a numeric, and two color objects can be added. However, it must by pointed out that for color, unlike for pair, there are no operators like unitvector or abs, and there is no transform. If such operators are needed, they must be written by the user. … By the way, the type color is the only MetaPost type which is not a type in . ”

Graphics, in particular plotting data 18 m3D — Anthony Phan

; r a e r u , e o y r; n o o r ea ; r a g e t n ou e r ; r a i y ne, , u o y r a s g n i nto go e n o t e g y a s i re o g n i n i d e ing a r; e r a g n i p t a g m s ys ea ; r a s s u , e s n i t e da g e g y a e ; y d n d c e l t m g tin n i d g m t p u o o t n n le in sa , p t n i e l c b s a a e c e u , a s c e n l a k d m e an nd on y r; p u e d n a m e u l i m d a t od ; to your ea y n o n a d d l o o f n s e o o k s mb e usp ing in d t s k e m o . g o t o s C C C C ome eil le a t l c ; p s o b e m a t e l l i e s a g o a p t o os s n u n gone, o y o t n i g u n i l n s o b a : h u a h o om t o fo e g days are ; r a e r u c u o o t a m r h g t h m h oasep dt ancld in n e f t a d p e s e h o h e n i a h t T T T T hth e u a o g. y a d g d l n i c n a u t v h r g c l l h e hl o h lteh : g a satin gear; , e n o g e r a s . o g : h t e l h o l l l i n a n i u o l cs gata tgs ialk b and

g a a g t t g a h c t s u l l s u l p o A A A A l llu thne r inmer

k l i a s e s l i a s d n b a n r m n i o l a s u r l e ol ad n v e stone, ; r a e g n i t r e i a d p t l r e o p l e tr optphucl hua uilpo n a v l i p e n u u h l a u c u p h o r n i h f a r C C C C t ra oua s sn s dy up

, e n o t s a n o s s n o a a o r r t s r h d mro f et a c thati nfoul bo

s a c e t f u r n y a W W W W o a a rsaupth pi nisng in g i h t g n n i i s h p t p a s o r e a c il g

d o b l u o f t a n n i i y c a r o r n u t n rary onh ulk a rag: in

p u y g g k l n o h s r r a a n n n a I I I I ranc s rapso fo u a d t en cup; u o f p m n c s ld

i : g a r a l u o p a c c p hci a am p pe sunn in aa go o

n d a p i r m a a c e I I I I a lci i asrryin th o d y . y n h t y r s e p k nen g n rs bag

g t a n i a n u d s e o n h c h c s huca fo o n in a sislve o ; p u c n e d l o o y n g m o f e a h T T T T a in p hg g er m ou th a a a u s s s a n i n t o o u r r h h r f ha ry l a s ti r r . g a y b r e v l i a h l y e r o g o n de t a t t n e

o a a s a a t m e y u n i m h r f o g g e

i t t h m t d a n o e a o n e o a

u r n t r e o t l h o s g u e r e o o f a o h a a y t n u : l , a n ;

r g n g s n o a s o n b r e g e u : u o e r t s a s in o ; , e o e l n i a a in d a a , b n n s f t t y n o n i a u g o o i a y s a r r i n ilv g u ; e ; d a s n n : u n r o e o p , y l i e r ld g v a i l e g u b e u o e n b a n

l r s , e g r g p d a o . c e b il a o u

n a v g d e p g e r ; . y n a c r o ; u l e p b d u r ; a e p , ; g n . c u p ;

m3D is a METAPOST macro package to produce 3D graphics developed by Anthony Phan. It is “work in progress” (version January 2006 available on CTAN; a revised version (of the manual) dated October 2010 is available at http://www-math.univ-poitiers.fr/~phan). Graphics, in particular plotting data 19 m3D is programmer-oriented From the m3Dmanual, Anthony Phan writes: “…Denis Roegel’s package 3D. Its syntax didn’t fit what I could have in mind about 3D-programming in METAPOST…” “Also one of my believes is that any METAPOST programmer prefers to build his/her own macros’ system than relying on someone else’s programs—especially when these ones are claimed to be unstable. Such a programmer would simply have an overview of the syntax, of some special hacks, may publish his/her own programs on Internet and in this way give some feed back to every other people interested in such programming.” m3D also manipulates 3D vectors using the METAPOST ‘color’ variable type, a triplet.

Denis Roegel: “We might have cheated and stored points as colors, but instead, we found it interesting to illustrate a construction equivalent to Pascal’s records or C’s structures.”

Graphics, in particular plotting data 20 So, where do we go from here?

Graphics, in particular plotting data 21 And, does this have to do with plotting data? Plotting data in 2D and 3D as well as drawing in 3D involves transformations from a data space to the 2D drawing space of METAPOST. METAPOST 2.0 that promises arbitrary precision arithmetic may be largely sufficient for most plotting tasks. The graph macros need to be re-written (simplified) and extended. New graph types can be added. It would be nice to introduce the type triplet (distinct from color) and paths of triplets. Drawing such paths would transform to a 2D projection. Or, maybe, one can continue to work with arrays (either a triplet of arrays or arrays of triplets)? METAPOST arrays are simulated: variable → tag suffix h i h ih i suffix → empty suffix subscript suffix tag h i h i|h ih i|h ih i subscript → number [ numeric expression ] h i h i| h i Graphics, in particular plotting data 22 More on arrays… awk has a slightly different approach to arrays. It has one dimensional arrays; arrays with multiple dimensions may be simulated. “Array subscripts may be any string, not necessarily numeric; this allows for a form of associative memory. Multiple subscripts such as [i,j,k] are permitted; the constituents are concatenated, separated by the value of SUBSEP.” (One can do the same in lua.) New relational expressions facilitate the handling of arrays: expression in array-name (expr,expr,...) in array-name if (val in array) print array[val] and for (i in array) print array[i]

Graphics, in particular plotting data 23 Conclusions METAPOST is the best solution for graphics (and for plotting data) in ConTEXt. However, METAPOST can be daunting: 1. a powerful programming language, solutions can be ‘too clever’ for the average user; 2. errors are easily made, and error messages require some experience to digest; 3. more, higher-level standard packages are needed (which ones?, who?, …); 4. a good, standard, relatively rich tutorial is needed (who is going to write this?); METAPOST Division of tasks between TEX, and lua? To respect the coherence of ConTEXt, we must avoid the situation encountered with LATEX, were an (infinity) of different ‘third-party’ packages are available.

Graphics, in particular plotting data 24