我想从数据文件生成散点图。数据文件可能如下所示:
x y color myvalue
0.5 0.2 1 test
0.2 0.1 2 uniform
我想将列中的字符串数据添加myvalue
到节点,但仍可以选择为标记着色。我尝试了手册中推荐的两件事:
\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[% scatter/use mapped color={draw=black,fill=mapped color},
enlargelimits=0.2]
\addplot[scatter, mark=*,only marks,
scatter src=explicit symbolic,
nodes near coords,]
table [x=x, y=y, meta=myvalue] {tab.tbl};
\end{axis}
\end{tikzpicture}
\end{document}
这使我能够将标签放到图中的标记上。
但我不知道如何添加颜色。我的第二次尝试是使用颜色作为点元数据(手册第 88 页),但后来我无法添加文本(它只接受下面失败代码中的数字):
% same preamble
\begin{tikzpicture}
\begin{axis}[enlargelimits=0.2]
\addplot[scatter, mark=*,only marks,
scatter src=explicit symbolic,
% we use ’point meta’ as color data...
point meta=\thisrow{color},
% ... therefore, we can’t use it as argument for nodes near coords ..
nodes near coords*={\myvalue},
% ... which requires to define a visualization dependency:
visualization depends on={\thisrow{myvalue} \as \myvalue},
]
table [x=x, y=y, meta=myvalue] {tab.tbl};
\end{axis}
\end{tikzpicture}
我怎样才能同时拥有两者?
答案1
哦,我在相关问题中找到了解决方案。
关键是value
在宏定义前面使用。
工作代码是:
\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[% scatter/use mapped color={draw=black,fill=mapped color},
enlargelimits=0.2]
\addplot[scatter, mark=*,only marks,
% we use ’point meta’ as color data...
point meta=\thisrow{color},
% ... therefore, we can’t use it as argument for nodes near coords
nodes near coords*={\myvalue},
% ... which requires to define a visualization dependency:
visualization depends on={value \thisrow{myvalue} \as \myvalue},
]
table [x=x, y=y]
{tab.tbl};
\end{axis}
\end{tikzpicture}
\end{document}