how to draw a graph in latex
Plotting functions and data in LaTeX is quiet piece of cake and this is possible thanks to the TikZ and Pgfplots packages. In this tutorial, we volition learn how to plot functions from a mathematical expression or from a given data. Moreover, we will learn how to create axes, add labels and fable and alter the graph style.
Let us consider the post-obit function:
where we would like to get something similar to the side by side figure together with a data plotted from a given file.
Let's start by setting upward the tikzpicture environment.
Create a TikZpicture environment
First of all, we demand to set upward the project by loading the Pgfplots package in the preamble. Information technology should be noted that when you write \usepackage{pgfplots} some code in the pgfplots loads the TikZ package.
\documentclass{standalone} \usepackage{pgfplots} \pgfplotsset{compat = newest} \begin{document} \begin{tikzpicture} \brainstorm{centrality}[] % here comes the lawmaking \cease{axis} \end{tikzpicture} \end{document}
Compiling the in a higher place lawmaking yields to the post-obit analogy:
It is important to apply the command \pgfplotsset{compat = newest} to specify to the compiler that we are working with the last version of the Pgfplots package.
In order to showtime plot functions and information in TikZ nosotros need to create the axis environment within the tikzpicture environment. All the pgfplots commands must be inside the axis environment.
Plot a function in LaTeX
To plot a function, we but need to use the control \addplot[options]{ewpression}. Check the following code to figure out how this control should exist used for the above part.
\documentclass{standalone} \usepackage{pgfplots} \pgfplotsset{compat = newest} \begin{document} \brainstorm{tikzpicture} \begin{centrality}[] \addplot[] {exp(-x/ten)*( cos(deg(x)) + sin(deg(x))/ten )}; \cease{centrality} \end{tikzpicture} \end{certificate}
Compiling the above lawmaking yields:
The domain and range of the plot is auto determinate past the compiler. And then if we want to modify the limits of the plot nosotros demand to specify information technology manually in the axis surround. For this purpose nosotros can use these options:
For this example let be xmin=0.0, xmax=30, ymin=-one.v and ymax=2.0. Of course y'all can change these values depending on the domain and range of the function. Here is a modified version of the in a higher place code:
\documentclass{standalone} \usepackage{pgfplots} \pgfplotsset{compat = newest} \begin{certificate} \brainstorm{tikzpicture} \begin{axis}[ xmin = 0, xmax = 30, ymin = -1.5, ymax = 2.0] \addplot[ domain = 0:30, ] {exp(-ten/10)*( cos(deg(x)) + sin(deg(x))/10 )}; \finish{axis} \end{tikzpicture} \terminate{certificate}
Don't forget to specify the domain of the role with the selection domain = a:b. In this case we ready this parameter to domain = 0:xxx. The domain of the function is independent of the limits of the axes, but normally it takes the same values to get a plot that fills the axis.
How to make the plot smooth in LaTeX
The previous figure has a rough plot and to get a shine one, we can employ the following options:
The obtained illustration is shown below (smooth plot). We have changed the stroke and color of the function by providing the options thick and blue to the \addplot command. You tin can try dissimilar strokes: ultra thin, very thin, sparse, semithick, thick, very thick, ultra thick and line width=<value>.
\documentclass{standalone} \usepackage{pgfplots} \pgfplotsset{compat = newest} \brainstorm{document} \begin{tikzpicture} \brainstorm{centrality}[ xmin = 0, xmax = 30, ymin = -one.five, ymax = 2.0] \addplot[ domain = 0:xxx, samples = 200, shine, thick, blueish, ] {exp(-ten/10)*( cos(deg(x)) + sin(deg(x))/10 )}; \end{axis} \end{tikzpicture} \end{document}
Compiling this code yields:
How to modify figure size in LaTeX
The side by side footstep is to set up the grid and the attribute ratio of the effigy. This can be accomplished past using these options:
Here is a slice of code that uses the above parameters:
\documentclass{standalone} \usepackage{pgfplots} \pgfplotsset{compat = newest} \begin{document} \begin{tikzpicture} \begin{centrality}[ xmin = 0, xmax = 30, ymin = -1.v, ymax = ii.0, xtick distance = two.5, ytick distance = 0.5, grid = both, small tick num = one, major grid style = {lightgray}, minor grid style = {lightgray!25}, width = \textwidth, tiptop = 0.five\textwidth] \addplot[ domain = 0:30, samples = 200, smooth, thick, blue, ] {exp(-10/10)*( cos(deg(x)) + sin(deg(x))/x )}; \finish{centrality} \cease{tikzpicture} \end{document}
How to plot data from a file in LaTeX
Now suppose you don't have the expression of the function y'all want to plot, only instead you lot have a file, for example cosine.dat with the coordinates as shown below. Here we use a space as separator of the coordinates, simply likewise you tin can use a comma, only specifies the separator using the option col sep = comma in the \addplot command.
x y 0.0 1.0000 0.v 0.8776 1.0 0.5403 i.5 0.0707 2.0 -0.4161 2.5 -0.8011 3.0 -0.9900 3.5 -0.9365 four.0 -0.6536 iv.5 -0.2108 five.0 0.2837 5.v 0.7087 6.0 0.9602 half dozen.five 0.9766 7.0 0.7539 7.5 0.3466 ... % data file
Plotting a data files is very similar to plot a mathematical expression, we utilize again the \addplot command but in slightly different way:
\addplot[options] file[options] {file_name.dat}
The next lawmaking shows the implementation of this sentence for plot the previous data file:
\documentclass{standalone} \usepackage{pgfplots} \pgfplotsset{compat = newest} \begin{document} \begin{tikzpicture} \begin{centrality}[ xmin = 0, xmax = 30, ymin = -1.5, ymax = ii.0, xtick distance = 2.5, ytick distance = 0.5, grid = both, minor tick num = 1, major grid style = {lightgray}, minor grid mode = {lightgray!25}, width = \textwidth, height = 0.5\textwidth, xlabel = {$x$}, ylabel = {$y$},] % Plot a function \addplot[ domain = 0:xxx, samples = 200, smooth, thick, blue, ] {exp(-x/x)*( cos(deg(x)) + sin(deg(x))/10 )}; % Plot data from a file \addplot[ smooth, thin, red, dashed ] file[skip start] {cosine.dat}; \terminate{axis} \end{tikzpicture} \cease{certificate}
At present we have the file plotted, which in this case represents the cosine function, simply you lot can plot any prepare of coordinates. In options, nosotros have added dashed style for the curve and we have painted it with carmine colour. Also discover that we have used the selection skip first since the data file has labels instead of numbers in their get-go line.
Nosotros are virtually done, nosotros just demand to add a legend to the graphic. This can be done simply by adding the post-obit line code:
\legend{Plot from expression, Plot from file}
after the concluding \addplot line code. Past doing so, we will get the effigy in question.
Plot data from a multicolumn file in LaTeX
Now suppose you want to plot a data file with several columns, for example these file named multiple_functions.dat, which have multiple columns.
10 y1 y2 y3 0.0 0.0000 0.0000 0.0000 one.0 0.0100 0.0050 0.0025 2.0 0.0400 0.0200 0.0100 3.0 0.0900 0.0450 0.0225 four.0 0.1600 0.0800 0.0400 5.0 0.2500 0.1250 0.0625 6.0 0.3600 0.1800 0.0900 7.0 0.4900 0.2450 0.1225 viii.0 0.6400 0.3200 0.1600 ix.0 0.8100 0.4050 0.2025 x.0 1.0000 0.5000 0.2500 % data file
In this information file we can run into 4 columns, the first 1 is the coordinates of the x-axis and the other three columns corresponds to the y-axis values for each x-value. It means we need to plot three functions from a single data file.
This can be accomplished hands past using the control:
\pgfplotstableread{file.dat}{\tabular array}
This command reads a file and saves information technology every bit a table where y'all tin access the columns one time by once.
The next code shows the implementation of the \pgfplotstableread command in order to plot the information file:
\documentclass{standalone} \usepackage{pgfplots} \pgfplotsset{compat = newest} \begin{document} \pgfplotstableread{multiple_functions.dat}{\table} \brainstorm{tikzpicture} \begin{axis}[ xmin = 0, xmax = 10, ymin = 0, ymax = ane, xtick distance = 1, ytick distance = 0.25, grid = both, minor tick num = 1, major filigree style = {lightgray}, minor filigree style = {lightgray!25}, width = \textwidth, elevation = 0.75\textwidth, legend cell align = {left}, fable pos = northward westward ] \addplot[blue, mark = *] table [x = {x}, y = {y1}] {\tabular array}; \addplot[red, simply marks] tabular array [x ={x}, y = {y2}] {\table}; \addplot[teal, only marks, marker = x, mark size = 3pt] table [ten = {x}, y = {y3}] {\table}; \fable{ Plot with marks and line, Plot only with marks, Plot with other type of marks } \stop{axis} \terminate{tikzpicture} \cease{document}
Compiling the code yields:
To plot a specific cavalcade from a data file, we can use \addplot along with the \table control, as follows:
\addplot[options] tabular array [x = {column_x}, y = {column_y}] {\table};
With the table option you tin specify the name of the column you desire to exist in the ten-axis and the name of the column you desire to be in the y-axis.
To become unlike plot style, i with marks and lines, and two only with marks, we used the following options:
This can be useful when you take to plot several graphics on the same figure.
With the method described in this tutorial, you are able to describe any explicit functional equation or whatsoever set of coordinates. The Pgfplots parcel allows to plot whatever number of functions in the aforementioned figure. At present it'south upward to you how to apply this tools to create your own graphics.
At this level, nosotros reached the terminate of this tutorial. If you have any remarks or suggestions, I will be happy to hear from you .
Source: https://latexdraw.com/plot-a-function-and-data-in-latex/
Posted by: foltzchai1944.blogspot.com
0 Response to "how to draw a graph in latex"
Post a Comment