Skip to content

Instantly share code, notes, and snippets.

@johnwickerson
Last active October 28, 2019 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnwickerson/eb063a1b6aa2478ccb6540699cb2d336 to your computer and use it in GitHub Desktop.
Save johnwickerson/eb063a1b6aa2478ccb6540699cb2d336 to your computer and use it in GitHub Desktop.
LaTeX code for creating some scatter plots and bar charts using pgfplots
\documentclass[margin=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usetikzlibrary{calc}
\usepackage{filecontents}
\begin{filecontents}{foo.dat}
theirs ours1 ours2 label
29 37 23 A
21 16 11 B
39 45 34 C
17 9 14 D
18 29 21 E
31 39 25 F
23 18 13 G
41 47 36 H
14 6 13 I
20 31 23 J
29 35 24 K
23 17 14 L
40 44 37 M
12 7 12 N
17 30 22 O
29 38 24 P
21 19 17 Q
43 42 38 R
16 8 14 S
18 34 26 T
42 47 38 U
19 22 22 V
29 32 28 W
24 15 16 X
13 8 13 Y
12 4 11 Z
\end{filecontents}
\begin{document}
\colorlet{reddy}{red!80!black}
\colorlet{greeny}{green!70!black}
\colorlet{bluey}{blue!80!black}
\begin{tikzpicture}
\begin{axis}[
width=190mm,
height=70mm,
bar width=1mm,
ybar=0mm,
ymin=0,
enlarge x limits={abs=4mm},
ylabel={time taken (s)},
xlabel={benchmark},
xtick=data,
symbolic x coords={A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z},
]
\addplot[draw=none, fill=reddy]
table[x=label,y=theirs] {foo.dat};
\addlegendentry{old}
\addplot[draw=none, fill=greeny]
table[x=label,y=ours1] {foo.dat};
\addlegendentry{new}
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
width=70mm,
height=70mm,
xmin=0,
ymin=0,
xmax=50,
ymax=50,
xlabel={time taken by old (s)},
ylabel={time taken by new (s)},
legend pos=north west,
]
\addplot[
fill opacity=0.4,
draw opacity=0,
mark=*,
fill=black,
only marks,
]
table[x=theirs,y=ours1, meta=label] {foo.dat};
\draw[dotted] (rel axis cs:0,0) -- (rel axis cs:1,1);
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
width=190mm,
height=70mm,
bar width=1mm,
ybar=0mm,
ymin=0,
enlarge x limits={abs=4mm},
ylabel={time taken (s)},
xlabel={benchmark},
xtick=data,
symbolic x coords={A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z},
]
\addplot[draw=none, fill=reddy]
table[x=label,y=theirs] {foo.dat};
\addlegendentry{old}
\addplot[draw=none, fill=greeny]
table[x=label,y=ours1] {foo.dat};
\addlegendentry{new v1}
\addplot[draw=none, fill=bluey]
table[x=label,y=ours2] {foo.dat};
\addlegendentry{new v2}
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
width=70mm,
height=70mm,
xmin=0,
ymin=0,
xmax=50,
ymax=50,
xlabel={time taken by old (s)},
ylabel={time taken by new (s)},
legend pos=north west,
]
\addplot[
mark=*,
fill=greeny,
fill opacity=0.4,
draw opacity=0,
only marks,
]
table[x=theirs,y=ours1, meta=label] {foo.dat};
\addlegendentry{new v1}
\addplot[
mark=square*,
fill=bluey,
fill opacity=0.4,
draw opacity=0,
only marks,
]
table[x=theirs,y=ours2, meta=label] {foo.dat};
\addlegendentry{new v2}
\draw[dotted] (rel axis cs:0,0) -- (rel axis cs:1,1);
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
width=70mm,
height=70mm,
xmin=0,
ymin=0,
xmax=50,
ymax=50,
xlabel={time taken by old (s)},
ylabel={time taken by new (s)},
legend pos=north west,
]
\pgfplotsinvokeforeach{0,1,...,25}{
\pgfplotstablegetelem{#1}{theirs}\of{foo.dat} \xdef\x{\pgfplotsretval}
\pgfplotstablegetelem{#1}{ours1}\of{foo.dat} \xdef\yone{\pgfplotsretval}
\pgfplotstablegetelem{#1}{ours2}\of{foo.dat} \xdef\ytwo{\pgfplotsretval}
\edef\tmp{\noexpand%
\draw[opacity=0.4, -latex] (\x, \yone) to (\x,\ytwo);
}\tmp
}
\draw[dotted] (rel axis cs:0,0) -- (rel axis cs:1,1);
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
width=190mm,
height=70mm,
bar width=1mm,
ybar=0mm,
ymin=0,
enlarge x limits={abs=4mm},
ybar=0pt,
ylabel={time taken (relative to old)},
xlabel={benchmark},
xtick=data,
symbolic x coords={A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z},
legend pos=north west,
]
\addplot[draw=none, fill=reddy]
table[x=label, y expr=\thisrow{theirs}/\thisrow{theirs}] {foo.dat};
\addlegendentry{old}
\addplot[draw=none, fill=greeny]
table[x=label, y expr=\thisrow{ours1}/\thisrow{theirs}] {foo.dat};
\addlegendentry{new v1}
\addplot[draw=none, fill=bluey]
table[x=label, y expr=\thisrow{ours2}/\thisrow{theirs}] {foo.dat};
\addlegendentry{new v2}
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
width=190mm,
height=70mm,
bar width=1mm,
enlarge x limits={abs=4mm},
ybar=0pt,
ymode=log,
log ticks with fixed point,
ymin=0.25,
ymax=2.00,
ytick={0.25, 0.5, 0.75, 1, 1.25, 1.5, 2},
ylabel={time taken (relative to old)},
xlabel={benchmark},
symbolic x coords={A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z},
xtick=data,
legend pos=south west,
]
\addplot[draw=none, fill=greeny]
table[x=label, y expr=\thisrow{ours1}/\thisrow{theirs}] {foo.dat};
\addlegendentry{ours v1}
\addplot[draw=none, fill=bluey]
table[x=label, y expr=\thisrow{ours2}/\thisrow{theirs}] {foo.dat};
\addlegendentry{ours v2}
\draw[shorten <=-10mm, shorten >=-10mm] (axis cs:A,1) -- (axis cs:Z,1);
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
width=70mm,
height=70mm,
xmode=log,
ymode=log,
xmin=0.25,
ymin=0.25,
xmax=1.75,
ymax=1.75,
log ticks with fixed point,
xtick={0.25, 0.5, 0.75, 1, 1.25, 1.75},
ytick={0.25, 0.5, 0.75, 1, 1.25, 1.75},
xlabel={time taken by new v1 (relative to old)},
ylabel={time taken by new v2 (relative to old)},
]
\addplot[
fill opacity=0.4,
draw opacity=0,
mark=*,
fill=black,
only marks,
]
table[
x expr=\thisrow{ours1}/\thisrow{theirs},
y expr=\thisrow{ours2}/\thisrow{theirs},
meta=label
] {foo.dat};
\draw[dotted] (rel axis cs:0,0) -- (rel axis cs:1,1);
\draw[dotted] (axis cs:1,0.25) -- (axis cs:1,1.75);
\draw[dotted] (axis cs:0.25,1) -- (axis cs:1.75,1);
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
width=70mm,
height=70mm,
xmin=0,
ymin=0,
xmax=50,
ymax=50,
xlabel={time taken by old (s)},
ylabel={time taken by new (s)},
legend pos=north west,
]
% JW: Sorry about the following code. I should work out how to
% make it use the same foo.dat data file.
\foreach \x/\y/\w/\h in {
% x position / y position / width of ellipse / height of ellipse
29 / 37 / 1.0 / 2.5,
21 / 16 / 1.5 / 1.5,
39 / 45 / 0.5 / 1,
17 / 9 / 2 / 2,
18 / 29 / 1.5 / 2.5,
31 / 39 / 1 / 1.5,
23 / 18 / 0.5 / 1,
41 / 47 / 1.5 / 0.5,
14 / 6 / 2 / 1.5,
20 / 31 / 1.5 / 2,
29 / 35 / 0.5 / 1.5,
23 / 17 / 1 / 1,
40 / 44 / 1.5 / 1,
12 / 7 / 2 / 2.5,
17 / 30 / 1.5 / 2,
29 / 38 / 1 / 1.5,
21 / 19 / 1.5 / 2.5,
43 / 42 / 2 / 2.5,
16 / 8 / 1 / 1.5,
18 / 34 / 2 / 2,
42 / 47 / 2 / 3.5,
19 / 22 / 1.5 / 1.5,
29 / 32 / 0.5 / 1.5,
24 / 15 / 1 / 1,
13 / 8 / 0.5 / 2.5,
12 / 4 / 0.5 / 2
}{
\edef\temp{% JW: some unpleasant hackery needed here (https://tex.stackexchange.com/a/17817/25356)
\noexpand\path[fill=black,fill opacity=0.4, draw opacity=0]
let \noexpand\p1=($(axis cs: \x+\w,\y+\h)-(axis cs: \x-\w,\y-\h)$)
in ($(axis cs: \x+\w,\y+\h)!0.5!(axis cs: \x-\w,\y-\h)$)
circle[x radius=\noexpand\x1/2,y radius=\noexpand\y1/2];
}
\temp
}
\draw[dotted] (rel axis cs:0,0) -- (rel axis cs:1,1);
\end{axis}
\end{tikzpicture}
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment