我有以下散点图:
\documentclass[class=elsarticle]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
enlargelimits=0.2,
xlabel=Depth, ylabel=Breadth
]
\addplot+[nodes near coords,only marks,
point meta=explicit symbolic]
table[meta=label] {
Depth Breadth label
2.57 3.59 Cambridge
2.58 4.27 Dresden
2.45 3.77 {Palo\ Alto}
2.61 2.10 Asan
2.03 4.17 Heidelberg
2.25 4.29 Yamagata
};
\end{axis}
\end{tikzpicture}
\end{document}
我怎样才能将cambridge
标签放置在标记下方?
答案1
您可以这样做,尽管目前还不够简单。(在PGFPlots 追踪器为了那个原因。)
有关更多详细信息,请查看代码中的注释。
% used PGFPlots v1.14
% to provide the table only once either put it in an external file or
% load it to a command
% (because there currently is a bug in PGFPlotsTable when using filtering
% in a "loaded" table
% <https://sourceforge.net/p/pgfplots/bugs/109/>
% we have to store the data in a file)
\begin{filecontents*}{mydata.txt}
Depth Breadth label
2.57 3.59 Cambridge
2.58 4.27 Dresden
2.45 3.77 {Palo Alto}
2.61 2.10 Asan
2.03 4.17 Heidelberg
2.25 4.29 Yamagata
\end{filecontents*}
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{
% use this `compat' level or higher to make use of the improved
% positioning of the axes labels
compat=1.3,
% create styles to filter stuff and apply different `nodes near coords' styles
% (borrowed from <http://tex.stackexchange.com/a/301991/95441>)
discard if/.style 2 args={
% suppress LOG messages about the filtered points
filter discard warning=false,
x filter/.append code={
\edef\tempa{\thisrow{#1}}
\edef\tempb{#2}
\ifx\tempa\tempb
\def\pgfmathresult{NaN}
\fi
},
},
% here we want an OR combination so we have to modify the code a bit
% (`.append style' --> `.style')
discard if not/.style 2 args={
% suppress LOG messages about the filtered points
filter discard warning=false,
x filter/.code={
\edef\tempa{\thisrow{#1}}
\edef\tempb{#2}
\ifx\tempa\tempb
\else
\def\pgfmathresult{NaN}
\fi
},
},
% define the style of the `nodes near coords' that should be shown
% above the point
nodes near coords above style/.style={
nodes near coords style={
anchor=south,
},
},
% define the style of the `nodes near coords' that should be shown
% below the point
nodes near coords below style/.style={
nodes near coords style={
anchor=north,
},
},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
enlargelimits=0.2,
xlabel=Depth,
ylabel=Breadth,
only marks,
nodes near coords,
point meta=explicit symbolic,
table/meta=label,
]
% first draw all points that should show the `nodes near coords'
% above the points
\addplot+ [
% if *all* points -- also the ones with the other style(s) --
% should show the same style (except the `nodes near coords')
% then add `forget plot', so the `cycle list' index isn't
% increased
forget plot,
% apply the style which should be used
nodes near coords above style,
% state all the elements that should be filtered away
discard if={label}{Cambridge},
discard if={label}{Heidelberg},
% unfortunately there seems to be a bug that the filtering
% doesn't work for "stored" tables ...
% ] table {\datatable};
% ... that is, why we have to use the values stored in an external file
] table {mydata.txt};
% to apply the other style we need to loop through the elements
% which should get the other style
% (so the list should consist of the same names as above)
% the rest should be pretty clear now how it works
\pgfplotsinvokeforeach {
Cambridge,
Heidelberg%
} {
\addplot+ [
forget plot,
nodes near coords below style,
discard if not={label}{#1},
] table {mydata.txt};
}
\end{axis}
\end{tikzpicture}
\end{document}