我无法从.csv
文件中获取数据并生成简单的散点图。文件内容如下:
Group x y
Group1 10 1.1
Group2 20 8.1
Group3 9 0
Group1 11 2
Group2 20 8.2
Group3 8 0.2
Group1 11 2
Group2 20 9.1
Group3 9 0.1
代码如下:
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{statistics}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
grid=major,
grid style={dotted},
table/col sep=comma,
scatter/classes={%
Group1={brown, mark=*, mark size=1.5pt},
Group2={black, mark=x, mark size=2.5pt},
Group3={red, mark=square*, mark size=1pt}},
scatter, only marks,
scatter src=explicit symbolic
]
\addplot+ [] table[header=true, only marks, x=x, y=y, meta=Group] {Test.csv};%
\end{axis}
\end{tikzpicture}
\end{document}
我不断收到错误:
Sorry, could not retrieve column 'x' from table '\\pgfplotstableread@filename@@table@name '. Please check spelling (or introduce name aliases)..
答案1
\begin{filecontents}[overwrite]{Test.csv}
Group,x,y
Group1,10,1.1
Group2,20,8.1
Group3,9,0
Group1,11,2
Group2,20,8.2
Group3,8,0.2
Group1,11,2
Group2,20,9.1
Group3,9,0.1
\end{filecontents}
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{statistics}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
grid=major,
grid style={dotted},
table/col sep=comma,
scatter/classes={%
Group1={brown, mark=*, mark size=1.5pt},
Group2={black, mark=x, mark size=2.5pt},
Group3={red, mark=square*, mark size=1pt}},
scatter, only marks,
scatter src=explicit symbolic
]
\addplot+ [] table[header=true, only marks, x=x, y=y, meta=Group] {Test.csv};%
\end{axis}
\end{tikzpicture}
\end{document}
如果您想保留.csv
空间的原始结构,请在选项中更改为以下内容:
table/col sep=space,
\begin{filecontents}{test.csv}
Group x y
Group1 10 1.1
Group2 20 8.1
Group3 9 0
Group1 11 2
Group2 20 8.2
Group3 8 0.2
Group1 11 2
Group2 20 9.1
Group3 9 0.1
\end{filecontents}
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{statistics}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
grid=major,
grid style={dotted},
table/col sep=space,
scatter/classes={%
Group1={brown, mark=*, mark size=1.5pt},
Group2={black, mark=x, mark size=2.5pt},
Group3={red, mark=square*, mark size=1pt}},
scatter, only marks,
scatter src=explicit symbolic
]
\addplot+ [] table[header=true, only marks, x=x, y=y, meta=Group] {test.csv};%
\end{axis}
\end{tikzpicture}
\end{document}