What is the best way to plot coordinates in Latex?
Data points can be directly entered using the data
command from the datavisualization
library. The same applies if you have the function instead of the data points. Here are some examples adapted from the TikZ-PGF manual:
\documentclass[border=2mm,tikz]{standalone}
\usetikzlibrary{datavisualization}
\begin{document}
\begin{tikzpicture}
\datavisualization [school book axes, visualize as smooth line]
data {
x, y
-1.5, 2.25
-1, 1
-.5, .25
0, 0
.5, .25
1, 1
1.5, 2.25
};
\end{tikzpicture}
\end{document}
Changing the axes style to scientific, e.g., can give a different look:
\datavisualization [scientific axes, all axes={grid}, visualize as smooth line]
Also, if you have the function at hand, this can be quite easy:
\documentclass[border=2mm,tikz]{standalone}
\usetikzlibrary{datavisualization.formats.functions}
\begin{document}
\begin{tikzpicture}
\datavisualization [school book axes, visualize as smooth line]
data [format=function] {
var x : interval [-1.5:1.5] samples 7;
func y = \value x*\value x;
};
\end{tikzpicture}
\end{document}
with the same result as the data points entry:
Here is an example using pgfplots
. The data can also be read from a csv
file or can be computed using formulas.
\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot coordinates {
(1, 1)
(2, 4)
(3, 9)
(4, 16)
(5, 25)
};
\end{axis}
\end{tikzpicture}
\end{document}
Here a small example created with PSTricks
rather with TikZ
. Still I do not understand why many users are already leaving this opportunity to build graphics with PSTricks
.
\documentclass[pstricks,border=12pt,12pt]{standalone}
\usepackage{pst-plot}
\usepackage{pst-eucl}
\psset{algebraic,arrows=->}
\def\f{x^2}
\begin{document}
\begin{pspicture}(-5,-3)(5,5)
\psaxes[linewidth=.5pt,linecolor=darkgray](0,0)(-4,-1)(3,4)[$x$,0][$y$,90]
\psplot[linecolor=magenta]{-5}{5}{\f}
\pstGeonode[PosAngle=-30,PointNameSep=15pt,PointName={{\scriptstyle(0,.5)},{\scriptstyle(1,f(2))}}]
(0,0.5){F}(*1 {\f(x)}){A}
\end{pspicture}
\end{document}