Diagramme erstellen mit pgfplots
Stefan Pinnow
Dante Herbsttagung 2010
Institut für Chemische VerfahrenstechnikTU Clausthal
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 1
Motivation / ZielMotivation– . . . alles aus einem Guss– aber warum dann GNUplot-Diagramme einbinden?
Ziel– Diagramme für einen Artikel→ schwarz/weiß-Diagramme
– nicht jeden Befehl merken
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 2
Motivation / ZielMotivation– . . . alles aus einem Guss– aber warum dann GNUplot-Diagramme einbinden?
Ziel– Diagramme für einen Artikel→ schwarz/weiß-Diagramme
– nicht jeden Befehl merken
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 2
Motivation / ZielMotivation– . . . alles aus einem Guss– aber warum dann GNUplot-Diagramme einbinden?
Ziel– Diagramme für einen Artikel→ schwarz/weiß-Diagramme
– nicht jeden Befehl merken
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 2
Grundlagen
Paket laden
1 \input pgfplots.tex % Plain TeX\usepackage{pgfplots} % LaTeX\usemodule[pgfplots] % ConTeXt
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 3
Grundlagen
Paket laden
1 \input pgfplots.tex % Plain TeX\usepackage{pgfplots} % LaTeX\usemodule[pgfplots] % ConTeXt
Standardachse1 \begin{tikzpicture}
\begin{axis}% Plot code
\end{axis}5 \end{tikzpicture}
0 0.5 10
0.5
1
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 3
Grundlagen
Paket laden
1 \input pgfplots.tex % Plain TeX\usepackage{pgfplots} % LaTeX\usemodule[pgfplots] % ConTeXt
logarithmische x -Achse1 \begin{tikzpicture}
\begin{semilogxaxis}% Plot code
\end{semilogxaxis}5 \end{tikzpicture}
100 100.2 100.4
0
0.5
1
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 3
Grundlagen
Paket laden
1 \input pgfplots.tex % Plain TeX\usepackage{pgfplots} % LaTeX\usemodule[pgfplots] % ConTeXt
logarithmische Achsen1 \begin{tikzpicture}
\begin{loglogaxis}% Plot code
\end{loglogaxis}5 \end{tikzpicture}
100 100.2 100.4
100
100.2
100.4
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 3
Grundlagen
Paket laden
1 \input pgfplots.tex % Plain TeX\usepackage{pgfplots} % LaTeX\usemodule[pgfplots] % ConTeXt
logarithmische Achsen1 \begin{tikzpicture}
\begin{loglogaxis}% Plot code
\end{loglogaxis}5 \end{tikzpicture}
100 100.2 100.4
100
100.2
100.4
„neue“ Verbesserungennutzen
1 \pgfplotsset{compat = newest}
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 3
Kleiner Datensatz
Concentration Rate/mmol dm−3 / s−1
338.1 266.45169.1 143.4384.5 64.8042.3 34.1921.1 9.47
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 4
Kleiner Datensatz
Concentration Rate/mmol dm−3 / s−1
338.1 266.45169.1 143.4384.5 64.8042.3 34.1921.1 9.47
1 \addplot coordinates {( 338.1, 266.45 )( 169.1, 143.43 )( 84.5, 64.80 )
5 ( 42.3, 34.19 )( 21.1, 9.47 )
};
0 100 200 3000
100
200
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 4
Kleiner Datensatz
Concentration Rate/mmol dm−3 / s−1
338.1 266.45169.1 143.4384.5 64.8042.3 34.1921.1 9.47
1 \addplot coordinates {( 338.1, 266.45 )( 169.1, 143.43 )( 84.5, 64.80 )
5 ( 42.3, 34.19 )( 21.1, 9.47 )
};
0 100 200 3000
100
200
keine Farbekeine Linien
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 4
Kleiner Datensatz
1 \addplot[color = black,fill = black,mark = *,
5 only marks] coordinates {
( 338.1, 266.45 )( 169.1, 143.43 )( 84.5, 64.80 )
10 ( 42.3, 34.19 )( 21.1, 9.47 )
};
0 100 200 3000
100
200
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 4
Kleiner Datensatz
1 \addplot[color = black,fill = black,mark = *,
5 only marks] coordinates {
( 338.1, 266.45 )( 169.1, 143.43 )( 84.5, 64.80 )
10 ( 42.3, 34.19 )( 21.1, 9.47 )
};
0 100 200 3000
100
200
Trendlinie hinzufügen
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 4
Kleiner Datensatz
1 \addplot[color = black,fill = black,mark = *,
5 only marks] coordinates { ... };
\addplot[color = black,
10 mark = none]
coordinates {( 0, 0 )( 350, 279 )
15 };
0 100 200 3000
100
200
300
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 4
Kleiner Datensatz
1 \addplot[color = black,fill = black,mark = *,
5 only marks] coordinates { ... };
\addplot[color = black,
10 mark = none]
coordinates {( 0, 0 )( 350, 279 )
15 };
0 100 200 3000
100
200
300
Achsen beschriftenAchsenlimits anpassen
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 4
Kleiner Datensatz
1 \begin{axis}[xlabel =
Concentration\,/\,%mmol\,dm$^{-3}$,
5 xmax = 400,xmin = 0,ylabel = Rate\,/\,s$^{-1}$,ymax = 300,ymin = 0
10 ]...
0 100 200 300 4000
100
200
300
Concentration /mmol dm−3
Rate/s
−1
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 4
Großer Datensatz
Datenfile „data-set-two.txt“
1 Time a b c d0 49 7 41 1.367 55 9 33 1.6134 61 10 26 1.9
5 200 65 12 20 1.9...
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 5
Großer Datensatz
Datenfile „data-set-two.txt“
1 Time a b c d0 49 7 41 1.367 55 9 33 1.6134 61 10 26 1.9
5 200 65 12 20 1.9...
Einlesen des Datenfiles
1 \pgfplotstableread{data-set-two.txt}\datatable
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 5
Großer Datensatz
Erzeugen des Plots
1 \pgfplotstableread{data-set-two.txt}\datatable
\addplot table[y = a] from \datatable ;5 \addplot table[y = b] from \datatable ;
\addplot table[y = c] from \datatable ;\addplot table[y = d] from \datatable ;
0 500 1,0000
20
40
60
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 5
Großer Datensatz
Alternative
1 \pgfplotstableread{data-set-two.txt}\datatable
5
\addplot table[y = a] from \datatable ;\addplot table[y = b] {data-set-two.txt} ;
10 \addplot table[y index = 3] {data-set-two.txt} ;\addplot table[y index = 4] from \datatable ;
0 500 1,0000
20
40
60
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 5
Großer Datensatz
Hinzufügen einer Legende
1 \addplot table[y = a] from \datatable ;\addplot table[y = b] from \datatable ;\addplot table[y = c] from \datatable ;\addplot table[y = d] from \datatable ;
5
\legend{%Component \textbf{a},Component \textbf{b},Component \textbf{c},
10 Component \textbf{d},}
0 500 1,0000
20
40
60Component aComponent bComponent cComponent d
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 5
Großer Datensatz
Alternative
1 \begin{axis}[legend entries = {%
Component \textbf{a},Component \textbf{b},
5 Component \textbf{c},Component \textbf{d},
},]
10 \addplot table[y = a] from \datatable ;\addplot table[y = b] from \datatable ;\addplot table[y = c] from \datatable ;\addplot table[y = d] from \datatable ;
0 500 1,0000
20
40
60Component aComponent bComponent cComponent d
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 5
Großer Datensatz
Noch eine Alternative
1 \addplot table[y = a] from \datatable ;\addlegendentry{%
Component \textbf{a}};\addplot table[y = b] from \datatable ;
5 \addlegendentry{%Component \textbf{b}};
...0 500 1,000
0
20
40
60Component aComponent bComponent cComponent d
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 5
Großer Datensatz
Schönheitskorrekturen– Achsenbeschriftungen hinzufügen– Linien entfernen– 1000er Trenner entfernen
1 \pgfkeys{/pgf/number format/
set thousands separator =}
0 200 400 600 800 10000
20
40
60
80
Time / s
Relativ
eam
ount
/% Component aComponent bComponent cComponent d
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 5
Großer Datensatz
Eigene Marker definieren
1 \pgfdeclareplotmark{halfcircle}{%\begin{pgfscope}
\pgfsetfillcolor{white}%\pgfpathcircle{\pgfpoint{0pt}{0pt}}
5 {\pgfplotmarksize}\pgfusepathqfillstroke
\end{pgfscope}%\pgfpathmoveto
{\pgfpoint{\pgfplotmarksize}{0pt}}10 \pgfpatharc{0}{180}{\pgfplotmarksize}
\pgfpathclose\pgfusepathqfill
}
Ergebnis (200%)
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 6
Großer Datensatzneuen Marker verwenden
1 \addplot[mark = halfcircle, ...
. . . mit Optionen
1 \addplot[mark = halfcircle,mark options = {rotate = 90},
Legende verschieben
1 [legend style = { at = {(0.6,0.75)}}
]
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 7
0 200 400 600 800 10000
10
20
30
40
50
60
70
80
Time / s
Relativ
eam
ount
/% Component aComponent bComponent cComponent d
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 8
Ein experimentelles Spektrum
was wir schon kennen
1 [xlabel = Chemische
Verschiebung\,/\,ppm,xmin = 18,
5 xmax = 24,ymax = 1.25,ymin = 0
] \addplot[color = black,
10 mark = none] table {plots/data/data-set-three.txt};
18 20 22 240
0.5
1
Chemische Verschiebung / ppm
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 9
Ein experimentelles Spektrum
y -Achsenbeschriftungentfernen
1 [yticklabels = ]
18 20 22 24Chemical shift / ppm
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 10
Ein experimentelles Spektrum
y -Achsenbeschriftungentfernen
1 [yticklabels = ]
x -Achse umdrehen
1 \addplot[x dir = reverse,...
18202224Chemical shift / ppm
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 10
Ein experimentelles Spektrum
Peaks beschriften
1 \node[coordinate,pin = {[rotate=90]right:22.26}
] at (axis cs:22.26,1.1) { };
22.26
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 11
Ein experimentelles Spektrum
Peaks beschriften
1 \node[coordinate,pin = {[rotate=90]right:22.26}
] at (axis cs:22.26,1.1) { };
gedrehte Beschriftung
1 \node[coordinate,pin = {[rotate=90]5:22.32}
] at (axis cs:22.32,1.1) { };22
.26
22.32
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 11
Ein experimentelles Spektrum
1 [x dir = reverse,xlabel
= Chemical shift\,/\,ppm,xmin = 18,
5 xmax = 24,ymax = 1.75,ymin = 0,yticklabels =
] \addplot[10 color = black,
mark = none] table from {plots/data/data-set-three.txt};
\node[15 coordinate,
pin = {[rotate=90]5:22.32}] at (axis cs:22.32,1.1) { }; \node[
coordinate,pin = {[rotate=90]right:22.26}
20 ] at (axis cs:22.26,1.1) { }; ...
18192021222324
22.32
22.26
21.96
21.90
21.26
19.03
18.32
Chemical shift / ppmStefan Pinnow Diagramme erstellen mit pgfplotsICVT 12
Funktionen mit GNUplot
Formel
E (Θ) = K (K Θ)K−1
(K − 1)! exp(−K Θ)
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 13
Funktionen mit GNUplot
Formel
E (Θ) = K (K Θ)K−1
(K − 1)! exp(−K Θ)
1 % number of CSTRs\newcommand\K{3}
\addplot gnuplot[5 id=cascade_model_full,
domain=\xmin:\xmax,samples=101,
]{\K * (\K*x)^(\K-1) / (\K-1)!
10 * exp(-\K * x)};
0 1 2 30
0.2
0.4
0.6
0.8
1
ΘE(Θ
)
-shell-escape nichtvergessen
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 13
Funktionen mit GNUplotcascade_model_full.gnuplot
1 set table "cascade_model_full.table"; set format "%.5f"set format "%.7e";; set samples 101; set dummy x,y; plot [x=0:3] 3* (3*x)**(3-1) / (3-1)! * exp(-3* x);
cascade_model_full.table
1
# Curve 0 of 1, 101 points # Curve title: "3* (3*x)**(3-1) / (3-1)! *exp(-3* x)" # x y type 0.0000000e+000 0.0000000e+000 i 3.0000000e-0021.1104264e-002 i 6.0000000e-002 4.0594132e-002 i ...
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 14
Funktionen mit GNUplot
Anteil des abfließen-den Stromes, der äl-ter als Θ′ ist.
0 0.5 1 1.5 2 2.5 30
0.2
0.4
0.6
0.8
1
Θ′
Θ
E(Θ
)
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 15
Berechnungen mit GNUplot (und Co.)
erstes Beispiel noch einmal (Kleiner Datensatz)
0 100 200 300 4000
100
200
300
Concentration /mmol dm−3
Rate/s
−1
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 16
Berechnungen mit GNUplot (und Co.)
erstes Beispiel noch einmal (Kleiner Datensatz)1 ...
% plot regression curve with GNUplot (calculated also by GNUplot)\addplot [raw gnuplot] gnuplot [id=conc_rate] {
5 % define function which should be fittedf(x)=a*x;
% let gnuplot fit using column 1 and 2 of the data filefit f(x) ’conc_rate.txt’ using 1:2 via a;
% Plot the function with the specified plot range10 plot [x=\xmin:350] f(x); };
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 16
Berechnungen mit GNUplot (und Co.)
erstes Beispiel noch einmal (Kleiner Datensatz)
0 100 200 300 4000
100
200
300
Concentration / mmol dm−3
Rate
/s−
1
measured dataexternal program + pgfplotsexternal program + GNUploteverything GNUploteverything pgfplots
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 16
Berechnungen mit GNUplot (und Co.)
erstes Beispiel noch einmal (Kleiner Datensatz)
0 100 200 300 4000
100
200
300
Concentration / mmol dm−3
Rate
/s−
1
measured dataexternal program + pgfplotsexternal program + GNUploteverything GNUploteverything pgfplots
0 100 200 300 4000
100
200
300
Concentration / mmol dm−3
Rate
/s−
1
measured dataexternal program + pgfplotsexternal program + GNUploteverything GNUploteverything pgfplots
0 100 200 300 4000
100
200
300
Concentration / mmol dm−3
Rate
/s−
1
measured dataexternal program + pgfplotsexternal program + GNUploteverything GNUploteverything pgfplots
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 16
Diagramme exportierennach PDF
1 \usetikzlibrary{pgfplots.external}\tikzexternalize
nach EPS
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 17
Diagramme exportierennach PDF
1 \usetikzlibrary{pgfplots.external}\tikzexternalize
nach EPS1 \tikzset{external/system call = {latex \tikzexternalcheckshellescape
-halt-on-error -interaction=batchmode -jobname "\image" "\texsource";dvips -o "\image".ps "\image".dvi}}
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 17
Zusammenfassungpgfplots ist ein mächtiges Paket zum Erstellen von Diagrammen
Die Diagramme sind– verschiedensten Typs (Linien, Balken, 2D, 3D, Dreieck)– hoher Qualität– konsistent zum restlichen Dokument– leicht anpassbar im Stil
Keine Drittsoftware nötig
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 18
Literatur
J. WrightPlotting experimental data using pgfplotsTUGboat 31:1, 2010, p. 50https:
//www.tug.org/members/TUGboat/tb31-1/tb97wright-pgfplots.pdf
Chr. Feuersängerpgfplots manualhttp://pgfplots.sourceforge.net/pgfplots.pdf
http://pgfplots.sourceforge.net/pgfplotstable.pdf
T. Tantaupgf / TikZ manualhttp://sourceforge.net/projects/pgf
Stefan Pinnow Diagramme erstellen mit pgfplotsICVT 19
0 0.2 0.4 0.6 0.8 1 0
0.5
10
0.5
1
φxφy
φx
φy
0 10 20 30
5
10
15
20
25
0 2 4 6 8 10 12 14 160
50
100
150
200
1min load nodes cpus processes0 0.2 0.4 0.6 0.8 1 0
0.5
10
1
x
y
f(x
,y)
=x
·y
Using Coordinate Filters to fix z = 1.4
0
0.2
0.4
0.6
0.8
1
00.20.40.60.81
0
0.5
1
0
0.5
1
−4 −2 0 2 4
100
105
110
115
120
1930 1940 1950 1960 1970
3
4
5
6
7
·107
5
3.3
4
5
7
3.84.2 4.3
4.5
6.5
Pop
ulat
ion
Far Near −6 −4 −2 0 2 4 6−150
−100
−50
0
50
100
150
−11
35
79
−1 13
57
9
−1
13579
l1l2
l 3
00.5 1
1.5 2 0
0.5
10
0.5
1
− 610
110
1
− 32π −π/2 π/2 3
2π
−5
5
tan(x)
101 102 103 104 105 106
10−5
10−4
10−3
10−2
10−1
Degrees of freedom
L2
Err
or
d = 2d = 3d = 4d = 5d = 6
−6 −4 −2 0 2 4 6
−3,000
−2,000
−1,000
0
1,000
2,000
3,000 modelestimate
0 0.2 0.4 0.6 0.8
0.1
0.2
0.3
0.4
0.5
Class 1Class 2Class 3Line
−4 −2 0 2 4−4−2
02
4−10
0
10