PGFPlots 节点放置避开轴

PGFPlots 节点放置避开轴

我有一个图,其中包含分配给每个坐标的几个点和节点。渲染时,一些节点与轴或刻度重叠,我想避免这种情况。我试过使用anchorxshift(和yshift)等...

我如何设置节点的样式以便它们自动避开轴和刻度?

在此处输入图片描述

main.tex

\documentclass[a4paper]{article}

\usepackage{amsmath} % Advanced math typesetting
\usepackage{pgfplots} % Plotting

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        scale=1.5, % Make the plot larger
        title={Points sur le plan complexe}, % Title on top
        xlabel=$Re(z)$, ylabel=$Im(z)$, % Label X and y axes
        xstep=1, ystep=1, % Set the incrementation
        xtick={-2,...,2}, ytick={-2,...,2}, % Incrementation
        axis lines=middle, % Align axes on the center
        enlarge x limits=0.1, enlarge y limits=0.1, % Padding
        axis equal, % Orthogonal
        grid, thick, % Grid options
    ]

        \addplot[
            only marks,
            mark=*,
            nodes near coords={\label}, % Use labels
            visualization depends on={value \thisrow{Label}\as\label}, % Define labels
            every node near coord/.append style={
                font=\large,
                red,
                anchor= north west,
            } % Define label style
        ]
        table[
            x=X, % Read coordinates from table
            y=Y,
            y expr={\thisrow{Y}}, % Make the table read fractions
        ] {coordinates.dat};

    \end{axis}
\end{tikzpicture}
\end{document}

coordinates.dat

Label X   Y
A     0   2
B     1   0
C     1   3/4
D     0   -2
E     -1  0

答案1

一个不太优雅的方法是添加一个额外的列。

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{filecontents}
\begin{filecontents}{coordinates.dat}
Label X   Y   Yshift
A     0   2   0
B     1   0   20
C     1   3/4 0
D     0   -2  0
E     -1  0   20
\end{filecontents}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        scale=1.5, % Make the plot larger
        title={Points sur le plan complexe}, % Title on top
        xlabel=$\mathrm{Re}(z)$, ylabel=$\mathrm{Im}(z)$, % Label X and y axes
        xstep=1, ystep=1, % Set the incrementation
        xtick={-2,...,2}, ytick={-2,...,2}, % Incrementation
        axis lines=middle, % Align axes on the center
        enlarge x limits=0.1, enlarge y limits=0.1, % Padding
        axis equal, % Orthogonal
        grid, thick, % Grid options
    ]

        \addplot[
            only marks,
            mark=*,
            nodes near coords={\label}, % Use labels
            visualization depends on={value \thisrow{Label}\as\label}, % Define labels
            visualization depends on={value \thisrow{Yshift}\as\myshift}, % Define labels
            every node near coord/.append style={
                font=\large,
                red,
                anchor=north west,
                yshift=\myshift
            } % Define label style
        ]
        table[
            x=X, % Read coordinates from table
%            y=Y, %<- you don't need this
            y expr={\thisrow{Y}}, % Make the table read fractions
        ] {coordinates.dat};

    \end{axis}
\end{tikzpicture}

在此处输入图片描述

更新:避开新栏目并不太难。

\documentclass{standalone}
\usepackage{amsmath}
\DeclareMathOperator{\re}{Re}
\DeclareMathOperator{\im}{Im}
\usepackage{pgfplots}
\usepackage{filecontents}
\begin{filecontents}{coordinates.dat}
Label X   Y  
A     0   2  
B     1   0  
C     1   3/4
D     0   -2 
E     -1  0  
\end{filecontents}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        scale=1.5, % Make the plot larger
        title={Points sur le plan complexe}, % Title on top
        xlabel=$\re z$, ylabel=$\im z$, % Label X and y axes
        xstep=1, ystep=1, % Set the incrementation
        xtick={-2,...,2}, ytick={-2,...,2}, % Incrementation
        axis lines=middle, % Align axes on the center
        enlarge x limits=0.1, enlarge y limits=0.1, % Padding
        axis equal, % Orthogonal
        grid, thick, % Grid options
    ]

        \addplot[
            only marks,
            mark=*,
            nodes near coords={\label}, % Use labels
            visualization depends on={value \thisrow{Label}\as\label}, % Define labels
            visualization depends on={value \thisrow{Y}\as\myy}, % Define labels
            every node near coord/.append style={
                font=\large,
                red,
                anchor=north west,
                yshift={(1-sign(\myy*\myy))*20}
            } % Define label style
        ]
        table[
            x=X, % Read coordinates from table
%            y=Y, %<- you don't need this
            y expr={\thisrow{Y}}, % Make the table read fractions
        ] {coordinates.dat};

    \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

我没能说服 Tiz 根据是否改变锚点y=0

当然,如果有一个可以自动避免这些碰撞的程序会更好,但这远远超出了我目前的技能。

相关内容