我想画一个散点图根据 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}