我正在尝试在散点图上添加标签,但问题是散点图太拥挤了。我希望我的标签是浮动的,即尽可能找到位置。
我尝试了这个代码:
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
width=4.5in,
height=3.5in,
xtick={0,50,100,150,200,250,300,350,400},
xlabel=Length,
ylabel=Info,
legend pos=north west,
]
\addplot[%
scatter=true,
only marks,
mark=*,
point meta=explicit,
visualization depends on = {85*\thisrow{Val} \as \perpointmarksize},
scatter/@pre marker code/.append style={/tikz/mark
size=\perpointmarksize},
nodes near coords*={\Label},
visualization depends on={value \thisrow{label} \as \Label}
visualization depends on={value \thisrow{anchor}\as\myanchor},
every node near coord/.append style={anchor=\myanchor},
] table [y={Info},x={Length},meta index=2] {
Info Length Val label anchor
361.805 391.28 0.006538731 {Scrabble} south
19.575 141.25 0.009144791 {Duolingo (Winning Streak)} south
28 141.25 0.010245367 {Duolingo (Badge)} north
250 208 0.011528558 {Go} south
80 115 0.016548609 {Shogi} south
68.6 106.2 0.017291188 {DotA} south
54.86 96.47 0.018028981 {Table Tennis} south
36.38 82.01 0.01903259 {Basketball} south
2.64 22 0.019163815 {Soccer} south
35 80 0.019300851 {Chess} south
46.34 79.34 0.020997681 {Badminton} south
242.3043 25.52174 0.093424709 {Baghchal} south
9 9 0.106026945 {Tic-tac-toe} north
};
\end{axis}
\end{tikzpicture}
\caption{multiple games}
\label{fig:f4}
\end{figure}
所有标签都太拥挤了,因为图中气泡所在的区域很拥挤。我尝试使用锚点,但当我使用锚点时,气泡的大小会变得恒定。对我来说,保持大小很重要,因为大小代表第三个值。
我想让他们找到自己的空间。可以吗?如果可以,我该怎么做?
答案1
当您anchor
通过“定位”进行更改时,即更改anchor=south
为,above
您可以进一步向该“定位”添加移位。这解决了您的问题,即标签不再与气泡相交,但标签仍然相互相交(因为大多数气泡都在非常小的区域内)。为了进一步改善这一点,我在表格中添加了另一列,您可以在其中指定额外的偏移量。如果这是一个选项,您可以根据需要更改值。(您还可以向标签添加换行符,这可能有助于更轻松地找到标签不相交的偏移量。)
% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{
compat=1.3,
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=4.5in,
height=3.5in,
xlabel=Length,
ylabel=Info,
% (it is simpler to use `xtick distance`)
% xtick={0,50,100,150,200,250,300,350,400},
xtick distance=50,
]
\addplot[
scatter=true,
only marks,
mark=*,
point meta=explicit,
visualization depends on={85*\thisrow{Val} \as \perpointmarksize},
scatter/@pre marker code/.append style={
/tikz/mark size=\perpointmarksize,
},
nodes near coords*={\Label},
visualization depends on={value \thisrow{label} \as \Label},
% visualization depends on={value \thisrow{anchor} \as \myanchor},
visualization depends on={value \thisrow{Pos} \as \myPos},
visualization depends on={\thisrow{addOffset} \as \myOffset},
visualization depends on={\thisrow{Val} \as \myval},
every node near coord/.append style={
font=\tiny,
% anchor=\myanchor,
\myPos=\perpointmarksize pt + \myOffset pt,
},
] table [y={Info},x={Length},meta index=2] {
Info Length Val label anchor Pos addOffset
361.805 391.28 0.006538731 {Scrabble} south above 0
19.575 141.25 0.009144791 {Duolingo (Winning Streak)} south above 0
28 141.25 0.010245367 {Duolingo (Badge)} north below 0
250 208 0.011528558 {Go} south above 0
80 115 0.016548609 {Shogi} south above 0
68.6 106.2 0.017291188 {DotA} south above 0
54.86 96.47 0.018028981 {Table Tennis} south above 0
36.38 82.01 0.01903259 {Basketball} south above 0
2.64 22 0.019163815 {Soccer} south above 0
35 80 0.019300851 {Chess} south above 0
46.34 79.34 0.020997681 {Badminton} south above 0
242.304 25.5217 0.093424709 {Baghchal} south above 0
9 9 0.106026945 {Tic-tac-toe} north below 0
};
\end{axis}
\end{tikzpicture}
\end{document}