5

I tried to plot a scatter in latex using tikzpicture.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture} [x=12cm,y=4cm]
\begin{axis}[xlabel={y title}, ylabel={x title}]
\addplot[scatter,mark=*,only marks, point meta=x,nodes near coords*={\data},
visualization depends on={value \thisrow{dataname} \as \data},] 
table [x=x,y=y]{
x      y    dataname
99    113   water
37    142   rain
18    164   liver
5   195 coast
69   95 butterfly
330 36  mountain
121 96  dragonfly
208 0   greentree
};
\end{axis}
\end{tikzpicture}
\end{document}

However, in this case, some labels are overlapped and one can't see correctly. How can I avoid the overlap? I think one way to do it is to draw it more widely. However I found that [x=12cm,y=4cm] does not work. Or is there a smarter way to do it?

5
  • 1
    I solved this question for myself. the width of the scatter can be adjusted in axis option like: \begin{axis}[ xlabel={y title}, ylabel={x title}, width=15cm, height=9cm]
    – Fedal
    Mar 28, 2014 at 4:18
  • Welcome to TeX.sx! Your post was migrated here from Stack Overflow. Please register on this site, too, and make sure that both accounts are associated with each other (by using the same OpenID), otherwise you won't be able to comment on or accept answers or edit your question.
    – Werner
    Mar 28, 2014 at 16:27
  • You could delete the question and no one would notice. Oops, never mind. You got a point for it. Mar 29, 2014 at 16:23
  • We'd like to keep answers separate from questions, so you should write a separate answer instead of writing your answer in the comments. Self-answers are perfectly admissible, and a well-written answer may earn you additional reputation. Welcome to TeX.SX! Mar 31, 2014 at 5:30
  • 1
    Does this answer your question? "Collision" between marker labels in pgfplots scatter plot Apr 11, 2021 at 15:28

2 Answers 2

5

The following example shows a method for manual fixes. The two problematic labels are moved by macros \ToSouth and \ToWest.

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}

\begin{tikzpicture} [x=12cm,y=4cm]

  \def\MarkSize{.75em}
  \protected\def\ToWest#1{%
    \llap{#1\kern\MarkSize}\phantom{#1}%
  }
  \protected\def\ToSouth#1{%
    \sbox0{#1}%
    \smash{%
      \rlap{%
        \kern-.5\dimexpr\wd0 + \MarkSize\relax
        \lower\dimexpr.375em+\ht0\relax\copy0 %
      }%
    }%
    \hphantom{#1}%
  }

  \begin{axis}[
    xlabel={y title},
    ylabel={x title},
  ]
  \addplot[
    scatter,
    mark=*,
    only marks,
    point meta=x,
    nodes near coords*={\data},
    visualization depends on={value \thisrow{dataname} \as \data},
  ] 
  table [x=x,y=y]{
  x      y    dataname
  99    113   water
  37    142   rain
  18    164   liver
  5   195 coast
  69   95 \ToSouth{butterfly}
  330 36  \ToWest{mountain}
  121 96  dragonfly
  208 0   greentree
  };
  \end{axis}
\end{tikzpicture}
\end{document}

Result

1
  • 1
    how to do this with data from a csv file? Apr 11, 2021 at 15:07
1

I solved this question for myself. the width of the scatter can be adjusted in axis option like:

\begin{axis}[ xlabel={y title}, ylabel={x title}, width=15cm, height=9cm]

instead of

\begin{tikzpicture} [x=12cm,y=4cm] 

, which does not work.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .