在 Tikz 图表中自定义 y 轴

在 Tikz 图表中自定义 y 轴

在此处输入图片描述我想自定义下图中的 y 轴,使该轴上的数字 1 和 2 不重叠。非常感谢您的意见。

实际代码如下:

\documentclass[english, 12pt]{article}
\usepackage{tikz}
\usepackage[absolute,overlay]{textpos}

\usepackage{forest}


\usepackage{textcomp}

\usepackage{pgfplots}
\pgfplotsset{width=10cm,compat=1.9}

\usetikzlibrary{decorations.pathreplacing,arrows.meta,calligraphy}
\begin{document}
\begin{figure}
    \centering
    \begin{tikzpicture}[scale=1]
    \begin{axis}[
    %title={},
    xlabel={Size of the supermajority ($\alpha$)},
    ylabel={Maximum number of equilibria {\large \textbf{$N^{*}$}}},
    xmin=0.5, xmax=1,
    ymin=1, ymax=100,
    xtick={0.5, .6,.7, .8, .9, 1},
    ytick={1, 2, 10, 22, 34, 42, 52, 62, 82, 100, 110},
    legend pos=outer north east,
    ymajorgrids=true,
    xmajorgrids=true,
    grid style=dashed,
    ]
    \addplot[
    color=blue,
    mark=*,
    ]
    coordinates {
        (.5,2)(.6,22)(0.67,34)(0.7,42)(0.75,52)(.8,62)(0.9,82)(1,100)
    };
    \addlegendentry{Maximum number of equilibria}
    \addplot[
    color=red,
    mark=*,
    ]
    coordinates {
        (.5,1)(.6,1)(0.67,1)(0.7,1)(0.75,1)(.8,1)(0.9,1)(1,1)
    };
    \addlegendentry{minimum number of equilibria}
    \end{axis}
    \end{tikzpicture}
        \end{figure}
\end{document}

答案1

您可以使用 将“1”向下移动 4pt yticklabel style={yshift={(\ticknum == 0 ? "-4pt" : 0)}},使用 将“2”向上移动 3pt yticklabel style={yshift={(\ticknum == 1 ? "3pt" : 0)}}

第一个关键点是:

  1. yticklabel style =... 为给定的值设置 yticklabel 样式ytick={...}
  2. yshift=... 垂直移动 yticklabel
  3. \ticknum == 0 ?... 测试这是否是第一个 ticknum(注意tiknum从 0 开始)
  4. "-4pt"... 如果这是第一个刻度,则向下应用 4pt 的垂直偏移
  5. : 0...否则什么也不做

我还在enlarge y limits=true“1”和 x 轴之间添加了一些空间。

结果如下: 在此处输入图片描述

这是 MWE:

\documentclass[english, 12pt]{article}
\usepackage{tikz}
\usepackage[absolute,overlay]{textpos}
\usepackage{forest}
\usepackage{textcomp}
\usepackage{pgfplots}
\pgfplotsset{width=10cm,compat=1.9} % Compatability could be set to 1.15 or newest

\usetikzlibrary{decorations.pathreplacing,arrows.meta,calligraphy}
\begin{document}
\begin{figure}
    \centering
    \begin{tikzpicture}[scale=1]
    \begin{axis}[
    %title={},
    xlabel={Size of the supermajority ($\alpha$)},
    ylabel={Maximum number of equilibria {\large \textbf{$N^{*}$}}},
    xmin=0.5, xmax=1,
    ymin=1, ymax=100,
    enlarge y limits=true,
    xtick={0.5, .6,.7, .8, .9, 1},
    ytick={1, 2, 10, 22, 34, 42, 52, 62, 82, 100, 110},
    yticklabel style={yshift={(\ticknum == 0 ? "-4pt" : 0)}},
    yticklabel style={yshift={(\ticknum == 1 ? "3pt" : 0)}},
    legend pos=outer north east,
    ymajorgrids=true,
    xmajorgrids=true,
    grid style=dashed,
    ]
    \addplot[
    color=blue,
    mark=*,
    ]
    coordinates {
        (.5,2)(.6,22)(0.67,34)(0.7,42)(0.75,52)(.8,62)(0.9,82)(1,100)
    };
    \addlegendentry{Maximum number of equilibria}
    \addplot[
    color=red,
    mark=*,
    ]
    coordinates {
        (.5,1)(.6,1)(0.67,1)(0.7,1)(0.75,1)(.8,1)(0.9,1)(1,1)
    };
    \addlegendentry{minimum number of equilibria}
    \end{axis}
    \end{tikzpicture}
        \end{figure}
\end{document}

答案2

例如,你可以尝试

yticklabels={,,10,22,34,42,52,62,82,100,110},

如下所示:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{width=10cm,compat=1.9}
\begin{document}
    \begin{tikzpicture}
    \begin{axis}[
    %title={},
    xlabel={Size of the supermajority ($\alpha$)},
    ylabel={Maximum number of equilibria {\large \textbf{$N^{*}$}}},
    xmin=0.5, xmax=1,
    ymin=1, ymax=100,
    xtick={0.5, .6,.7, .8, .9, 1},
    ytick={1, 2, 10, 22, 34, 42, 52, 62, 82, 100, 110},
    yticklabels={,,10,22,34,42,52,62,82,100,110},
    legend pos=outer north east,
    ymajorgrids=true,
    xmajorgrids=true,
    grid style=dashed,
    ]
    \addplot[
    color=blue,
    mark=*,
    ]
    coordinates {
        (.5,2)(.6,22)(0.67,34)(0.7,42)(0.75,52)(.8,62)(0.9,82)(1,100)
    };
    \addlegendentry{Maximum number of equilibria}
    \addplot[
    color=red,
    mark=*,
    ]
    coordinates {
        (.5,1)(.6,1)(0.67,1)(0.7,1)(0.75,1)(.8,1)(0.9,1)(1,1)
    };
    \addlegendentry{minimum number of equilibria}
    \end{axis}
    \end{tikzpicture}
\end{document}

答案3

这不是 Tikz 的问题。只需1ytick列表中删除即可。

在此处输入图片描述

相关内容