On our last section on charts and drawing with LaTeX, we are going to
take a look at PGFPlots since, as we have previously stated, it is the
package both TikZ and SmartDiagram are based on.
We won't go in detail with this package as it is really complex and hard
to learn. We will simply give you an example of what you can do with the package
even though the possibilites are infinite if mastered.
PGFPlots
Drawing with PGFPlots
Drawing a 2D plot
Last but not least, we'll attempt to explain and draw step by step a 2D
plot with PGFPlots.
CODE
\documentclass[a4paper]{article}
\title{2D Plot: Step 1}
\usepackage{pgfplots}
\begin{document}
\maketitle
\begin{center}
\begin{tikzpicture}
\begin{axis}[ymin=0, xlabel=Time Spent On Learning Ti\emph{k}Z and PGFPlots, ylabel=My Skills With \LaTeX]
\addplot[color=red, mark=none] {x};
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
Here we have our first plot, as you can already see we've used a
Other than the
The behaviour of PGFPlots is quite similar to that of TikZ. You give a command and then specify the options. As this package includes many, many options, we will only go through the ones we use.
First of all, after the
Finally we drew our plot with the new
tikzpicture
like in TikZ. This is because, as we've already
said, these packages are all connected and they're simply higher or lower level
languages based on the same principles. Other than the
tikzpicture
, we now have an new environment:
the axis
environment. The behaviour of PGFPlots is quite similar to that of TikZ. You give a command and then specify the options. As this package includes many, many options, we will only go through the ones we use.
First of all, after the
axis
environment we specified
the minimum y of our plot with the ymin
option (there also exists a ymax
option and the same goes for x).
Then we labeled our two axes with the xlabel
and ylabel
options. Finally we drew our plot with the new
\addplot
command, which takes
options as well as mathematical functions along with specific coordinates;
we will see that in the following examples.
The options we used so far are color
to choose our color and
mark
to decide the marks along our line, we set it to none
to get a simple line. This is also something we'll see soon.
PGFPlots also allows you to draw more plots on the same axis
environment:
CODE
\documentclass[a4paper]{article}
\title{2D Plot: Step 2}
\date{}
\usepackage{pgfplots}
\begin{document}
\maketitle
\begin{center}
\begin{tikzpicture}
\begin{axis}
[ymin=0, xmin=0, xmax=10, ymax=10, xlabel=Time Spent On Learning Ti\emph{k}Z and PGFPlots, ylabel=My Skills With \LaTeX]
\addplot[color=red, mark=none] coordinates {(0,0) (10,10)};
\addplot[color=green, mark=x]
coordinates {(0,0) (0.2,0.5) (1,0.8) (2,1.5) (3,2.5) (4,2.7) (5,3.2) (6,3.5) (7,3.8) (8,4.2) (9,4.5) (10,5)};
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
You can add as many plots as you want with
We also added the minimum and maximum values x and y as well as changed the first function to a simple line mimicking the behaviour of the function f(x)=x. Now we'll move on to writing legends:
\addplot
.
We didn't do much now, we just changed the mark of our second plot and
we specified its coordinates. We also added the minimum and maximum values x and y as well as changed the first function to a simple line mimicking the behaviour of the function f(x)=x. Now we'll move on to writing legends:
CODE
\documentclass[a4paper]{article}
\title{2D Plot: Step 3}
\date{}
\usepackage{pgfplots}
\begin{document}
\maketitle
\begin{center}
\begin{tikzpicture}[scale=1.5]
\begin{axis}
[ymin=0, xmin=0, xmax=10, ymax=10, xlabel=Time Spent On Learning Ti\emph{k}Z and PGFPlots, ylabel=My Skills With \LaTeX]
\addplot[color=red, mark=none] coordinates {(0,0) (10,10)};
\addplot[color=green, mark=x]
coordinates {(0,0) (0.2,0.5) (1,0.8) (2,1.5) (3,2.5) (4,2.7) (5,3.2) (6,3.5) (7,3.8) (8,4.2) (9,4.5) (10,5)};
\legend {Expected Result, Actual Result}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
Adding a legend was also fairly simple, we have the
We won't cover anymore about the package because it only gets tougher and the aim of this website is to attempt and make things easier (not harder) on newcomers to LaTeX. This was just an overview of the capabilities of the package. Remember that you can give any mathematical function to
But there is much more to be discovered about this package so don't give up if you can't understand much about it at first. Everyone has been there and, ultimately, they got what they wanted from it.
\legend
command which does the job for us. We won't cover anymore about the package because it only gets tougher and the aim of this website is to attempt and make things easier (not harder) on newcomers to LaTeX. This was just an overview of the capabilities of the package. Remember that you can give any mathematical function to
\addplot
.
A straight line was the easiest example and, as you've seen, it quite resembles
TikZ when it comes down to drawing lines. But there is much more to be discovered about this package so don't give up if you can't understand much about it at first. Everyone has been there and, ultimately, they got what they wanted from it.