pgfplots:将标记标签放在图外

pgfplots:将标记标签放在图外

以下代码:

\documentclass{article}

\usepackage[english]{babel}

\usepackage{pgfplots}      

\usepackage{tikz}
\usepgfplotslibrary{groupplots}

\begin{document}

\begin{figure}
\begin{tikzpicture}
\pgfplotsset{width=7cm}
\pgfplotsset{every axis title/.style={at={(0.5,0)},below,yshift=-24pt}}
\pgfplotsset{every axis grid/.append style={very thin,dashed,gray}}

\begin{groupplot}[
group style={
xlabels at = edge bottom,
ylabels at = edge left},
width=0.67\linewidth,
height=0.67\linewidth,
xlabel={Original},
every axis x label/.style=
{at={(ticklabel cs:0.5)},anchor=near ticklabel, font=\scriptsize},
ylabel={DiSL-based},
every axis y label/.style=
{at={(ticklabel cs:0.5)},rotate=90,anchor=near ticklabel, font=\scriptsize},
xtick={500,1500,2500},
xticklabel={\axisdefaultticklabel },
ytick={500,1500,2500},
yticklabel={\axisdefaultticklabel },
extra x tick style={grid=major},
extra y tick style={grid=major},
axis equal,
ticklabel style={font=\scriptsize},
cycle list={
{black,fill=lightgray},
},
nodes near coords,
legend entries = {$ASM$, $AspectJ$, $other$,$geo. mean$},
legend columns=-1,
legend style={font=\scriptsize},
legend style={draw=none},
legend style={at={(0.75,1.18)}}
]
\nextgroupplot[title={},xmin=0,ymin=0,xmax=2500,ymax=2500]
\addplot+[ mark=*,only marks, point meta=explicit symbolic, font=\scriptsize]      coordinates {
(650,70) [jcarder]% jcarder 
(470, 105) [jp2]% jp2
(1306, 37) [jrat]% jrat
(2489 , 280)[emma] % emma 
(1048,788) [cobertura]% cobertura
};
\nextgroupplot[title={},xmin=0,ymin=0,xmax=2500,ymax=2500]
\addplot+[mark=square*,fill=lightgray,only marks, point meta=explicit symbolic,    font=\scriptsize] coordinates {
(100, 225) [senseo]% senseo
(120, 124) [racer]% racer
};
\draw[/pgfplots/every axis grid] (axis cs:0,0) -- (axis cs:2500,2500);
\end{groupplot}
\end{tikzpicture}

给我一个带有非常密集的标记标签的图,很难阅读。不幸的是,我无法在此处发布图片,但我的问题是,有没有办法将标记标签(racer、senseo、jp2)放在图外,并用箭头指向标记?

答案1

是的,你可以。一个想法是让pgfplots你自动生成带标签的坐标 - 然后你可以添加自定义节点,将其放置在你想要的任何位置,并连接到带标签的节点。

我相应地修改了你的例子:

在此处输入图片描述

\documentclass{article}

\usepackage[english]{babel}

\usepackage{pgfplots}      

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\pgfplotsset{width=7cm}
\pgfplotsset{every axis title/.style={at={(0.5,0)},below,yshift=-24pt}}
\pgfplotsset{every axis grid/.append style={very thin,dashed,gray}}

\begin{axis}[
title={},xmin=0,ymin=0,xmax=2500,ymax=2500,% ---- CF
width=0.67\linewidth,
height=0.67\linewidth,
xlabel={Original},
every axis x label/.style=
{at={(ticklabel cs:0.5)},anchor=near ticklabel, font=\scriptsize},
ylabel={DiSL-based},
every axis y label/.style=
{at={(ticklabel cs:0.5)},rotate=90,anchor=near ticklabel, font=\scriptsize},
xtick={500,1500,2500},
xticklabel={\axisdefaultticklabel },
ytick={500,1500,2500},
yticklabel={\axisdefaultticklabel },
extra x tick style={grid=major},
extra y tick style={grid=major},
axis equal,
ticklabel style={font=\scriptsize},
cycle list={
{black,fill=lightgray},
},
nodes near coords=,% ---------- CF
every node near coord/.style={anchor=center,name=N-\pgfplotspointmeta},% ------- CF
legend entries = {$ASM$, $AspectJ$, $other$,$geo. mean$},
legend columns=-1,
legend style={font=\scriptsize},
legend style={draw=none},
legend style={at={(0.75,1.18)}}
]
\addplot+[ mark=*,only marks, point meta=explicit symbolic, font=\scriptsize]      coordinates {
(650,70) [jcarder]% jcarder 
(470, 105) [jp2]% jp2
(1306, 37) [jrat]% jrat
(2489 , 280)[emma] % emma 
(1048,788) [cobertura]% cobertura
};
\addplot+[mark=square*,fill=lightgray,only marks, point meta=explicit symbolic,    font=\scriptsize] coordinates {
(100, 225) [senseo]% senseo
(120, 124) [racer]% racer
};
\draw[/pgfplots/every axis grid] (axis cs:0,0) -- (axis cs:2500,2500);
\end{axis}

\draw
   (current axis.north east) 
     ++ (20pt,0pt) 
     node {JCarder} 
   edge (N-jcarder);

\draw
   (current axis.north east) 
     ++ (20pt,-20pt) 
     node {Senseo} 
   edge (N-senseo);

\end{tikzpicture}

\end{document}

请注意,我消除了组图 - 它完全没有必要(因为 pgfplots 无论如何都会将图添加到当前轴)。

该代码包括以下变化:

  • 使用nodes near coords={}。空值告诉 pgfplots 不要将任何内容写入节点。

  • 提供一种样式,为其every node near coord分配一个姓名到每个节点。名称是扩展到您的符号名称的N-\pgfplotspointmeta位置\pgfplotspointmeta。换句话说:名称类似于等N-jcarder

  • 添加自定义\draw指令,将文本节点放置在某处,并使用 TikZ 的edge路径连接新的文本节点N-jcarder等。我想您可能希望非常单独地放置这些标签;我的选择仅仅是一些起点。

相关内容