27

I need to plot some points in a graph; just a simple list of x and y values and then connect them with line segments. Currently, I am graphing it in excel and importing the image into latex and it doesn't look good. Is there any way to plot coordinates just using latex?

4
  • 1
    There's pgfplots
    – cgnieder
    Feb 1, 2017 at 18:32
  • 1
    Or you can use PSTricks. For my opinion is very easy to utilize.
    – Sebastiano
    Feb 1, 2017 at 18:38
  • 3
    It depends on: if you prefer the classical LaTeX and compile a DVI-file, PStricks is suited better. If you prefer the modern way and run pdfLaTeX or similar compilers, I advise to use TikZ and PGF. PSTricks is based on PostScript, which works well with DVI, but not with PDF. On the other hand, TikZ and PGF work well with PDF but not DVI and PostScript. If you are an absolute beginner, you won't care, if you have to learn the one or the other from scratch. Therefore, my suggestion would be to learn pgfplots.
    – Jan
    Feb 1, 2017 at 19:04
  • You may want to have a lookt at this example: d.xav.free.fr/tikz
    – DRi
    Feb 14, 2017 at 11:43

4 Answers 4

30

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}

enter image description here

Changing the axes style to scientific, e.g., can give a different look:

\datavisualization [scientific axes, all axes={grid}, visualize as smooth line]

enter image description here

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:

enter image description here

26

Here is an example using pgfplots. The data can also be read from a csv file or can be computed using formulas.

enter image description here

\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}
7
  • You can actually read any txt file, even some generated by laboratory equipment, isn't?
    – G. Bay
    Feb 7, 2017 at 21:35
  • 2
    @G.Bay It depends what you mean by "any" text file. The data must be structured: one data set per line, the values separated by a space or some other unique character. But this is usually how data is made available.
    – gernot
    Feb 7, 2017 at 22:41
  • @gernot is it possible to show how to put two such plots side by side? Sep 7, 2020 at 17:12
  • @AlexanderCska What exactly do you mean by side by side? Have you tried the code above, but doubling the tikzpicture, i.e., using \begin{tikzpicture}....\end{tikzpicture}\begin{tikzpicture}....\end{tikzpicture}? There are many other ways of arranging plots, e.g. by using them as entries of a tabular environment or as subfigures (if you want to have captions and labels). If this comment doesn't help, please open a new question with a description of what you want to achieve,
    – gernot
    Sep 7, 2020 at 17:52
  • 1
    @alper I'm pretty sure the answer is yes. What type of legend are you thinking of? See e.g. Put legend below the plot. If you need a different legend, please post a question with more details.
    – gernot
    Oct 1, 2023 at 9:58
9

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.

enter image description here

\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}
4

If you know some about R, knitr is a good option:

mwe

\documentclass{article}
\begin{document}
<<echo=F, fig.cap="One simple plot.", dev='tikz'>>=
df <- data.frame(x = c(1,2,3,4,5,6,7), y= c(1,2,4,8,16,32,64))
plot(df, pch=16, col="red")
lines(df$x,df$y, col="red", lwd=3)
@
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .