如何避免 tikz 散佈圖中文字標籤重疊?

如何避免 tikz 散佈圖中文字標籤重疊?

我嘗試使用 繪製乳膠中的散點圖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}

然而,在這種情況下,一些標籤重疊並且無法正確看到。如何避免重疊?我認為一種方法是將其繪製得更廣泛。然而我發現這[x=12cm,y=4cm]不起作用。或者有更聰明的方法嗎?

答案1

以下範例顯示了手動修復的方法。這兩個有問題的標籤由巨集\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

我自己解決了這個問題。散佈的寬度可以在 axis 選項中調整,例如:

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

代替

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

,這是行不通的。

相關內容