我的输入数据是一个包含多列和多行的 txt 文件。最后一列的值始终是一个整数,取值为 0、1、2 或 3。
我需要一个散点图,它根据最终值具有不同的标记。
\documentclass{article}
\usepackage{pgfplots}
\usepackage{filecontents}
\begin{filecontents*}{data.txt}
12.3458,0.709423,0.018174,10.3177,0.031258,0.360285,0.071809,0
13.3458,0.709423,0.018174,10.3177,0.031258,0.360285,0.171809,0
7.88918,0.037782,0.010597,13.0123,0.027078,0.345659,0.070872,1
8.88918,0.037782,0.010597,13.0123,0.027078,0.345659,0.170872,1
3.29679,0.175776,0.012142,18.2475,0.031448,0.292123,0.141521,2
4.29679,0.175776,0.012142,18.2475,0.031448,0.292123,0.241521,2
3.94161,0.204657,0.002334,2.09774,0.011567,0.278266,0.113811,3
4.94161,0.204657,0.002334,2.09774,0.011567,0.278266,0.213811,3
\end{filecontents*}
\begin{document}
\begin{tikzpicture}
\pgfplotstableread[col sep=comma] {data.txt}\thedata
\begin{axis}[width=7cm,height=7cm]
\addplot[scatter,
only marks,
% visualization depends on={value \thisrow{index 7} \as \labela},
% scatter/@pre marker code/.append style = {/tikz/mark=\labela},
] table[x index=0,y index=6] {\thedata};
\end{axis}
\end{tikzpicture}
\end{document}
导致
当我取消注释这些行时,没有出现任何标记。
- 我需要定义要使用的标记:例如,
x, o, sqaure, triangle
只有 4 个。 - 最好,我需要设置它们的颜色(例如
x
是红色,o
是蓝色,square
是绿色,triangle
是黑色) - 该图表将在具有不同数据的文档中多次使用。
- 如果可能的话,我希望保持
pgfplotstableread
在范围内tikzpicture
。
pgfplots 手册的第 187 页看起来相关,但我无法进一步了解
还有一些其他帖子非常有帮助,但它们都没有将整数转换为标记符号:
答案1
你可以按如下方式使用它:不要使用以 开头的宏名\the
。这是 TeX 的特殊情况,可能会导致很难调试的错误。
\documentclass{article}
\usepackage{pgfplotstable}
\begin{document}
\begin{tikzpicture}
\pgfplotstableread[col sep=comma,header=false]{
12.3458,0.709423,0.018174,10.3177,0.031258,0.360285,0.071809,0
13.3458,0.709423,0.018174,10.3177,0.031258,0.360285,0.171809,0
7.88918,0.037782,0.010597,13.0123,0.027078,0.345659,0.070872,1
8.88918,0.037782,0.010597,13.0123,0.027078,0.345659,0.170872,1
3.29679,0.175776,0.012142,18.2475,0.031448,0.292123,0.141521,2
4.29679,0.175776,0.012142,18.2475,0.031448,0.292123,0.241521,2
3.94161,0.204657,0.002334,2.09774,0.011567,0.278266,0.113811,3
4.94161,0.204657,0.002334,2.09774,0.011567,0.278266,0.213811,3
}\mydata
\begin{axis}[width=7cm,height=7cm]
\addplot+[scatter, only marks,
scatter/classes={0={mark=square*,green},
1={mark=triangle*,black},
2={mark=o,blue},
3={mark=x,red}
},
scatter src=explicit symbolic
] table[x index=0,y index=6,meta index=7] \mydata;
\end{axis}
\end{tikzpicture}
\end{document}