如何使用两个y轴标签将此pgf图切换到相邻的条形图

时间:2018-02-06 18:02:27

标签: latex tex tikz pgf

我有以下代码,大致近似我想用tikz制作的图表:

\begin{tikzpicture}
\begin{axis}[
% (normal text should not be set in math mode)
xlabel=,
ylabel=,
% if you use `data' ticks will be set on every x coordinate that is
% given by the *first* `\addplot' command
xtick=data,
xticklabels={
    Control,
    Voucher,
    Transfer%
},
ytick=data,
yticklabels={
    No increase,
    Increase%
},
% 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),
]
% (renamed `plot coordinates' by `coordinates'
\addplot [mark=*,blue] coordinates {
    (1,1)
    (2,1)
    (3,1)
};

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

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

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

enter image description here

正如您所看到的,我尝试添加y轴刻度标签,但实际图表本身并未出现这种情况。此外,我试图说明的是离散的,所以我希望x轴上的每个刻度都有三个相邻的条,这将说明水平是否应该是“高”。或者“低”'根据传说中的每一个理论。

如何将其转换为带有三个相邻条形的条形图并添加我需要的两个y轴标签?

1 个答案:

答案 0 :(得分:1)

根据this answer,当您使用ytick=data时,标签将从使用的第一个\addplot命令中提取。您的第一个\addplot命令仅包含1作为y tick标签。

您可以明确定义标签:

ytick={1, 2},

或更改\addplot命令的顺序,使其具有更具代表性的命令(一个包含所有x刻度标签所有y刻度标签)。

enter image description here