Example: Pgfplots

Published 2008-01-31 | Author: Christian Feuersänger

Pgfplots provides a friendly and well documented interface for creating plots with normal or logarithmic scaling. Axes, legends and labels are added easily.

Download pgfplots from CTAN.

Author:Christian Feuersänger
Source:Pgfplots manual

Download as: [PDF] [TEX]  •  [Open in writeLaTeX]

Pgfplots

Do you have a question regarding this example, TikZ or LaTeX in general? Just ask in the LaTeX Forum.
Oder frag auf Deutsch auf TeXwelt.de.

% Pgfplots demo
% Author: Christian Feuersänger
% Source: Pgfplots manual:
% http://www.ctan.org/tex-archive/help/Catalogue/entries/pgfplots.html
\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        xlabel=$x$,
        ylabel=$y$]
    \addplot[smooth,mark=*,blue] plot coordinates {
        (0,2)
        (2,3)
        (3,1)
    };
    \addlegendentry{Case 1}

    \addplot[smooth,color=red,mark=x]
        plot coordinates {
            (0,0)
            (1,1)
            (2,1)
            (3,2)
        };
    \addlegendentry{Case 2}
    \end{axis}
    \end{tikzpicture}


\begin{tikzpicture}
    \begin{loglogaxis}[
        xlabel=\textsc{Dof},
        ylabel=$L_2$ Error
    ]
    \axispath\draw
            (7.49165,-10.02171)
        |-  (8.31801,-11.32467)
        node[near start,left] {$\frac{dy}{dx} = -1.58$};
      \addplot plot coordinates {
        (5,     8.312e-02)
        (17,    2.547e-02)
        (49,    7.407e-03)
        (129,   2.102e-03)
        (321,   5.874e-04)
        (769,   1.623e-04)
        (1793,  4.442e-05)
        (4097,  1.207e-05)
        (9217,  3.261e-06)
    };

    \addplot plot coordinates {
        (7,     8.472e-02)
        (31,    3.044e-02)
        (111,   1.022e-02)
        (351,   3.303e-03)
        (1023,  1.039e-03)
        (2815,  3.196e-04)
        (7423,  9.658e-05)
        (18943, 2.873e-05)
        (47103, 8.437e-06)
    };

    \addplot plot coordinates {
        (9, 7.881e-02)
        (49,    3.243e-02)
        (209,   1.232e-02)
        (769,   4.454e-03)
        (2561,  1.551e-03)
        (7937,  5.236e-04)
        (23297, 1.723e-04)
        (65537, 5.545e-05)
        (178177,    1.751e-05)
    };

    \addplot plot coordinates {
        (11,    6.887e-02)
        (71,    3.177e-02)
        (351,   1.341e-02)
        (1471,  5.334e-03)
        (5503,  2.027e-03)
        (18943, 7.415e-04)
        (61183, 2.628e-04)
        (187903,    9.063e-05)
        (553983,    3.053e-05)
    };

    \addplot plot coordinates {
        (13,    5.755e-02)
        (97,    2.925e-02)
        (545,   1.351e-02)
        (2561,  5.842e-03)
        (10625, 2.397e-03)
        (40193, 9.414e-04)
        (141569,    3.564e-04)
        (471041,    1.308e-04)
        (1496065,   4.670e-05)
    };
    \legend{$d=2$\\$d=3$\\$d=4$\\$d=5$\\$d=6$\\}

    \end{loglogaxis}
\end{tikzpicture}
\end{document}

Comments

  • #1 alfC, November 19, 2009 at 10:37 a.m.

    Hi, I read somewhere that the there are 3D features comming soon to pgfplots, is this true?

  • #2 Kjell Magne Fauske, November 19, 2009 at 10:42 a.m.

    Take a look at the change log. There are several entries there related to 3D plotting.

  • #3 Maria, January 18, 2012 at 8:37 p.m.

    Is it possible to put the legend into another corner (eg. left top)?

  • #4 Michael, February 20, 2012 at 12:45 p.m.

    Yes, with

    legend pos=options

    options are: inner / outer

                   north / south / east / west / north east / north west ...
    

    btw.: And another nice feature is the addplot-option [only marks] if no lines are needed.

  • #5 Maria, June 16, 2012 at 2:20 p.m.

    Is it possible to create a legend without the outer frame/box?

  • #6 P.D.P., August 23, 2012 at 5:24 p.m.

    That is possible using

    \begin{axis}[xlabel = $x$, ylabel = $y$, legend style={draw=none}]
    
  • #7 Jakob, October 16, 2012 at 6:51 p.m.

    Is is also possible to draw the legend outside of the diagram? For exemple above in the center, below the title?

  • #8 Anas, March 21, 2013 at 11:13 p.m.

    what if I have big data to be plotted? latex crashes if I put them as mentioned: coordinates{(x1, y1) (x2, y2) (x3, y3) ...}

    how can I take the coordinates from a data file, and how the file should be formated? and does this solve the size issue?

  • #9 M3H0, March 24, 2013 at 8:42 p.m.

    I have a question regarding logarithmic plots. I'm trying to export a 3D plot from matlab into tex through the use of pgfplots and the function "matlab2tikz".

    In this 3D plot, all the axis are logarithmic axis but I cannot manage to import it properly into pgfplots. I know, that I could make a 2D plot with both axis being logarithmic axes by using the "loglogaxis" option. Is there any possibility of making a 3D plot with three logarithmic axis?

  • #10 Christian Feuersänger, April 4, 2013 at 11:28 p.m.

    @Anas plotting big data by means of pgfplots typically requires (a) lots of time to wait while the TeX interpreter ponders on your .tex file and (b) special attention for how to avoid memory limitations. Concerning (b), the best choice might be to call "lualatex " which appears to have dynamic memory limits. If you have any other executable, you may want to enlarge the limits (see the pgfplots manual for a brief howto).

    However, there is some complexity (perhaps around 100x100 points?) where it might make more sense to generate a PNG image by means of some 3rd party tool and import that using \addplot graphics.

  • #11 Christian Feuersänger, April 4, 2013 at 11:30 p.m.

    @M3H0 the predefined environments like "loglogaxis" are actually shortcuts for "\begin{axis}[xmode=log,ymode=log]".

    An answer to your question is: use

    \begin{axis}[xmode=log,ymode=log,zmode=log]

  • #12 Mo, April 10, 2013 at 6:45 p.m.

    Hello, I have a 1,000 line data file with lots of Blocks e.g.

    Block GAUGE Q= 1.00000000E+03 # (SUSY scale) 1 3.61595635E-01 # g1 2 6.36046918E-01 # g2 3 1.03579736E+00 # g3 Block HMIX Q= 1.00000000E+03 # (SUSY scale) 1 1.05981721E+03 # Mu 101 1.13615750E+05 # Bmu 102 9.79933848E+00 # vd 103 2.44983462E+02 # vu 3 2.45179370E+02 # v 10 1.53106614E+00 # betaH 11 3.10113454E+00 # alphaH Block MSOFT Q= 1.00000000E+03 # (SUSY scale) 21 7.29617844E+05 # mHd2 22 -1.54173867E+06 # mHu2 1 4.52555796E+02 # M1 2 7.38027529E+02 # M2 3 2.39102195E+03 # M3 Block PHASES Q= 1.00000000E+03 # (SUSY scale) 1 1.00000000E+00 # pG

    How do you get pgf to plot just one Block of data?

    or one column in one block?

    or just one entry of one column of one block?

    the only guides I see are for simple columns of data but the data files I deal with are more complicated.

    Thanks in advance!

Adding comments is currently not enabled.