Debian Codegraph @

texmf-dist/doc/latex/ualberta/02_Chapters/05_Plots_And_Graphs.tex

raw
1 \chapter{Plots, Charts, \& Graphs}\label{ch:plotsandgraphs}
2 Throughout this chapter we will be exploring some of the different ways of displaying your data in your thesis.
3 Mainly this will be accomplished with the \pkg{pgfplots} package.
4 In the following sections, there will be a few examples of how to generate different plots.
5 For more information on how to create plots, \href{https://mirror.its.dal.ca/ctan/graphics/pgf/contrib/pgfplots/doc/pgfplots.pdf}{\textcolor{blue}{\underline{here}}} is the manual for \pkg{pgfplots}(the package used to generate the information for TikZ to create the plots).
6 For an extensive list of examples please refer to \href{https://pgfplots.sourceforge.net/gallery.html}{\textcolor{blue}{\underline{this}}}
7
8 \section{Line Plots}
9 A simple line plot can be effectively created using the \env{axis} environment from the \pkg{pgfplots} package in \LaTeX.
10 The \pkg{pgfplots} package is a powerful tool for creating high-quality plots directly within \LaTeX\ documents.
11 It builds upon the \pkg{TikZ} package and provides a comprehensive set of options for plotting and customizing graphs.
12
13 The following code (see \Cref{lst:line-plot}) can be used to create the figure shown in \Cref{plt:line-plot}.
14 \begin{figure}[htbp]
15 \centering
16 \begin{subfigure}[b]{0.45\linewidth}
17 \centering
18 \begin{lstlisting}[style=LaTeXStyle,basicstyle=\scriptsize\ttfamily,frame=single]
19 \begin{figure}[htbp]
20 \centering
21 \begin{tikzpicture}
22 \begin{axis}[
23 title={Simple Line Plot},
24 xlabel={X-axis},
25 ylabel={Y-axis},
26 ]
27 \addplot coordinates {
28 (0,0)
29 (1,1)
30 (2,4)
31 (3,9)
32 (4,16)
33 };
34 \end{axis}
35 \end{tikzpicture}
36 \caption{A simple line plot.}
37 \label{fig:line-plot}
38 \end{figure}
39 \end{lstlisting}
40 \caption{}
41 \label{lst:line-plot}
42 \end{subfigure}
43 \hfill
44 \begin{subfigure}[b]{0.45\linewidth}
45 \centering
46 \begin{tikzpicture}
47 \begin{axis}[
48 width=\linewidth,
49 title={Simple Line Plot},
50 xlabel={X-axis},
51 ylabel={Y-axis},
52 ]
53 \addplot coordinates {
54 (0,0)
55 (1,1)
56 (2,4)
57 (3,9)
58 (4,16)
59 };
60 \end{axis}
61 \end{tikzpicture}
62 \caption{}
63 \label{plt:line-plot}
64 \end{subfigure}
65 \caption[A simple line plot.]{A simple line plot (\subref{plt:line-plot}) and the code to generate the plot (\subref{lst:line-plot})}\label{fig:line-plot}
66 \end{figure}
67 Expanding on this example, we can add a second plot by adding the following code below the closing bracket and semi-colon (\texttt{\};}) of the \cmd{addplot} command:
68 \begin{lstlisting}[float=ht,caption=,label=lst:line-plot-2-add,style=LaTeXStyle,basicstyle=\small\ttfamily,]
69 \addplot coordinates {
70 (0,16)
71 (1,9)
72 (2,4)
73 (3,1)
74 (4,0)
75 };
76 \end{lstlisting}
77 This will result in the addition of the second red line shown in \Cref{fig:line-plot-2}.
78 \begin{figure}[htbp]
79 \begin{subfigure}[b]{0.45\linewidth}
80 \centering
81 \begin{lstlisting}[style=LaTeXStyle,basicstyle=\scriptsize\ttfamily,frame=single]
82 \begin{tikzpicture}
83 \begin{axis}[
84 title={Dual Line Plot},
85 xlabel={X-axis},
86 ylabel={Y-axis},
87 ]
88 \addplot coordinates {
89 (0,0)
90 (1,1)
91 (2,4)
92 (3,9)
93 (4,16)
94 };
95 \addplot coordinates {
96 (0,16)
97 (1,9)
98 (2,4)
99 (3,1)
100 (4,0)
101 };
102 \end{axis}
103 \end{tikzpicture}
104 \end{lstlisting}
105 \caption{}
106 \label{lst:line-plot-2}
107 \end{subfigure}
108 \hfill
109 \begin{subfigure}[b]{0.45\linewidth}
110 \centering
111 \begin{tikzpicture}
112 \begin{axis}[
113 width=\linewidth,
114 title={Dual Line Plot},
115 xlabel={X-axis},
116 ylabel={Y-axis},
117 ]
118 \addplot coordinates {
119 (0,0)
120 (1,1)
121 (2,4)
122 (3,9)
123 (4,16)
124 };
125 \addplot coordinates {
126 (0,16)
127 (1,9)
128 (2,4)
129 (3,1)
130 (4,0)
131 };
132 \end{axis}
133 \end{tikzpicture}
134 \caption{}
135 \label{plt:line-plot-2}
136 \end{subfigure}
137 \caption{A simple line plot with two sets of data.}
138 \label{fig:line-plot-2}
139 \end{figure}
140
141 \section{Customizing Plots}
142 This section provides some ways to increase the readability and customization of the plots we generate.
143 \subsection{Adding a Legend}
144 Legends can be added to plots for better readability.
145 To add a legend to your plot you can use the code in \Cref{lst:legend-plot-header,lst:legend-plot-plots} to generate the plot shown in \Cref{fig:legend-plot}.
146 Each plot is individually added to the legend by adding a \cmd{addlegendentry}\mopt{YOUR LEGEND ENTRY HERE} command following the \cmd{addplot} command.
147
148 \note{The position of the legend can be specified by using the optional parameter \opt{legend pos=}} followed by a set of compass coordinates.
149
150 \begin{lstlisting}[float=htbp,caption=,label=lst:legend-plot-header,style=LaTeXStyle,basicstyle=\small\ttfamily,]
151 \begin{figure}[H]
152 \centering
153 \begin{tikzpicture}
154 \begin{axis}[
155 title={Plot with Added Legend},
156 xlabel={X-axis},
157 ylabel={Y-axis},
158 legend pos=north west,
159 ]
160 \end{lstlisting}
161 \begin{lstlisting}[float=htbp,caption=,label=lst:legend-plot-plots,style=LaTeXStyle,basicstyle=\small\ttfamily,]
162 \addplot coordinates {
163 (0,0)
164 (1,1)
165 (2,4)
166 (3,9)
167 (4,16)
168 };
169 \addlegendentry{\(y = x^2\)}
170 \addplot coordinates {
171 (0,16)
172 (1,9)
173 (2,4)
174 (3,1)
175 (4,0)
176 };
177 \addlegendentry{\(y = 16 - x^2\)}
178 \end{axis}
179 \end{tikzpicture}
180 \caption{A customized plot with a legend.}
181 \label{fig:legend-plot}
182 \end{figure}
183 \end{lstlisting}
184 \begin{figure}[H]
185 \centering
186 \begin{tikzpicture}
187 \begin{axis}[
188 title={Plot with Added Legend},
189 xlabel={X-axis},
190 ylabel={Y-axis},
191 legend pos=north west,
192 ]
193 \addplot coordinates {
194 (0,0)
195 (1,1)
196 (2,4)
197 (3,9)
198 (4,16)
199 };
200 \addlegendentry{\(y = x^2\)}
201 \addplot coordinates {
202 (0,16)
203 (1,9)
204 (2,4)
205 (3,1)
206 (4,0)
207 };
208 \addlegendentry{\(y = 16 - x^2\)}
209 \end{axis}
210 \end{tikzpicture}
211 \caption{A customized plot with a legend.}
212 \label{fig:legend-plot}
213 \end{figure}
214
215 \subsection{Adding Grid Lines}
216 To add gridlines to your plot
217 \begin{figure}[H]
218 \centering
219 \begin{tikzpicture}
220 \begin{axis}[
221 title={Plot with Added Gridlines},
222 xlabel={X-axis},
223 ylabel={Y-axis},
224 legend pos=north west,
225 grid=major,
226 ]
227 \addplot coordinates {
228 (0,0)
229 (1,1)
230 (2,4)
231 (3,9)
232 (4,16)
233 };
234 \addlegendentry{\(y = x^2\)}
235 \addplot coordinates {
236 (0,16)
237 (1,9)
238 (2,4)
239 (3,1)
240 (4,0)
241 };
242 \addlegendentry{\(y = 16 - x^2\)}
243 \end{axis}
244 \end{tikzpicture}
245 \caption{A customized plot with added gridlines.}
246 \label{fig:gridlines-plot}
247 \end{figure}
248
249 \subsection{Changing Colors and Line Styles}
250 Colors and line styles can be easily modified:
251
252 \begin{figure}[H]
253 \centering
254 \begin{tikzpicture}
255 \begin{axis}[
256 title={Colored and Styled Plot},
257 xlabel={X-axis},
258 ylabel={Y-axis},
259 legend pos=north west,
260 grid=major,
261 ]
262 \addplot[
263 color=red,
264 dashed
265 ] coordinates {
266 (0,0)
267 (1,1)
268 (2,4)
269 (3,9)
270 (4,16)
271 };
272 \addlegendentry{\(y = x^2\)}
273 \addplot[
274 color=blue,
275 thick,
276 dotted
277 ] coordinates {
278 (0,16)
279 (1,9)
280 (2,4)
281 (3,1)
282 (4,0)
283 };
284 \addlegendentry{\(y = 16 - x^2\)}
285 \end{axis}
286 \end{tikzpicture}
287 \caption{A plot with customized colors and line styles}
288 \label{fig:colour-plot}
289 \end{figure}
290
291 \section{Advanced Plot Types}
292 \subsection{Equations}
293 \begin{figure}[H]
294 \centering
295 \begin{tikzpicture}
296 \begin{axis}[
297 width=0.65\linewidth,
298 axis lines = left,
299 xlabel = \(x\),
300 ylabel = {\(f(x)\)},
301 ]
302 \addplot[
303 domain=-10:10,
304 samples=20,
305 color=red,
306 ]
307 {x^2 - 2*x - 1};
308 \addlegendentry{\(x^2 - 2x - 1\)}
309 \addplot[
310 domain=-10:10,
311 samples=20,
312 color=blue,
313 ]
314 {x^2 + 2*x + 1};
315 \addlegendentry{\(x^2 + 2x + 1\)}
316 \end{axis}
317 \end{tikzpicture}
318 \caption{Plot of two parabola.}
319 \label{fig:equation-plot}
320 \end{figure}
321 \subsection{Scatter Plot with External Data}
322 \begin{figure}[H]
323 \centering
324 \begin{tikzpicture}
325 \begin{axis}[
326 width=0.65\linewidth,
327 enlargelimits=true,
328 ]
329 \addplot+[
330 only marks,
331 scatter,
332 mark=*,
333 mark size=2.9pt
334 ]
335 table[meta=ma]
336 {\insertdata{scattered_example.dat}};%{./99_Inclusions/Data/scattered_example.dat};
337 \end{axis}
338 \end{tikzpicture}
339 \caption{Example of a Scatter Plot.}
340 \label{fig:scatter-plot}
341 \end{figure}
342 \subsection{Bar Plot}
343 Bar plots are useful for categorical data.
344 Here’s how to create one:
345 \begin{figure}[H]
346 \centering
347 \begin{tikzpicture}
348 \begin{axis}[
349 ybar,
350 symbolic x coords={A, B, C, D, E},
351 xtick=data,
352 nodes near coords,
353 ymin=0,
354 ]
355 \addplot coordinates {
356 (A,5) (B,10) (C,15) (D,20) (E,25)
357 };
358 \end{axis}
359 \end{tikzpicture}
360 \caption{A bar plot}
361 \label{fig:bar-plot}
362 \end{figure}
363
364 \begin{figure}[H]
365 \centering
366 \begin{tikzpicture}
367 \begin{axis}[
368 width=0.65\linewidth,
369 x tick label style={/pgf/number format/1000 sep=},
370 xlabel=Year,
371 ylabel= Population,
372 enlargelimits=0.15,
373 legend style={at={(0.5,-0.21)},
374 anchor=north,legend columns=-1},
375 ybar,
376 bar width=15pt,
377 ]
378 \addplot
379 coordinates {(2012,388950) (2011,393007) (2010,398449) (2009,395972) (2008,384621)};
380 \addplot
381 coordinates {(2012,408184) (2011,408348) (2010,414870) (2009,412156) (2008,415426)};
382 \legend{Men,Women}
383 \end{axis}
384 \end{tikzpicture}
385 \caption{Example of a Bar Graph.}
386 \label{fig:bar-plot-2}
387 \end{figure}
388
389 \subsection{Pie Chart}
390 Pie charts are less common in \LaTeX, as well as, a lot of other media due to the pour representation of the data.
391 %TODO: add citation for - https://scc.ms.unimelb.edu.au/resources/data-visualisation-and-exploration/no_pie-charts
392 However, if you are still inclined to use them the remainder of this section will show how the package \pkg{pgf-pie} can be used to create consistent graphs that look good.
393 \begin{figure}[H]
394 \centering
395 \begin{tikzpicture}
396 \pie[
397 radius=2,
398 text=legend,
399 ]{
400 20/A, 30/B, 10/C, 25/D, 15/E
401 }
402 \end{tikzpicture}
403 \caption{A basic pie chart.}
404 \label{fig:pie-chart}
405 \end{figure}
406
407 \begin{figure}[H]
408 \centering
409 \begin{tikzpicture}
410 \pie[
411 radius=2,
412 text=legend,
413 explode={0, 0, 0, 0, 0.2},
414 ]{
415 20/A, 30/B, 10/C, 25/D, 15/E
416 }
417 \end{tikzpicture}
418 \caption{A pie chart with an ``Exploded'' slice.}
419 \label{fig:pie-chart-2}
420 \end{figure}
421
422 \begin{figure}[H]
423 \centering
424 \begin{tikzpicture}
425 \pie[
426 square,
427 text=legend,
428 ]{
429 20/A, 30/B, 10/C, 25/D, 15/E
430 }
431 \end{tikzpicture}
432 \caption{A ``square'' pie chart.}
433 \label{fig:pie-chart-3}
434 \end{figure}
435
436 \subsection{3D Plot}
437 3D plots can be created for more complex data visualization:
438 \begin{figure}[H]
439 \centering
440 \begin{tikzpicture}
441 \begin{axis}[
442 view={60}{30},
443 xlabel={X-axis},
444 ylabel={Y-axis},
445 zlabel={Z-axis},
446 ]
447 \addplot3[surf] {
448 x^2 - y^2
449 };
450 \end{axis}
451 \end{tikzpicture}
452 \caption{A 3D surface plot}
453 \label{fig:3d-plot}
454 \end{figure}
455
456 \begin{figure}[H]
457 \centering
458 \begin{tikzpicture}
459 \begin{axis}[
460 width=0.65\linewidth,
461 title=Example using the mesh parameter,
462 hide axis,
463 colormap/cool,
464 ]
465 \addplot3[
466 mesh,
467 samples=25,
468 domain=-8:8,
469 ]
470 {sin(deg(sqrt(x^2+y^2)))/sqrt(x^2+y^2)};
471 \addlegendentry{\(\frac{sin(r)}{r}\)}
472 \end{axis}
473 \end{tikzpicture}
474 \caption{Example of a 3D Plot}
475 \label{fig:3d-plot-2}
476 \end{figure}
477
478 \subsection{Polar Plot}
479 Polar plots are useful for circular data:
480 \begin{figure}[H]
481 \centering
482 \begin{tikzpicture}
483 \begin{polaraxis}[
484 title={Polar Plot},
485 xlabel={Radius},
486 ylabel={Angle (degrees)},
487 ]
488 \addplot coordinates {
489 (0,1) (30,2) (60,3) (90,4)
490 (120,5) (150,6) (180,7)
491 };
492 \end{polaraxis}
493 \end{tikzpicture}
494 \caption{A polar plot}
495 \label{fig:polar-plot}
496 \end{figure}
497
498 \subsection{Box Plot}
499 Box plots are used to visualize the distribution of data:
500 \begin{figure}[H]
501 \centering
502 \begin{tikzpicture}
503 \begin{axis}[
504 boxplot/draw direction=y,
505 x axis line style = {opacity=0},
506 axis x line* = bottom,
507 axis y line = left,
508 ymajorgrids,
509 ymin=0,
510 ymax=10,
511 enlarge y limits,
512 xticklabel style = {align=center, font=\small, rotate=60},
513 xtick = {1,2,3,4},
514 xticklabels = {Apples, Oranges, Bananas, Grapes},
515 ]
516 \addplot+[
517 boxplot prepared={
518 median=5,
519 upper quartile=6,
520 lower quartile=4,
521 upper whisker=7,
522 lower whisker=3
523 },
524 fill=red,
525 draw=black
526 ] coordinates {};
527 \addplot+[
528 boxplot prepared={
529 median=4,
530 upper quartile=5,
531 lower quartile=2,
532 upper whisker=8,
533 lower whisker=1
534 },
535 fill=orange,
536 draw=black
537 ] coordinates {};
538 \addplot+[
539 boxplot prepared={
540 median=6,
541 upper quartile=6.5,
542 lower quartile=2,
543 upper whisker=7,
544 lower whisker=1.5
545 },
546 fill=yellow,
547 draw=black
548 ] coordinates {};
549 \addplot+[
550 boxplot prepared={
551 median=3,
552 upper quartile=4,
553 lower quartile=2.5,
554 upper whisker=9,
555 lower whisker=2
556 },
557 fill=green,
558 draw=black
559 ] coordinates {};
560 \end{axis}
561 \end{tikzpicture}
562 \caption{A box plot}
563 \label{fig:box-plot}
564 \end{figure}
565
566 \section{Conclusion}
567 The \pkg{pgfplots} package is an incredibly versatile tool for creating a wide range of plots and graphs in \LaTeX.
568 This chapter has provided examples of various plot types and customization options, showcasing the power and flexibility of \pkg{pgfplots}.
569 By leveraging these capabilities, you can create high-quality, publication-ready figures for your thesis.