在 tikz 上获取图外图例并自定义轴标签

在 tikz 上获取图外图例并自定义轴标签

我已经使用以下代码在 tikz 中制作了一个示意图。

\begin{tikzpicture}
\pgfplotsset{ticks=none}
\begin{axis}[
xlabel=$Treatment$,
ylabel=$Level of donation$]
\addplot[smooth,mark=*,blue] plot coordinates {
    (1,1)
    (2,1)
    (3,1)
};
\addlegendentry{Pure Altruism}

\addplot[smooth,color=red,mark=x]
plot coordinates {
    (1,1)
    (2,2)
    (3,1)
};
\addlegendentry{Warm Glow}

\addplot[smooth,color=green,mark=x]
plot coordinates {
    (1,1)
    (2,3)
    (3,3)
};
\addlegendentry{Mental Accounting}
\end{axis}
\end{tikzpicture}#

我的问题有两个。首先,我想将图例放在图之外。其次,我想在 x 轴上添加我自己的非数字标签。我希望这些标签是治疗名称,而不是数字。我该如何进行这两项调整?

答案1

这个答案与已经提供的答案非常相似。主要区别在于:

  • 用过的legend pos=outer north east
  • 用来xtick=data代替“手动”提供条目
  • 使用typeset ticklabels with strut,并且
  • 代码的一些其他小的修改/改进。

有关详细信息,请查看代码中的注释。

% used PGFPlots v1.15
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{
        % use this `compat' level or higher to use the advanced positioning of
        % the axis labels
        compat=1.3,
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        % (normal text should not be set in math mode)
        xlabel=Treatment,
        ylabel=Level of donation,
        % if you use `data' ticks will be set on every x coordinate that is
        % given by the *first* `\addplot' command
        xtick=data,
        xticklabels={
            one month,
            one year,
            one decade%
        },
        ytick=\empty,
        % use the following key so the baseline of all ticklabel entries is the same
        % (compare this image to the one from marmot)
        typeset ticklabels with strut,
        % there is one default value for the `legend pos' that is outside the axis
        legend pos=outer north east,
        % (so the legend looks a bit better)
        legend cell align=left,
        % (moved this common key here)
        smooth,
    ]
        % (renamed `plot coordinates' by `coordinates'
        \addplot [mark=*,blue] coordinates {
            (1,1)
            (2,1)
            (3,1)
        };

        \addplot [color=red,mark=x] coordinates {
            (1,1)
            (2,2)
            (3,1)
        };

        \addplot [color=green,mark=x] coordinates {
            (1,1)
            (2,3)
            (3,3)
        };

        % (replaced `\addlegendentry's with `\legend')
        \legend{
            Pure Altruism,
            Warm Glow,
            Mental Accounting,
        }
    \end{axis}
\end{tikzpicture}
\end{document}

该图显示了上述代码的结果

答案2

就是这样:

在此处输入图片描述

您可以配置legend style将其放置在图之外,并手动调整xtick和相应的xticklabels

\documentclass{article}

\usepackage{pgfplots}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
% \pgfplotsset{ticks=none}
\begin{axis}[
legend style={at={(1,1)},anchor=north west},
xtick={1,2,3},
xticklabels={One,Two,Three},
xlabel=$Treatment$,
ylabel=$Level of donation$]
\addplot[smooth,mark=*,blue] plot coordinates {
    (1,1)
    (2,1)
    (3,1)
};
\addlegendentry{Pure Altruism}

\addplot[smooth,color=red,mark=x]
plot coordinates {
    (1,1)
    (2,2)
    (3,1)
};
\addlegendentry{Warm Glow}

\addplot[smooth,color=green,mark=x]
plot coordinates {
    (1,1)
    (2,3)
    (3,3)
};
\addlegendentry{Mental Accounting}
\end{axis}
\end{tikzpicture}

\end{document}

答案3

与 Phelype Oleinik 的精彩回答非常相似,但对“外面的传奇”的解读不同。

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[legend to name=named,
xlabel=$Treatment$,
ylabel=$Level of donation$,
xticklabels={one month, one year, one decade},xtick={1,2,3},
ytick={},yticklabels={}]
\addplot[smooth,mark=*,blue] plot coordinates {
    (1,1)
    (2,1)
    (3,1)
};
\addlegendentry{Pure Altruism}

\addplot[smooth,color=red,mark=x]
plot coordinates {
    (1,1)
    (2,2)
    (3,1)
};
\addlegendentry{Warm Glow}

\addplot[smooth,color=green,mark=x]
plot coordinates {
    (1,1)
    (2,3)
    (3,3)
};
\addlegendentry{Mental Accounting}
\end{axis}
\end{tikzpicture}
\ref{named} % see pgfplots manual p. 266
\end{document}

在此处输入图片描述

相关内容