Graphics,inparticularplottingdata · PSTricks,PiCTeX,TikZ,matplotlib(python) ... TikZ(vs.MetaFun)...

24
Graphics, in particular plotting data Alan BRASLAU Service de physique de l’état condensé, CEA/Saclay, France Bassange – 20/09/2011

Transcript of Graphics,inparticularplottingdata · PSTricks,PiCTeX,TikZ,matplotlib(python) ... TikZ(vs.MetaFun)...

Page 1: Graphics,inparticularplottingdata · PSTricks,PiCTeX,TikZ,matplotlib(python) ... TikZ(vs.MetaFun) Graphics,inparticularplottingdata 7 TikZ(builtuponPGF)isa METAPOST-inspiredgraphics

Graphics, in particular plotting data

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

Bassange – 20/09/2011

Page 2: Graphics,inparticularplottingdata · PSTricks,PiCTeX,TikZ,matplotlib(python) ... TikZ(vs.MetaFun) Graphics,inparticularplottingdata 7 TikZ(builtuponPGF)isa METAPOST-inspiredgraphics

Why pay taxes?

Graphics, in particular plotting data 2

3D graphics are sexy!

Page 3: Graphics,inparticularplottingdata · PSTricks,PiCTeX,TikZ,matplotlib(python) ... TikZ(vs.MetaFun) Graphics,inparticularplottingdata 7 TikZ(builtuponPGF)isa METAPOST-inspiredgraphics

1. External graphics

Graphics, in particular plotting data 3

Produced using your favorite external program…

Usage:

\externalfigure [filename] [option=val]

file formats: pdf, mps, jpg|jpeg, jp2, png, jbig|jbig2|jb2,svg|svgz, eps, ai, gif, tif|tiff, mov, flv, mp4, (but not avi),tex, cld, … automatically converted using external programssuch as ghostscript, imageMagick, graphicsMagick, inkscape, etc.(if available).

advantage: use of your tried and true external programs that youknow and love…

disadvantage: poor control of style, fonts, font size, colors, lineweights, etc.; possible/probable inconsistencies between figures.

Page 4: Graphics,inparticularplottingdata · PSTricks,PiCTeX,TikZ,matplotlib(python) ... TikZ(vs.MetaFun) Graphics,inparticularplottingdata 7 TikZ(builtuponPGF)isa METAPOST-inspiredgraphics

2. TEX-friendly graphics

Graphics, in particular plotting data 4

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.

B. MetaFun: METAPOST graphics well integrated in ConTEXtUsage:

\startMPcode…\stopMPcode

\startuseMPgraphic{label}…\stopuseMPgraphic

Page 5: Graphics,inparticularplottingdata · PSTricks,PiCTeX,TikZ,matplotlib(python) ... TikZ(vs.MetaFun) Graphics,inparticularplottingdata 7 TikZ(builtuponPGF)isa METAPOST-inspiredgraphics

http://www.pragma-ade.com/general/manuals/metafun-p.pdf

Graphics, in particular plotting data 5

met

afun

Hans Hagen

cont

extm

kiv

Also available on paper,bound, for 49.00 € fromwww.h2o-books.com

375 pages.

Page 6: Graphics,inparticularplottingdata · PSTricks,PiCTeX,TikZ,matplotlib(python) ... TikZ(vs.MetaFun) Graphics,inparticularplottingdata 7 TikZ(builtuponPGF)isa METAPOST-inspiredgraphics

texlive/2011/texmf-dist/doc/metapost/base/mpman.pdf

Graphics, in particular plotting data 6

METAPOST

a user’s manual

John D. Hobbyand the MetaPost development team

documented version: 1.504April 5, 2011

Page 7: Graphics,inparticularplottingdata · PSTricks,PiCTeX,TikZ,matplotlib(python) ... TikZ(vs.MetaFun) Graphics,inparticularplottingdata 7 TikZ(builtuponPGF)isa METAPOST-inspiredgraphics

TikZ (vs. MetaFun)

Graphics, in particular plotting data 7

TikZ (built upon PGF) is aMETAPOST-inspired graphicslanguage. More and morepopular with LATEX users, it hasalways functioned well underConTEXt. Its great strength is avery well written tutorial andmanual; it is rich in featuresand has many extras (such aspgfplots, pgfgantt, tikz-3dplot,and more).

However, its syntax is morein the spirit of LATEX thenConTEXt (although thisjudgment is purely subjectiveand personal).

TikZTikZ&& PGFPGF

Manual for Version .Manual for Version .

\begin{tikzpicture}\coordinate (front) at (0,0);\coordinate (horizon) at (0,.31\paperheight);\coordinate (bottom) at (0,-.6\paperheight);\coordinate (sky) at (0,.57\paperheight);\coordinate (left) at (-.51\paperwidth,0);\coordinate (right) at (.51\paperwidth,0);

\shade [bottom color=white,top color=blue!30!black!50]

([yshift=-5mm]horizon - | left)rectangle (sky - | right);

\shade [bottom color=black!70!green!25,top color=black!70!green!10]

(front - | left) -- (horizon - | left)decorate [decoration=random steps] {

-- (horizon - | right) }-- (front - | right) -- cycle;

\shade [top color=black!70!green!25,bottom color=black!25]

([yshift=-5mm-1pt]front - | left)rectangle ([yshift=1pt]front - | right);

\fill [black!25](bottom - | left)

rectangle ([yshift=-5mm]front - | right);

\def\nodeshadowed[#1]#2;{\node[scale=2,above,#1]{

\global\setbox\mybox=\hbox{#2}\copy\mybox};

\node[scale=2,above,#1,yscale=-1,scope fading=south,opacity=0.4]{\box\mybox};

}

\nodeshadowed [at={(-5,8 )},yslant=0.05]{\Huge Ti\textcolor{orange}{\emph{k}}Z};

\nodeshadowed [at={( 0,8.3)}]{\huge \textcolor{green!50!black!50}{\&}};

\nodeshadowed [at={( 5,8 )},yslant=-0.05]{\Huge \textsc{PGF}};

\nodeshadowed [at={( 0,5 )}]{Manual for Version \pgftypesetversion};

\foreach \where in {-9cm,9cm} {\nodeshadowed [at={(\where,5cm)}] { \tikz

\draw [green!20!black, rotate=90,l-system={rule set={F -> FF-[-F+F]+[+F-F]},

axiom=F, order=4,step=2pt,randomize step percent=50, angle=30,randomize angle percent=5}] l-system; }}

\foreach \i in {0.5,0.6,...,2}\fill

[white,opacity=\i/2,decoration=Koch snowflake,shift=(horizon),shift={(rand∗11,rnd∗7)},scale=\i,double copy shadow={

opacity=0.2,shadow xshift=0pt,shadow yshift=3∗\i pt,fill=white,draw=none}]

decorate {decorate {

decorate {(0,0)- ++(60:1) -- ++(-60:1) -- cycle

} } };

\node (left text) ...\node (right text) ...

\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);\end{tikzpicture}

Page 8: Graphics,inparticularplottingdata · PSTricks,PiCTeX,TikZ,matplotlib(python) ... TikZ(vs.MetaFun) Graphics,inparticularplottingdata 7 TikZ(builtuponPGF)isa METAPOST-inspiredgraphics

TikZ has a loyal following

Graphics, in particular plotting data 8

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 compatiblewith the many flavors of LATEX.

Calculations can be very slow. For example:“PGFPLOTS’ three dimensional routines are slow. There arereasons for this and some of them may vanish in future versions.… Besides the speed limitations, three dimensional plots reachmemory limits easily. Therefore, the plot complexity of threedimensional plots is limited to relatively coarse resolutions.”

These limitations remain true for all TikZ graphics.

(Could TikZ evolve towards lualatex, or must it remaincompatible with all LATEX motors?)

Page 9: Graphics,inparticularplottingdata · PSTricks,PiCTeX,TikZ,matplotlib(python) ... TikZ(vs.MetaFun) Graphics,inparticularplottingdata 7 TikZ(builtuponPGF)isa METAPOST-inspiredgraphics

MetaFun is in the core of ConTEXt

Graphics, in particular plotting data 9

So, why do we ever consider using anything else?

METAPOST has a steep learning-curve;

METAPOST, although pretty well-documented, is perhaps lackinga good tutorial;

MetaFun and METAPOST have been extended through manyhigher-level macro packages (such as graph and the chartmodule, just to name two that I sometimes use). We need more,for example, convenient ways to manipulate common graphicalrepresentations (such as diagrams, trees, etc.).

Page 10: Graphics,inparticularplottingdata · PSTricks,PiCTeX,TikZ,matplotlib(python) ... TikZ(vs.MetaFun) Graphics,inparticularplottingdata 7 TikZ(builtuponPGF)isa METAPOST-inspiredgraphics

Drawing diagrams

Graphics, in particular plotting data 10

Suggestion/question: could the \framed macro be extended to be usedin 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 dowe get

from here

to here?

\startCANVAS\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]\stopCANVAS

Page 11: Graphics,inparticularplottingdata · PSTricks,PiCTeX,TikZ,matplotlib(python) ... TikZ(vs.MetaFun) Graphics,inparticularplottingdata 7 TikZ(builtuponPGF)isa METAPOST-inspiredgraphics

What about plotting data?

Graphics, in particular plotting data 11

METAPOST graph macros(John D. Hobby)

\usemodule [graph]

2D (‘xy’) plotting of data

METAPOST (< 2.0) usesfixed point arithmetic. Muchof the complication of thegraph macros arises fromhandling floating-pointnumbers.→ limitations in plottingvery small and vary largequantities (as well as verysmall increments).

0.999

1

1.001(100)(101)(102)(110)(112)

crystalline helium

Q/

Q0

0 0.5 1 1.5

0.999

1

1.001(100)(101)(102)(110)(112)

T (K)

d/

d 0

Page 12: Graphics,inparticularplottingdata · PSTricks,PiCTeX,TikZ,matplotlib(python) ... TikZ(vs.MetaFun) Graphics,inparticularplottingdata 7 TikZ(builtuponPGF)isa METAPOST-inspiredgraphics

Mlog arithmetic

Graphics, in particular plotting data 12

The graph macros represent numbers in “Mlog” form, that is as µx·216

where µ = −e2−24:“A number x in Mlog form represents exp(x) if x is an evenmultiple of ε and − exp(x) if x is an odd multiple of ε,where ε = 2−16 [≈1.53e-5] is the basic unit for METAPOST’sfixpoint numbers. Such numbers can represent values large as3.8877e+55 or as small as 1.604e-28 (anything less than that istreated as zero).”

METAPOST 2.0 promises arbitrary precision, floating-pointarithmetic.

The graph macros can be rewritten (and greatly simplified).

What about using lua?

Think about the possibilities (easily achieved…).

Page 13: Graphics,inparticularplottingdata · PSTricks,PiCTeX,TikZ,matplotlib(python) ... TikZ(vs.MetaFun) Graphics,inparticularplottingdata 7 TikZ(builtuponPGF)isa METAPOST-inspiredgraphics

Limitations in handling real data

Graphics, in particular plotting data 13

Currently, all but the most trivial manipulations of numerical datamust 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 withlua.

Another route, quite popular today, is python.Python is at the basis of the matplotlib package, and this solutionis found to be quite satisfactory by many. Matplotlib uses aMATLAB-like syntax.

Page 14: Graphics,inparticularplottingdata · PSTricks,PiCTeX,TikZ,matplotlib(python) ... TikZ(vs.MetaFun) Graphics,inparticularplottingdata 7 TikZ(builtuponPGF)isa METAPOST-inspiredgraphics

What is needed:

Graphics, in particular plotting data 14

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.)

Page 15: Graphics,inparticularplottingdata · PSTricks,PiCTeX,TikZ,matplotlib(python) ... TikZ(vs.MetaFun) Graphics,inparticularplottingdata 7 TikZ(builtuponPGF)isa METAPOST-inspiredgraphics

3D METAPOST

Graphics, in particular plotting data 15

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

\startMPcodeinput 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 ;

fiset_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

Page 16: Graphics,inparticularplottingdata · PSTricks,PiCTeX,TikZ,matplotlib(python) ... TikZ(vs.MetaFun) Graphics,inparticularplottingdata 7 TikZ(builtuponPGF)isa METAPOST-inspiredgraphics

Examples

Graphics, in particular plotting data 16

Cubic crystal lattices

c bcc fccpolyhedra – 3dpoly

Page 17: Graphics,inparticularplottingdata · PSTricks,PiCTeX,TikZ,matplotlib(python) ... TikZ(vs.MetaFun) Graphics,inparticularplottingdata 7 TikZ(builtuponPGF)isa METAPOST-inspiredgraphics

3d, 3dgeom, 3dpoly

Graphics, in particular plotting data 17

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 thensimply as a collection of indices?

Handles different projection systems or perspectives(linear, parallel, oblique);

A unique, global triplet of arrays stores the constituent pointsof all objects. The allocation (and freeing) of storage is a bitawkward. This could (easily?) be improved with some internalhousekeeping.

Documentation?

version 1.34, August 2003.Will development continue, or is the project frozen?

Page 18: Graphics,inparticularplottingdata · PSTricks,PiCTeX,TikZ,matplotlib(python) ... TikZ(vs.MetaFun) Graphics,inparticularplottingdata 7 TikZ(builtuponPGF)isa METAPOST-inspiredgraphics

Another approach — Urs Oswald

Graphics, in particular plotting data 18

http://www.ursoswald.ch/metapost/tutorial.html

x

y

z

x

y

z

“In this example, the MetaPost type color is «abused» to represent 3D vectors. Indeed, a color object is, apartfrom its possible use as an argument to the withcolor command, just a triplet of numbers. It can be multipliedwith 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 METAFONT. ”

Page 19: Graphics,inparticularplottingdata · PSTricks,PiCTeX,TikZ,matplotlib(python) ... TikZ(vs.MetaFun) Graphics,inparticularplottingdata 7 TikZ(builtuponPGF)isa METAPOST-inspiredgraphics

m3D — Anthony Phan

Graphics, in particular plotting data 19

Come let me sing into your ear;

Those dancing days are gone,

All that silk and satin gear;

Crouch upon a stone,

Wrapping that foul body up

In as foul a rag:

I carry the sun in a golden cup;

The moon in a silver bag. Co

me le

t me sing

into your ear;

Those d

ancing da

ys are g

one,

All that silk and satin gear;

Crouch upon a sto

ne,

Wrapping that foul body up

In as foul a rag:

I carry the sun in a golden cup;

The moon in a silver bag.

Come

let m

e sing

into y

our e

ar;

Thos

e dan

cing d

ays a

re go

ne,

All th

at si

lk an

d sati

n gea

r;

Crou

ch u

pon

a sto

ne,

Wra

ppin

g th

at fo

ul b

ody u

p

In a

s fou

l a ra

g:

I car

ry th

e sun

in a

gold

en cu

p;

The

moo

n in

a si

lver

bag.

Com e let m e sing into your ear;

Those dancing days are gone,

All that silk and satin gear;

Crouch upon a stone,

W rapping that foul body

up

In as foul a rag:

I carry the sun in a golden

cup;

The m oon in a silver bag.

Come let me sing into your ear; Those dancing days are gone, All that silk and satin gear; Crouch upon a stone, Wrapping that foul body up In as foul a rag:I carry the sun in a golden cup; The moon in a silver bag.

Come let m

e sing into your ear;

Those dancing days are gone,

All that silk and satin gear;

Crouch

upon a sto

ne,

Wrappin

g that fo

ul body up

In as foul a ra

g:

I carry the sun in a g

olden cup;

The m

oon in a s

ilver bag.

Com

e let me sin

g into y

our e

ar;

Those dan

cing d

ays a

re gone,

All th

at silk and s

atin g

ear;

Cro

uch u

pon a

sto

ne,

Wra

ppin

g th

at fo

ul b

ody u

p

In as fo

ul a ra

g:

I carry th

e sun in

a gold

en c

up;

The m

oon in

a silv

er b

ag.

Come let me sing into your ear;

Those dancing days are go

n

e

,

All that silk and satin gear;

Crouch upon a stone,

Wrapping that foul bod

y u

p

In as foul a rag:

I carry the sun in a g

o

ld

e

n

c

u

p

;

The moon in a silver b

a

g

.

m3D is a METAPOST macro package to produce 3D graphicsdeveloped 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 athttp://www-math.univ-poitiers.fr/~phan).

Page 20: Graphics,inparticularplottingdata · PSTricks,PiCTeX,TikZ,matplotlib(python) ... TikZ(vs.MetaFun) Graphics,inparticularplottingdata 7 TikZ(builtuponPGF)isa METAPOST-inspiredgraphics

m3D is programmer-oriented

Graphics, in particular plotting data 20

From the m3Dmanual, Anthony Phan writes:

“…Denis Roegel’s package 3D. Its syntax didn’t fit what I could have inmind about 3D-programming in METAPOST…”

“Also one of my believes is that any METAPOST programmer prefersto build his/her own macros’ system than relying on someone else’sprograms—especially when these ones are claimed to be unstable. Such aprogrammer would simply have an overview of the syntax, of some specialhacks, may publish his/her own programs on Internet and in this way givesome feed back to every other people interested in such programming.”

m3D also manipulates 3D vectors using the METAPOST ‘color’ variabletype, a triplet.

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

Page 21: Graphics,inparticularplottingdata · PSTricks,PiCTeX,TikZ,matplotlib(python) ... TikZ(vs.MetaFun) Graphics,inparticularplottingdata 7 TikZ(builtuponPGF)isa METAPOST-inspiredgraphics

So, where do we go from here?

Graphics, in particular plotting data 21

Page 22: Graphics,inparticularplottingdata · PSTricks,PiCTeX,TikZ,matplotlib(python) ... TikZ(vs.MetaFun) Graphics,inparticularplottingdata 7 TikZ(builtuponPGF)isa METAPOST-inspiredgraphics

And, does this have to do with plotting data?

Graphics, in particular plotting data 22

Plotting data in 2D and 3D as well as drawing in 3D involvestransformations from a data space to the 2D drawing space ofMETAPOST.

METAPOST 2.0 that promises arbitrary precision arithmetic maybe largely sufficient for most plotting tasks. The graph macrosneed to be re-written (simplified) and extended. New graph typescan be added.

It would be nice to introduce the type triplet (distinct fromcolor) and paths of triplets. Drawing such paths wouldtransform to a 2D projection.

Or, maybe, one can continue to work with arrays (either a tripletof arrays or arrays of triplets)?METAPOST arrays are simulated:〈variable〉→ 〈tag〉〈suffix〉〈suffix〉→ 〈empty〉|〈suffix〉〈subscript〉|〈suffix〉〈tag〉〈subscript〉→ 〈number〉|[〈numeric expression〉]

Page 23: Graphics,inparticularplottingdata · PSTricks,PiCTeX,TikZ,matplotlib(python) ... TikZ(vs.MetaFun) Graphics,inparticularplottingdata 7 TikZ(builtuponPGF)isa METAPOST-inspiredgraphics

More on arrays…

Graphics, in particular plotting data 23

awk has a slightly different approach to arrays. It has one dimensionalarrays; arrays with multiple dimensions may be simulated.

“Array subscripts may be any string, not necessarily numeric;this allows for a form of associative memory. Multiplesubscripts such as [i,j,k] are permitted; the constituents areconcatenated, 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]

Page 24: Graphics,inparticularplottingdata · PSTricks,PiCTeX,TikZ,matplotlib(python) ... TikZ(vs.MetaFun) Graphics,inparticularplottingdata 7 TikZ(builtuponPGF)isa METAPOST-inspiredgraphics

Conclusions

Graphics, in particular plotting data 24

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 ‘tooclever’ for the average user;

2. errors are easily made, and error messages require someexperience 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?);

Division of tasks between TEX, METAPOST and lua?

To respect the coherence of ConTEXt, we must avoid thesituation encountered with LATEX, were an (infinity) of different‘third-party’ packages are available.