⬆️ ⬇️

How to make graphics in LaTeX

Very often, one or another chart should be inserted into the document. Today, there are many tools that allow you to do this with the ability to insert into the LaTeX document among them Gnuplot, Matplotlib. In this post I would like to highlight another way to create graphics using the pgfplots package. This package is an add-on / add-on to the Tikz package (PGF).







So, we will assume that the packages you have already installed. To connect the packages you need to insert the following code into the preamble.

')

\usepackage{tikz}

\usepackage{pgfplots}





The chart code is placed in the special environment:



\begin{tikzpicture}

...

\end{tikzpicture}





As the first graph, it is proposed to create a graph using known points. Suppose we have a small table of data that needs to be displayed, the code for the graph in this case will have the form.



\begin{tikzpicture}

\begin{axis}

\addplot coordinates {

( 338.1, 266.45 )

( 169.1, 143.43 )

( 84.5, 64.80 )

( 42.3, 34.19 )

( 21.1, 9.47 )};

\end{axis}

\end{tikzpicture}





Visually, it will look like this.



image



As you can see in the graph, we have both lines and markers. If we need something one, then we modify \ begin {axis} to the form: \ begin {axis} [only marks], or \ begin {axis} [mark = none].



We now consider the case when we need to place two graphs on the same field. Let's add the first example with the interpolation graph of the available data.



\begin{tikzpicture}

\begin{axis}

\addplot[only marks] coordinates {

( 338.1, 266.45 )

( 169.1, 143.43 )

( 84.5, 64.80 )

( 42.3, 34.19 )

( 21.1, 9.47 )};

\addplot[mark = none] coordinates {( 350, 279 )

( 0, 0 )};

\end{axis}

\end{tikzpicture}





image



I draw attention to the fact that the presence / absence of markers is not set by the grid, but by a separate graphic.



Let us now consider the case when it is necessary to build a graph not by 10-20 points, but by a significantly larger number. Suppose there is a file of the following structure



tx

0.000000 -0.000000

0.001000 20.326909

0.002000 19.856433

0.003000 8.330376

0.004000 13.982180

0.005000 17.589183

0.006000 19.611675





Then we can easily build the dependence x (t) with the following code:



\begin{tikzpicture}

\begin{axis}[ no markers,

xmin=0, xmax=0.3 ]

\addplot table[x=t,y=x] {foo.dat};

\end{axis}

\end{tikzpicture}





image



In this example, we used two more variables xmin, xmax. It is easy to guess using these variables, we set the boundaries of the coordinate grid. There are also commands ymin and ymax.



In conclusion, I would like to describe the mechanism for creating signatures and legends. Signatures are created by the ylabel and xlabel commands. After them there is a signature in which there can be mathematical symbols. Legends are created via the \ addlegendentry {Some-text} commands after the graphics command.



\begin{tikzpicture}

\begin{axis}[

ylabel=Data,

xlabel=time ]

\addplot[only marks] coordinates {

( 338.1, 266.45 )

( 169.1, 143.43 )

( 84.5, 64.80 )

( 42.3, 34.19 )

( 21.1, 9.47 )};

\addlegendentry{Dots}

\addplot[mark = none] coordinates {( 350, 279 )

( 0, 0 )};

\addlegendentry{Line}

\end{axis}

\end{tikzpicture}





image



In the package there are still many different settings, chips. You can read about them in the official documentation .

Source: https://habr.com/ru/post/94931/



All Articles