tikz 散布図でテキスト ラベルが重ならないようにするにはどうすればよいですか?

tikz 散布図でテキスト ラベルが重ならないようにするにはどうすればよいですか?

を使用して、LaTeX で散布図をプロットしてみました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}

しかし、この場合、一部のラベルが重なり合ってしまい、正しく見えません。どうすれば重なりを避けることができますか? 1 つの方法は、より広く描画することだと思います。しかし、それはうまく[x=12cm,y=4cm]いかないことがわかりました。それとも、もっとスマートな方法がありますか?

答え1

次の例は、手動で修正する方法を示しています。問題のある 2 つのラベルは、マクロ\ToSouthとによって移動されます\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}

結果

答え2

私はこの質問を自分で解決しました。散布の幅は、次のように軸オプションで調整できます。

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

の代わりに

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

、これは機能しません。

関連情報