如何在 PGFPlots 中绘制散点图

如何在 PGFPlots 中绘制散点图

我尝试根据数据绘制散点图。我从网站上得到了一些提示http://pgfplots.sourceforge.net/gallery.html我的 tex 文件是

\documentclass[a4paper]{amsart}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[finnish]{babel}
\usepackage{url}
\usepackage{parskip} % Tyhjä rivi kappaleiden väliin.
\usepackage{booktabs}
\usepackage{tikz,pgfplots}
\renewcommand*\arraystretch{2}
\listfiles
\begin{document}

\begin{tikzpicture}
\begin{axis}[%
scatter/classes={%
    a={mark=o,draw=black}}]
\addplot[scatter,only marks,%
    scatter src=explicit symbolic]%
table[meta=label] {
x y label
1 4.3 a
2 5.1 a
3 5.7 a
4 6.3 a
5 6.8 a
6 7.1 a
7 7.2 a
8 7.2 a
9 7.2 a
10 7.2 a
11 7.5 a
12 7.8 a
    };
\end{axis}
\end{tikzpicture}
\end{document}

然而,LaTeX 却给出了如下提示:

    ! Package pgfplots Warning: You requested to open table ' x y label 1 4.3 a 2 5
    .1 a 3 5.7 a 4 6.3 a 5 6.8 a 6 7.1 a 7 7.2 a 8 7.2 a 9 7.2 a 10 7.2 a 11 7.5 a 
    12 7.8 a ', but there is also a ' x y label 1 4.3 a 2 5.1 a 3 5.7 a 4 6.3 a 5 6
    .8 a 6 7.1 a 7 7.2 a 8 7.2 a 9 7.2 a 10 7.2 a 11 7.5 a 12 7.8 a .tex'. TeX will
     automatically append the suffix '.tex', so I will now open ' x y label 1 4.3 a
     2 5.1 a 3 5.7 a 4 6.3 a 5 6.8 a 6 7.1 a 7 7.2 a 8 7.2 a 9 7.2 a 10 7.2 a 11 7.
    5 a 12 7.8 a .tex'. Please make sure you don't accidentally load TeX files - th
    is may produce unrecoverable errors.
    ! Extra \or.
    <argument> ...dcsname \end \pgfplotstable@EOI \or 
                                                      \let \pgfplotstableread@im...
    l.33     };

    ? x

日志文件显示:

File: tikzlibrarytopaths.code.tex 2008/06/17 v2.10 (rcs-revision 1.2)
))) (/usr/share/texmf-texlive/tex/latex/pgfplots/pgfplots.sty
Package: pgfplots 2009/02/14 Version 1.2.2

(/usr/share/texmf-texlive/tex/generic/pgfplots/pgfplots.code.tex
(/usr/share/texmf-texlive/tex/generic/pgfplots/pgfplotscore.code.tex
\t@pgfplots@toka=\toks37
\t@pgfplots@tokb=\toks38
\t@pgfplots@tokc=\toks39
\pgfplots@tmpa=\dimen173
)
(/usr/share/texmf-texlive/tex/generic/pgfplots/oldpgfcompatib/pgfplotsoldpgfsup
p_loader.code.tex
Package pgfplots: loading complementary code for your PGF version...

(/usr/share/texmf-texlive/tex/generic/pgfplots/oldpgfcompatib/pgfplotsoldpgfsup
p_misc.code.tex)
(/usr/share/texmf-texlive/tex/generic/pgfplots/oldpgfcompatib/pgfplotsoldpgfsup
p_pgfkeysfiltered.code.tex
\pgfkeys@tmptoks=\toks40
)
(/usr/share/texmf-texlive/tex/generic/pgfplots/oldpgfcompatib/pgfplotsoldpgfsup
p_pgfmathfloat.code.tex
\c@pgfmathroundto@lastzeros=\count124
)
(/usr/share/texmf-texlive/tex/generic/pgfplots/oldpgfcompatib/pgfplotsoldpgfsup
p_pgflibraryplothandlers.code.tex
File: pgfplotsoldpgfsupp_pgflibraryplothandlers.code.tex 2009/02/13 v2.10 (rcs-
revision 1.7)
\pgf@plot@mark@count=\count125
\pgfplotmarksize=\dimen174
)
(/usr/share/texmf/tex/generic/pgf/libraries/pgflibraryfpu.code.tex))
(/usr/share/texmf-texlive/tex/generic/pgfplots/util/pgfplotsutil.code.tex
(/usr/share/texmf-texlive/tex/generic/pgfplots/liststructure/pgfplotsliststruct
ure.code.tex)
(/usr/share/texmf-texlive/tex/generic/pgfplots/liststructure/pgfplotsliststruct
ureext.code.tex)
(/usr/share/texmf-texlive/tex/generic/pgfplots/liststructure/pgfplotsarray.code
.tex
\c@pgfplotsarray@tmp=\count126
)
(/usr/share/texmf-texlive/tex/generic/pgfplots/numtable/pgfplotstable.code.tex
\c@pgfplotstable@counta=\count127
\pgfplotstable@outfile=\write4

(/usr/share/texmf-texlive/tex/generic/pgfplots/numtable/pgfplotstable.coltype.c
ode.tex)))
(/usr/share/texmf-texlive/tex/generic/pgfplots/util/pgfplotscolormap.code.tex)
(/usr/share/texmf-texlive/tex/generic/pgfplots/pgfplots.stackedplots.code.tex)
(/usr/share/texmf-texlive/tex/generic/pgfplots/pgfplotscoordprocessing.code.tex
) (/usr/share/texmf-texlive/tex/generic/pgfplots/pgfplotsticks.code.tex)
(/usr/share/texmf/tex/generic/pgf/frontendlayer/tikz/libraries/tikzlibrarydecor
ations.code.tex
(/usr/share/texmf/tex/generic/pgf/modules/pgfmoduledecorations.code.tex
\pgfdecoratedcompleteddistance=\dimen175
\pgfdecoratedremainingdistance=\dimen176
\pgfdecoratedinputsegmentcompleteddistance=\dimen177
\pgfdecoratedinputsegmentremainingdistance=\dimen178
\pgf@decorate@distancetomove=\dimen179
\pgf@decorate@repeatstate=\count128
\pgfdecorationsegmentamplitude=\dimen180
\pgfdecorationsegmentlength=\dimen181
)
\tikz@lib@dec@box=\box38
)
(/usr/share/texmf/tex/generic/pgf/frontendlayer/tikz/libraries/tikzlibrarydecor
ations.pathmorphing.code.tex
(/usr/share/texmf/tex/generic/pgf/libraries/decorations/pgflibrarydecorations.p
athmorphing.code.tex))
(/usr/share/texmf/tex/generic/pgf/frontendlayer/tikz/libraries/tikzlibrarydecor
ations.pathreplacing.code.tex
(/usr/share/texmf/tex/generic/pgf/libraries/decorations/pgflibrarydecorations.p
athreplacing.code.tex))
\pgfplots@numplots=\count129
\pgfplots@xmin@reg=\dimen182
\pgfplots@xmax@reg=\dimen183
\pgfplots@ymin@reg=\dimen184
\pgfplots@ymax@reg=\dimen185
\pgfplots@zmin@reg=\dimen186
\pgfplots@zmax@reg=\dimen187
)
(/usr/share/texmf/tex/generic/pgf/frontendlayer/tikz/libraries/tikzlibraryplotm
arks.code.tex
File: tikzlibraryplotmarks.code.tex 2008/01/09 v2.10 (rcs-revision 1.1)

那么,在 LaTeX 中绘制散点图的简单方法是什么?如何在图中添加回归线?

更新:这些是我正在使用的文件的版本:

grep File testi.log
File: amsgen.sty 1999/11/30 v2.0
File: umsa.fd 2009/06/22 v3.00 AMS symbols A
File: utf8.def 2008/04/05 v1.1m UTF-8 support for inputenc
File: t1enc.dfu 2008/04/05 v1.1m UTF-8 support for inputenc
File: ot1enc.dfu 2008/04/05 v1.1m UTF-8 support for inputenc
File: omsenc.dfu 2008/04/05 v1.1m UTF-8 support for inputenc
File: t1enc.def 2005/09/27 v1.99g Standard LaTeX file
File: babel.def 2008/07/06 v3.8l Babel common definitions
File: graphics.cfg 2009/08/28 v1.8 graphics configuration of TeX Live
File: dvips.def 1999/02/16 v3.0i Driver-dependant file (DPC,SPQR)
File: pgf.cfg 2008/05/14  (rcs-revision 1.7)
File: pgfsys-dvips.def 2008/04/03  (rcs-revision 1.20)
File: pgfsys-common-postscript.def 2008/10/01  (rcs-revision 1.22)
File: pgfsyssoftpath.code.tex 2008/07/18  (rcs-revision 1.7)
File: pgfsysprotocol.code.tex 2006/10/16  (rcs-revision 1.4)
File: color.cfg 2007/01/18 v1.5 color configuration of teTeX/TeXLive
File: pgfcorepoints.code.tex 2010/04/09  (rcs-revision 1.20)
File: pgfcorepathconstruct.code.tex 2010/08/03  (rcs-revision 1.24)
File: pgfcorepathusage.code.tex 2008/04/22  (rcs-revision 1.12)
File: pgfcorescopes.code.tex 2010/09/08  (rcs-revision 1.34)
File: pgfcoregraphicstate.code.tex 2008/04/22  (rcs-revision 1.9)
File: pgfcoretransformations.code.tex 2009/06/10  (rcs-revision 1.11)
File: pgfcorequick.code.tex 2008/10/09  (rcs-revision 1.3)
File: pgfcoreobjects.code.tex 2006/10/11  (rcs-revision 1.2)
File: pgfcorepathprocessing.code.tex 2008/10/09  (rcs-revision 1.8)
File: pgfcorearrows.code.tex 2008/04/23  (rcs-revision 1.11)
File: pgfcoreshade.code.tex 2008/11/23  (rcs-revision 1.13)
File: pgfcoreimage.code.tex 2010/03/25  (rcs-revision 1.16)
File: pgfcoreexternal.code.tex 2010/09/01  (rcs-revision 1.17)
File: pgfcorelayers.code.tex 2010/08/27  (rcs-revision 1.2)
File: pgfcoretransparency.code.tex 2008/01/17  (rcs-revision 1.2)
File: pgfcorepatterns.code.tex 2009/07/02  (rcs-revision 1.3)
File: pgfmoduleshapes.code.tex 2010/09/09  (rcs-revision 1.13)
File: pgfmoduleplot.code.tex 2010/10/22  (rcs-revision 1.8)
File: pgflibraryplothandlers.code.tex 2010/05/31 v2.10 (rcs-revision 1.15)
File: pgfmodulematrix.code.tex 2010/08/24  (rcs-revision 1.4)
File: tikzlibrarytopaths.code.tex 2008/06/17 v2.10 (rcs-revision 1.2)
File: pgfplotsoldpgfsupp_pgflibraryplothandlers.code.tex 2009/02/13 v2.10 (rcs-
File: tikzlibraryplotmarks.code.tex 2008/01/09 v2.10 (rcs-revision 1.1)
File: pgflibraryplotmarks.code.tex 2010/10/15 v2.10 (rcs-revision 1.12)
File: umsa.fd 2009/06/22 v3.00 AMS symbols A
File: umsb.fd 2009/06/22 v3.00 AMS symbols B

答案1

我认为您漏掉了一label列。以下 MWE 在这里运行良好:

\documentclass{article}

\usepackage{tikz,pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[%
scatter/classes={%
    a={mark=o,draw=black}}]
\addplot[scatter,only marks,%
    scatter src=explicit symbolic]%
table[meta=label] {
x y label
1 4.3 a
2 5.1 a
3 5.7 a
4 6.3 a
5 6.8 a
6 7.1 a
7 7.2 a
8 7.2 a
9 7.2 a
10 7.2 a
11 7.5 a
12 7.8 a
    };
\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

问题可能出在 Ubuntu 12.04 和 Texlive 2009 的组合上。当我将软件升级到 Ubuntu 12.10 和 Texlive 2012 并通过命令编译文件时pdflatex test.tex,我可以在 Ghostview 中看到正确的图片。

相关内容