pgfplots_demo.tex

Download
latex 372 lines 7.8 KB
  1% pgfplots_demo.tex
  2% PGFPlots demonstrations: function plots, scatter plots, bar charts
  3% Showcases data visualization capabilities of PGFPlots
  4
  5\documentclass[12pt,a4paper]{article}
  6
  7\usepackage{pgfplots}
  8\usepackage[margin=1in]{geometry}
  9\usepackage{amsmath}
 10
 11% Set PGFPlots version for compatibility
 12\pgfplotsset{compat=1.18}
 13
 14\title{PGFPlots: Data Visualization in \LaTeX}
 15\author{LaTeX Student}
 16\date{\today}
 17
 18\begin{document}
 19
 20\maketitle
 21
 22\section{Function Plots}
 23
 24\subsection{Trigonometric Functions}
 25
 26\begin{center}
 27\begin{tikzpicture}
 28\begin{axis}[
 29    width=12cm,
 30    height=8cm,
 31    xlabel={$x$},
 32    ylabel={$y$},
 33    title={Sine and Cosine Functions},
 34    domain=-2*pi:2*pi,
 35    samples=100,
 36    grid=major,
 37    legend pos=north east,
 38    xticklabel={\pgfmathparse{\tick/pi}\pgfmathprintnumber{\pgfmathresult}$\pi$},
 39    xtick={-6.28318, -3.14159, 0, 3.14159, 6.28318}
 40]
 41    \addplot[blue, thick] {sin(deg(x))};
 42    \addlegendentry{$\sin(x)$}
 43
 44    \addplot[red, thick, dashed] {cos(deg(x))};
 45    \addlegendentry{$\cos(x)$}
 46
 47    \addplot[green!70!black, thick, dotted] {sin(deg(x))*cos(deg(x))};
 48    \addlegendentry{$\sin(x)\cos(x)$}
 49\end{axis}
 50\end{tikzpicture}
 51\end{center}
 52
 53\subsection{Exponential and Logarithmic Functions}
 54
 55\begin{center}
 56\begin{tikzpicture}
 57\begin{axis}[
 58    width=12cm,
 59    height=8cm,
 60    xlabel={$x$},
 61    ylabel={$y$},
 62    title={Exponential and Logarithmic Functions},
 63    domain=-2:3,
 64    samples=100,
 65    grid=major,
 66    legend pos=north west,
 67    ymin=-2,
 68    ymax=8
 69]
 70    \addplot[blue, thick] {exp(x)};
 71    \addlegendentry{$e^x$}
 72
 73    \addplot[red, thick] {ln(x+3)};
 74    \addlegendentry{$\ln(x+3)$}
 75
 76    \addplot[green!70!black, thick, dashed] {exp(-x)};
 77    \addlegendentry{$e^{-x}$}
 78\end{axis}
 79\end{tikzpicture}
 80\end{center}
 81
 82\subsection{Polynomial Functions}
 83
 84\begin{center}
 85\begin{tikzpicture}
 86\begin{axis}[
 87    width=12cm,
 88    height=8cm,
 89    xlabel={$x$},
 90    ylabel={$y$},
 91    title={Polynomial Functions},
 92    domain=-3:3,
 93    samples=100,
 94    grid=major,
 95    legend pos=north west,
 96    ymin=-10,
 97    ymax=30
 98]
 99    \addplot[blue, thick] {x^2};
100    \addlegendentry{$x^2$}
101
102    \addplot[red, thick] {x^3};
103    \addlegendentry{$x^3$}
104
105    \addplot[green!70!black, thick] {x^2 - 2*x + 1};
106    \addlegendentry{$x^2 - 2x + 1$}
107
108    \addplot[orange, thick, dashed] {-0.5*x^2 + 3*x + 2};
109    \addlegendentry{$-\frac{1}{2}x^2 + 3x + 2$}
110\end{axis}
111\end{tikzpicture}
112\end{center}
113
114\section{Parametric Plots}
115
116\subsection{Parametric Curves}
117
118\begin{center}
119\begin{tikzpicture}
120\begin{axis}[
121    width=10cm,
122    height=10cm,
123    xlabel={$x$},
124    ylabel={$y$},
125    title={Parametric Circle and Spiral},
126    grid=major,
127    legend pos=south east,
128    axis equal
129]
130    % Circle
131    \addplot[blue, thick, domain=0:2*pi, samples=100] ({cos(deg(x))}, {sin(deg(x))});
132    \addlegendentry{Circle}
133
134    % Spiral
135    \addplot[red, thick, domain=0:4*pi, samples=200] ({x*cos(deg(x))/10}, {x*sin(deg(x))/10});
136    \addlegendentry{Spiral}
137\end{axis}
138\end{tikzpicture}
139\end{center}
140
141\section{Scatter Plots}
142
143\subsection{Random Data Scatter}
144
145\begin{center}
146\begin{tikzpicture}
147\begin{axis}[
148    width=12cm,
149    height=8cm,
150    xlabel={Feature 1},
151    ylabel={Feature 2},
152    title={Scatter Plot with Different Markers},
153    grid=major,
154    legend pos=north west
155]
156    % Dataset 1
157    \addplot[only marks, mark=*, blue] coordinates {
158        (1, 2.3) (1.5, 2.8) (2, 3.1) (2.5, 3.5) (3, 4.2)
159        (3.5, 4.8) (4, 5.1) (4.5, 5.9) (5, 6.2)
160    };
161    \addlegendentry{Class A}
162
163    % Dataset 2
164    \addplot[only marks, mark=square*, red] coordinates {
165        (1, 5.5) (1.5, 5.2) (2, 4.8) (2.5, 4.3) (3, 3.9)
166        (3.5, 3.2) (4, 2.8) (4.5, 2.3) (5, 1.9)
167    };
168    \addlegendentry{Class B}
169
170    % Dataset 3
171    \addplot[only marks, mark=triangle*, green!70!black] coordinates {
172        (0.5, 3.5) (1, 3.8) (1.5, 4.1) (2, 4.0) (2.5, 3.9)
173        (3, 4.2) (3.5, 4.5) (4, 4.3) (4.5, 4.1) (5, 3.8)
174    };
175    \addlegendentry{Class C}
176\end{axis}
177\end{tikzpicture}
178\end{center}
179
180\section{Bar Charts}
181
182\subsection{Simple Bar Chart}
183
184\begin{center}
185\begin{tikzpicture}
186\begin{axis}[
187    width=12cm,
188    height=8cm,
189    ybar,
190    bar width=15pt,
191    xlabel={Category},
192    ylabel={Value},
193    title={Bar Chart Example},
194    symbolic x coords={A, B, C, D, E},
195    xtick=data,
196    nodes near coords,
197    nodes near coords align={vertical},
198    ymin=0,
199    ymax=100,
200    grid=major
201]
202    \addplot[fill=blue!60] coordinates {(A,65) (B,78) (C,54) (D,89) (E,72)};
203\end{axis}
204\end{tikzpicture}
205\end{center}
206
207\subsection{Grouped Bar Chart}
208
209\begin{center}
210\begin{tikzpicture}
211\begin{axis}[
212    width=13cm,
213    height=8cm,
214    ybar,
215    bar width=10pt,
216    xlabel={Month},
217    ylabel={Sales (thousands)},
218    title={Quarterly Sales Comparison},
219    symbolic x coords={Jan, Feb, Mar, Apr, May, Jun},
220    xtick=data,
221    legend pos=north west,
222    ymin=0,
223    ymax=100,
224    grid=major,
225    enlarge x limits=0.15
226]
227    \addplot[fill=blue!60] coordinates {
228        (Jan,45) (Feb,52) (Mar,58) (Apr,65) (May,72) (Jun,78)
229    };
230    \addlegendentry{Product A}
231
232    \addplot[fill=red!60] coordinates {
233        (Jan,38) (Feb,42) (Mar,48) (Apr,55) (May,60) (Jun,65)
234    };
235    \addlegendentry{Product B}
236
237    \addplot[fill=green!60] coordinates {
238        (Jan,28) (Feb,35) (Mar,38) (Apr,42) (May,48) (Jun,52)
239    };
240    \addlegendentry{Product C}
241\end{axis}
242\end{tikzpicture}
243\end{center}
244
245\subsection{Horizontal Bar Chart}
246
247\begin{center}
248\begin{tikzpicture}
249\begin{axis}[
250    width=12cm,
251    height=8cm,
252    xbar,
253    bar width=12pt,
254    ylabel={Programming Language},
255    xlabel={Popularity Score},
256    title={Programming Language Popularity},
257    symbolic y coords={Python, Java, JavaScript, C++, Go, Rust},
258    ytick=data,
259    nodes near coords,
260    nodes near coords align={horizontal},
261    xmin=0,
262    xmax=100,
263    grid=major
264]
265    \addplot[fill=orange!60] coordinates {
266        (92,Python) (78,Java) (85,JavaScript) (65,C++) (58,Go) (45,Rust)
267    };
268\end{axis}
269\end{tikzpicture}
270\end{center}
271
272\section{Advanced Plots}
273
274\subsection{Multiple Y-Axes}
275
276\begin{center}
277\begin{tikzpicture}
278\begin{axis}[
279    width=12cm,
280    height=8cm,
281    xlabel={Time (months)},
282    ylabel={Temperature ($^\circ$C)},
283    title={Temperature and Precipitation},
284    axis y line*=left,
285    grid=major,
286    legend pos=north west
287]
288    \addplot[blue, thick, mark=*] coordinates {
289        (1,5) (2,7) (3,12) (4,18) (5,22) (6,25) (7,27)
290        (8,26) (9,22) (10,16) (11,10) (12,6)
291    };
292    \addlegendentry{Temperature}
293\end{axis}
294
295\begin{axis}[
296    width=12cm,
297    height=8cm,
298    ylabel={Precipitation (mm)},
299    axis y line*=right,
300    axis x line=none,
301    legend pos=south east
302]
303    \addplot[red, thick, dashed, mark=square*] coordinates {
304        (1,45) (2,38) (3,42) (4,55) (5,68) (6,72) (7,65)
305        (8,58) (9,62) (10,55) (11,48) (12,50)
306    };
307    \addlegendentry{Precipitation}
308\end{axis}
309\end{tikzpicture}
310\end{center}
311
312\subsection{3D Surface Plot}
313
314\begin{center}
315\begin{tikzpicture}
316\begin{axis}[
317    width=12cm,
318    height=10cm,
319    view={45}{30},
320    xlabel={$x$},
321    ylabel={$y$},
322    zlabel={$z$},
323    title={3D Surface: $z = \sin(x)\cos(y)$},
324    colormap/viridis,
325    grid=major
326]
327    \addplot3[
328        surf,
329        samples=30,
330        domain=-pi:pi
331    ]
332    {sin(deg(x))*cos(deg(y))};
333\end{axis}
334\end{tikzpicture}
335\end{center}
336
337\subsection{Contour Plot}
338
339\begin{center}
340\begin{tikzpicture}
341\begin{axis}[
342    width=10cm,
343    height=10cm,
344    xlabel={$x$},
345    ylabel={$y$},
346    title={Contour Plot: $z = x^2 + y^2$},
347    view={0}{90},
348    colormap/hot,
349    colorbar
350]
351    \addplot3[
352        contour gnuplot={number=15},
353        samples=50,
354        domain=-2:2
355    ]
356    {x^2 + y^2};
357\end{axis}
358\end{tikzpicture}
359\end{center}
360
361\section{Compilation Notes}
362
363To compile this document:
364\begin{verbatim}
365pdflatex --shell-escape pgfplots_demo.tex
366\end{verbatim}
367
368The \texttt{--shell-escape} flag is required for certain plot types (like contour plots)
369that use external programs.
370
371\end{document}