0

I am trying to add labels on the scatter plot but the problem is the plot being too congested. I want my labels to be floating, i.e, find the place wherever possible.

I tried this code:

\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}

All the labels are too congested since the area in the plot where the bubbles are present is congested. I tried using the anchor, but when I use the anchor, the size of the bubbles become constant. It is important for me to maintain the size since size represents 3rd value.

I want them to find their own space. Is it possible? If yes, how can I do it?

1

When you change anchor by a "positioning", i.e. change anchor=south to above you can further add a shift to that "positioning". That solves your problem that the labels don't intersect with the bubbles any more, but still the labels intersect each other (because most of the bubbles are in a very small area. To further improve this I added another column to the table were you can specify an additional offset. If this is an option you can change the values to your needs. (You could also add line breaks to the labels which might help to easier find offsets that the labels don't intersect.)

% 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}

image showing the result of above code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.