Sage Planar Graph Drawing Stackexchange
Contents
- 1 Introduction
- 1.1 The document preamble
- 1.ii Compilation time (brief background)
- 1.iii Reducing compilation time
- 2 Basic example (too externalizing the figures)
- 2.1 Explanation of the code
- three second plots
- iii.1 Plotting mathematical expressions
- 3.1.1 Explanation of the lawmaking
- 3.ii Plotting from data
- 3.two.1 Explanation of the code
- 3.3 Scatter plots
- 3.3.1 Explanation of the code
- 3.4 Bar graphs
- three.four.ane Explanation of the lawmaking
- iii.1 Plotting mathematical expressions
- four 3D Plots
- four.one Plotting mathematical expressions
- 4.1.i Explanation of the lawmaking
- 4.ii Contour plots
- 4.2.1 Explanation of the code
- four.3 Plotting a surface from information
- iv.iii.1 Explanation of the data
- 4.4 Parametric plot
- iv.4.ane Explanation of the code
- four.one Plotting mathematical expressions
- 5 Reference guide
- 6 Further reading
Introduction
The pgfplots
package, which is based on TikZ
, is a powerful visualization tool and ideal for creating scientific/technical graphics. The bones idea is that you provide the input data/formula and pgfplots
does the rest.
The document preamble
To use the pgfplots
package in your document add following line to your preamble:
\usepackage{pgfplots}
You also tin can configure the behaviour of pgfplots
in the document preamble. For case, to alter the size of each plot and guarantee backwards compatibility (recommended) add together the next line:
\pgfplotsset{width=10cm,compat=1.ix}
This changes the size of each pgfplot
figure to 10 centimeters, which is huge; you may use different units (pt, mm, in). The compat parameter is for the code to work on the package version i.ix or later.
Compilation time (brief background)
When the original TeX engine was conceived/written, more forty years ago, it was non designed for straight production of graphics—those were to be files created by external programs (e.1000., MetaPost) and imported into the typeset document. The advent of pdfTeX—which is closely based on the original TeX software—brought the ability to create graphics directly past using pdfTeX's new born TeX linguistic communication commands (called primitives) which tin can output the PDF operators/data required to produce graphics. The creation of pdfTeX led to the development of sophisticated LaTeX graphics packages, such as TikZ
, pgfplots
etc, capable of producing graphics coded using high-level LaTeX commands.
All the same, backside the scenes, and deep inside the pdfTeX engine (and other engines), those high-level LaTeX graphics commands need to be candy past "converting" them back into depression-level pdfTeX engine (primitive) commands which actually generate (output) the PDF operators required to produce the resultant figure(due south). That processing of graphical LaTeX commands—expansion and execution of primitives—tin take a non-negligible amount of time. Fifty-fifty a single high-level LaTeX graphics command, together with its corresponding data, might crave repeated execution of many low-level TeX engine (primitive) commands. From an end-user's perspective, documents containing multiple pgfplots
figures, and/or very complex graphics, can accept a considerable amount of time to return (compile).
Reducing compilation time
To increment speed of certificate-compilation you can configure the pgfplots
package to export the figures to separate PDF files and then import them into the document: compile once, then re-use the figures. To do that, add the code shown beneath to the preamble:
\usepgfplotslibrary{external} \tikzexternalize
See this help commodity for farther details on how to ready up tikz-externalization in your Overleaf project.
Bones example (besides externalizing the figures)
\documentclass {article} \usepackage [margin=0.25in] {geometry} \usepackage {pgfplots} \pgfplotsset {width=10cm,compat=1.ix} % We volition externalize the figures \usepgfplotslibrary {external} \tikzexternalize \begin {document} Starting time case is 2nd and 3D math expressions plotted side-by-side. %Here begins the 2d plot \begin {tikzpicture} \begin {axis} \addplot [color=red] {exp(10)}; \end {axis} \end {tikzpicture} %Here ends the second plot \hskip 5pt %Here begins the 3D plot \brainstorm {tikzpicture} \begin {axis} \addplot3[ surf, ] {exp(-10^2-y^two)*10}; \end {axis} \end {tikzpicture} %Here ends the 3D plot \end {certificate}
Open this pgfplots
example in Overleaf.
The following image shows the effect produced by the lawmaking above:
Caption of the code
Because pgfplots
is based on tikz
the plot must exist inside a tikzpicture
environment. Then the environs annunciation \begin{axis}
, \end{axis}
will set the right scaling for the plot—check the Reference guide for other centrality environments.
To add together an bodily plot, the control \addplot[color=red]{log(10)};
is used. Inside the foursquare brackets, [...]
, some options can be passed in; here, we prepare the color
of the plot to red
. The foursquare brackets are mandatory, if no options are passed go out a bare space betwixt them. Inside the curly brackets you put the office to plot. Is important to retrieve that this command must end with a semicolon (;).
To put a second plot side by side to the get-go 1 declare a new tikzpicture
environment. Do not insert a new line, but a small blank gap, in this case hskip 10pt
will insert a 10pt-wide blank space.
The rest of the syntax is the aforementioned, except for the \addplot3 [surf,]{exp(-x^ii-y^2)*ten};
. This volition add a 3dplot, and the option surf
within squared brackets declares that it's a surface plot. The function to plot must be placed inside curly brackets. Once more, don't forget to put a semicolon (;) at the end of the command.
Note: Information technology's recommended as a good practise to indent the lawmaking—meet the 2nd plot in the example in a higher place—and to add a comma (,) at the terminate of each option passed to \addplot
. This manner the code is more readable and is easier to add further options if needed.
2D plots
pgfplots
' 2D plotting functionalities are vast—and you can personalize your plots to suit your requirements. All the same, the default options usually give very good results, so all you need to do is feed the data and LaTeX will do the rest.
Plotting mathematical expressions
Hither is an instance:
\begin {tikzpicture} \brainstorm {axis}[ axis lines = left, xlabel = \( x \), ylabel = { \( f ( x ) \) }, ] %Below the cherry parabola is defined \addplot [ domain=-10:10, samples=100, color=red, ] {ten^2 - 2*x - i}; \addlegendentry { \( x^ 2 - 2 ten - one \) } %Here the blue parabola is divers \addplot [ domain=-x:10, samples=100, colour=blue, ] {x^ii + 2*x + 1}; \addlegendentry { \( x^ 2 + 2 ten + 1 \) } \end {centrality} \cease {tikzpicture}
Open this pgfplots
example in Overleaf.
The output from this code is shown in the image below—the LaTeX document preamble is added automatically when y'all open the link:
Explanation of the lawmaking
Let's analyse the new commands line-by-line:
-
axis lines = left
. - This will ready the axis simply on the left and bottom sides of the plot, instead of the default
box
. Further customisation options at the reference guide.
-
xlabel = \(10\)
andylabel = {\(f(10)\)}
. - Self-explanatory parameter names, these will let y'all put a label on the horizontal and vertical centrality. Notice the
ylabel
value in between curly brackets, this brackets tellpgfplots
how to group the text. Thexlabel
could have had brackets as well. This is useful for complicated labels that may confusepgfplots
.
-
\addplot
. - This will add a plot to the axis, full general usage was described at the introduction. There are two new parameters in this case.
-
domain=-10:10
. - This establishes the range of values of \(10\).
-
samples=100
. - Determines the number of points in the interval defined by
domain
. The greater the value ofsamples
the sharper the graph y'all get, just it volition take longer to render.
-
\addlegendentry{\(x^ii - 2x - 1\)}
. - This adds the legend to identify the function \(x^2 - 2x - 1\).
To add together some other graph to the plot just write a new \addplot
entry.
Plotting from data
Scientific research often yields data that has to be analysed. The next example shows how to plot data with pgfplots:
Plotting from data: \begin {tikzpicture} \begin {axis}[ title={Temperature dependence of CuSO\( _ 4 \cdot \)5H\( _ ii \)O solubility}, xlabel={Temperature [\textcelsius]}, ylabel={Solubility [g per 100 one thousand water]}, xmin=0, xmax=100, ymin=0, ymax=120, xtick={0,xx,twoscore,60,80,100}, ytick={0,xx,40,60,eighty,100,120}, fable pos=north w, ymajorgrids=true, grid mode=dashed, ] \addplot[ color=blue, mark=square, ] coordinates { (0,23.1)(10,27.5)(twenty,32)(30,37.8)(40,44.6)(60,61.8)(lxxx,83.eight)(100,114) }; \legend {CuSO\( _ 4 \cdot \)5H\( _ 2 \)O} \end {axis} \end {tikzpicture}
Open this pgfplots
instance in Overleaf.
The output from this code is shown in the paradigm beneath—the LaTeX document preamble is added automatically when you open the link:
Explanation of the lawmaking
There are some new commands and parameters here:
-
title={Temperature dependence of CuSO\(_4\cdot\)5H\(_2\)O solubility}
. - As y'all might expect, assigns a title to the figure. The title will be displayed above the plot.
-
xmin=0, xmax=100, ymin=0, ymax=120
. - Minimum and maximum premises of the x and y axes.
-
xtick={0,xx,40,60,80,100}, ytick={0,20,40,lx,80,100,120}
. - Points where the marks are placed. If empty the ticks are ready automatically.
-
legend pos=north w
. - Position of the fable box. Check the reference guide for more options.
-
ymajorgrids=true
. - This Enables/disables filigree lines at the tick positions on the y axis. Use
xmajorgrids
to enable grid lines on the ten axis.
-
filigree style=dashed
. - Cocky-explanatory. To display dashed grid lines.
-
mark=foursquare
. - This draws a squared mark at each point in the
coordinates
array. Each mark will be continued with the next one by a straight line.
-
coordinates {(0,23.i)(10,27.five)(xx,32)...}
- Coordinates of the points to be plotted. This is the data you want analyse graphically.
If the data is in a file, which is the case nearly of the fourth dimension; instead of the commands \addplot
and coordinates
yous should employ \addplot table {file_with_the_data.dat}
, the balance of the options are valid in this environment.
Scatter plots
Scatter plots are used to represent data by using some kind of marks and are normally used when calculating statistical regression. In this instance we'll create a scatter plot using information contained in a file called scattered_example.dat
, in which the data looks similar this:
GPA ma ve co un three.45 643 589 3.76 3.52 2.78 558 512 2.87 2.91 2.52 583 503 2.54 2.4 3.67 685 602 3.83 iii.47 3.24 592 538 iii.29 3.47 2.1 562 486 2.64 ii.37 ...
Our scatter plot uses the first two columns of the data:
\begin {tikzpicture} \begin {centrality}[ enlargelimits=imitation, ] \addplot+[ only marks, scatter, mark=halfcircle*, mark size=two.9pt] table[meta=ma] {scattered_example.dat}; \end {axis} \terminate {tikzpicture}
Open a scatter plot project instance in Overleaf (contains the data file scattered_example.dat
).
Explanation of the code
The parameters passed to the centrality
and addplot
environments tin also be used in a data plot, except for scatter
. Below the description of the code:
-
overstate limits=false
- This will shrink the axes so the bespeak with maximum and minimum values lay on the edge of the plot.
-
simply marks
- Really explicit, will put a marking on each betoken.
-
scatter
- When
scatter
is used the points are coloured depending on a value, the colour is given by themeta
parameter explained below.
-
mark=halfcircle*
- The kind of marking to use on each signal, cheque the reference guide for a list of possible values.
-
marking size=two.9pt
- The size of each mark, dissimilar units can be used.
-
table[meta=ma]{scattered_example.dat};
- Here the
tabular array
command tells latex that the data to be plotted is in a file. Themeta=ma
parameter is passed to choose the cavalcade that determines the colour of each point. Inside curly brackets is the name of the data file.
Bar graphs
Bar graphs (besides known as bar charts and bar plots) are used to display gathered data, mainly statistical data virtually a population of some sort. Bar plots in pgfplots
are highly configurable, but here we are going to show a evidently case:
\begin {tikzpicture} \begin {centrality}[ ten tick characterization fashion={ /pgf/number format/1000 sep=}, ylabel=Year, enlargelimits=0.05, legend mode={at={(0.v,-0.one)}, anchor=north,legend columns=-1}, ybar interval=0.7, ] \addplot coordinates {(2012,408184) (2011,408348) (2010,414870) (2009,412156)}; \addplot coordinates {(2012,388950) (2011,393007) (2010,398449) (2009,395972)}; \legend {Men,Women} \end {axis} \end {tikzpicture}
Open this pgfplots
bar code example in Overleaf.
The output from this code is shown in the prototype below—the LaTeX document preamble is added automatically when you open up the link:
Explanation of the code
The figure starts with the (previously explained) proclamation of the tikzpicture
and axis
environments, merely the axis
declaration has a number of new parameters:
-
ten tick label style={/pgf/number format/1000 sep=}
- This piece of code defines a complete manner for the plot. With this style you lot may include several
\addplot
commands inside thisaxis
environment, they will fit and look nice together with no further tweaks (theybar
parameter described below is mandatory for this to work).
-
enlargelimits=0.05
. - Enlarging the limits in a bar plot is necessary because these kind of plots ofttimes require some actress space to a higher place the bar to look better and/or add together a label. Then number 0.05 is relative to the full summit of of the plot.
-
legend fashion={at={(0.5,-0.2)}, anchor=north,legend columns=-i}
- Once again, this will work but fine most of the fourth dimension. If anything, change the value of
-0.2
to locate the legend closer/farther from the x-axis.
-
ybar interval=0.seven,
- Thickness of each bar.
1
meaning the confined will be one adjacent to the other with no gaps and0
meaning there volition be no bars, only but vertical lines.
The coordinates
in this kind of plot make up one's mind the base bespeak of the bar and its height.
The labels on the y-centrality volition show upwards to 4 digits. If the numbers y'all are working with are greater than 9999 pgfplots
will use the aforementioned note as in the case.
3D Plots
pgfplots
has the 3D Plotting capabilities that you may expect in a plotting software.
Plotting mathematical expressions
At that place's a simple case well-nigh this at the introduction, let's work on something slightly more complex:
\begin {tikzpicture} \brainstorm {axis}[ title=Example using the mesh parameter, hide axis, colormap/cool, ] \addplotthree[ mesh, samples=50, domain=-8:8, ] {sin(deg(sqrt(x^2+y^ii)))/sqrt(10^two+y^2)}; \addlegendentry { \( \frac {sin ( r ) }{r} \) } \end {axis} \end {tikzpicture}
Open up this pgfplots
3D case in Overleaf.
The output from this code is shown in the image below—the LaTeX certificate preamble is added automatically when you open the link:
Caption of the code
Most of the commands here take already been explained, simply there are 3 new things:
-
hide axis
- This selection in the
axis
environs is self descriptive, the axis won't be shown.
-
colormap/cool
- Is the colour scheme to be used in the plot. Check the reference guide for more than colour schemes.
-
mesh
- This choice is self-descriptive also, check besides the
surf
parameter in the introductory example.
Annotation: When working with trigonometric functions pgfplots
uses degrees every bit default units, if the angle is in radians (as in this example) you accept to use the deg
role to convert to degrees.
Profile plots
In pgfplots
it is possible to plot contour plots, only the data has to be pre-calculated past an external program. Allow's see an example:
\begin {tikzpicture} \begin {axis} [ title={Profile plot, view from top}, view={0}{ninety} ] \addplotiii[ profile gnuplot={levels={0.8, 0.four, 0.2, -0.ii}} ] {sin(deg(sqrt(x^two+y^2)))/sqrt(x^2+y^two)}; \end {axis} \finish {tikzpicture}
Open this pgfplots
contour plot instance in Overleaf.
The output from this lawmaking is shown in the image below—the LaTeX document preamble is added automatically when you open the link:
Caption of the code
This is a plot of some contour lines for the same equation used in the previous section. The value of the title
parameter is inside curly brackets because information technology contains a comma, then we utilize the grouping brackets to avert any confusion with the other parameters passed to the \begin{axis}
annunciation. There are two new commands:
-
view={0}{90}
- This changes the view of the plot. The parameter is passed to the
axis
environment, which means this can be used in any other type of 3D plot. The first value is a rotation, in degrees, around the z-axis; the second value is to rotate the view around the x-centrality. In this instance when we combine a 0° rotation around the z-centrality and a xc° rotation effectually the 10-axis nosotros end up with a view of the plot from height.
-
contour gnuplot={levels={0.viii, 0.4, 0.two, -0.two}}
- This line of code does two things: Outset, it tells LaTeX to use the external software
gnuplot
to compute the contour lines; this works fine in Overleaf but if you want to use this command in your local LaTeX installation you take to install gnuplot kickoff (matlab will likewise work, in such case writematlab
instead ofgnuplot
in the command). 2nd, the sub parameterlevels
is a list of values of pinnacle levels where the contour lines are to be computed.
Plotting a surface from data
To plot a set up of information into a 3D surface all nosotros need is the coordinates of each point. These coordinates could be an unordered fix or, in this case, a matrix:
\begin {tikzpicture} \begin {axis} \addplot3[ surf, ] coordinates { (0,0,0) (0,one,0) (0,2,0) (1,0,0) (1,1,0.6) (1,2,0.seven) (two,0,0) (2,i,0.7) (2,two,1.8) }; \end {axis} \cease {tikzpicture}
Open this pgfplots
3D surface example in Overleaf.
The output from this lawmaking is shown in the image beneath—the LaTeX document preamble is added automatically when you open the link:
Explanation of the data
The points passed to the coordinates
parameter are treated equally independent in a 3 × 3 matrix, using a blank line every bit the separator for each matrix row.
All the options for 3D plots in this commodity apply to data surfaces.
Parametric plot
The syntax for parametric plots is slightly different. Permit's see an example:
\begin {tikzpicture} \brainstorm {axis} [ view={60}{30}, ] \addplot3[ domain=0:v*pi, samples = 60, samples y=0, ] ({sin(deg(10))}, {cos(deg(x))}, {x}); \end {centrality} \terminate {tikzpicture}
Open this pgfplots
parametric plot example in Overleaf.
The output from this code is shown in the image below—the LaTeX document preamble is added automatically when y'all open the link:
Explanation of the lawmaking
In that location are just two new things in this example: kickoff, the samples y=0
to prevent pgfplots
from joining the farthermost points of the screw and; second, the way the function to plot is passed to the addplot3
environment. Each parameter function is grouped within curly brackets and the three parameters are delimited with a parenthesis.
Reference guide
Command/Pick/Environment | Description | Possible Values |
---|---|---|
axis | Normal plots with linear scaling | |
semilogxaxis | logarithmic scaling of ten and normal scaling for y | |
semilogyaxis | logarithmic scaling for y and normal scaling for x | |
loglogaxis | logarithmic scaling for the x and y axes | |
axis lines | changes the mode the axes are fatigued. default is 'box | box, left, middle, middle, right, none |
legend pos | position of the legend box | south west, s east, north west, north east, outer north east |
mark | blazon of marks used in information plotting. When a single-character is used, the character appearance is very similar to the actual marking. | *, x , +, |, o, asterisk, star, 10-pointed star, oplus, oplus*, otimes, otimes*, square, square*, triangle, triangle*, diamond, halfdiamond*, halfsquare*, correct*, left*, Mercedes star, Mercedes star flipped, halfcircle, halfcircle*, pentagon, pentagon*, cubes. (cubes only work on 3d plots). |
colormap | colour scheme to be used in a plot, can exist personalized just at that place are some predefined colormaps | hot, hot2, jet, blackwhite, bluered, absurd, greenyellow, redyellow, violet. |
Further reading
For more information encounter:
- Using colours in LaTeX
- TikZ bundle
- Externalising pgfplots and tikzpictures
- Inserting Images
- Lists of tables and figures
- Positioning images and tables
- Drawing diagrams straight in LaTeX
- The pgfplots package documentation.
- The TikZ and PGF Packages: Manual for version 3.0.0
- TikZ and PGF examples at TeXample.cyberspace
Source: https://www.overleaf.com/learn/latex/Pgfplots_package
0 Response to "Sage Planar Graph Drawing Stackexchange"
Post a Comment