我在尝试构建一个非常简单的散点图时遇到了问题。csv 的组织方式如下:
0.09298303885566861,1976.677756339018,1.360252917788105
0.05630059591218889,3608.903320218602,-12.0558030777737
0.1027903546069633,1976.677756339018,0.8145755693068558
0.08901905835364105,2282.470869446955,-1.414111279194796
0.07962106630870476,2551.88001037322,0.002312934257887339
我需要在前两列指定的位置绘制标记,并根据第三列为它们着色。没有任何错误,数据绘制在正确的位置,但根据第二列着色。
肯定是有一个非常简单的错误。代码是:
\documentclass[border=9,tikz]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[scale=0.75]
\begin{axis}[]
\addplot[scatter, only marks] table [x index=0, y index=1, meta index=2, col sep=comma] {files/data.csv};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
您需要设置scatter src
,而不是meta index
。
\documentclass[border=9,tikz]{standalone}
\usepackage{pgfplots,filecontents}
\begin{filecontents*}{data.dat}
0.09298303885566861,1976.677756339018,1.360252917788105
0.05630059591218889,3608.903320218602,-12.0558030777737
0.1027903546069633,1976.677756339018,0.8145755693068558
0.08901905835364105,2282.470869446955,-1.414111279194796
0.07962106630870476,2551.88001037322,0.002312934257887339
\end{filecontents*}
\begin{document}
\begin{tikzpicture}[scale=0.75]
\begin{axis}[colorbar]
\addplot[scatter, only marks] table [x index=0, y index=1, scatter src=\thisrowno{2}, col sep=comma] {data.dat};
\end{axis}
\end{tikzpicture}
\end{document}