![带有 2 个部分的 pgfplots 标签导致解析错误](https://linux22.com/image/264814/%E5%B8%A6%E6%9C%89%202%20%E4%B8%AA%E9%83%A8%E5%88%86%E7%9A%84%20pgfplots%20%E6%A0%87%E7%AD%BE%E5%AF%BC%E8%87%B4%E8%A7%A3%E6%9E%90%E9%94%99%E8%AF%AF.png)
我想画一个散点图根据 dat 文件创建散点图包含数据的表格(实际上在外部文件中)的结构为label
、x=x
和y=y
,但标签名称不断出错。LaTex 读取直接护理
作为两个单独的列,我该如何纠正这个问题?您可以自行决定使用 Tikz、pgfplot 或其他。但是不需要数据标签。
\documentclass{article}
\usepackage{pgfplots,filecontents}
\pgfplotsset{compat=1.7}
\begin{filecontents*}{mydata.dat}
label x y
Direct Care 3 1
Housekeeping 2.366666667 5
Mealtimes 1 1
Medication Round 2.7 7
Miscellaneous 0.883333333 1
Personal Care 8 5
\end{filecontents*}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[xlabel=Time(mins),ylabel=Surface contacts]
\addplot[
visualization depends on={\thisrow{nodes}\as\myvalue},
scatter/classes={
Direct Care={mark=*,blue},
Housekeeping={mark=*,red},
Mealtimes={mark=*,blue},
Medication Round={mark=*,red},
Miscellaneous={mark=*,blue},
Personal Care={mark=*,red},
},
scatter, only marks,
scatter src=explicit symbolic,
]
table[meta=label,x=x,y=y]
{mydata.dat};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
答案1
您需要使用非空格的列分隔符(例如逗号),或者将多部分条目括在{...}
:
\documentclass{article}
\usepackage{pgfplots,filecontents}
\pgfplotsset{compat=1.7}
\begin{filecontents*}{mydata.dat}
label x y
{Direct Care} 3 1
Housekeeping 2.366666667 5
Mealtimes 1 1
{Medication Round} 2.7 7
Miscellaneous 0.883333333 1
{Personal Care} 8 5
\end{filecontents*}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[xlabel=Time(mins),ylabel=Surface contacts]
\addplot[
scatter/classes={
Direct Care={mark=*,blue},
Housekeeping={mark=*,red},
Mealtimes={mark=*,blue},
Medication Round={mark=*,red},
Miscellaneous={mark=*,blue},
Personal Care={mark=*,red}
},
scatter, only marks,
scatter src=explicit symbolic,
]
table[meta=label,x=x,y=y]
{mydata.dat};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}