我正在使用以下代码绘制带组的条形图。但我还想细分一些条形,在底部加阴影。例如,我希望最左边的条形直到数字 1 都有阴影/图案。此外,我想在图例中包含阴影所代表的内容,作为单个额外条目。有什么想法吗?
\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{shapes,arrows,positioning,fit,patterns}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=8cm, height=8cm,
ybar,
bar width=35pt,
ymin=0,
ymax = 6,
xtick=data,
enlarge x limits=0.5,
xticklabels={{Did 1st},{Did 2nd}},
x tick label style={text width=2cm,align=center},
ylabel={Number of groups},
nodes near coords,
nodes near coords align={vertical},
area legend,
legend style={draw=none,cells={anchor=west},legend pos=outer north east},
]
\addplot [color=black,fill=cyan,point meta=explicit symbolic] coordinates{(1,4) [67\%] (2,3) [50\%]};
\addlegendentry{Experimental}
\addplot [color=black,fill=red,point meta=explicit symbolic] coordinates{(1,1) [17\%] (2,2) [33\%]};
\addlegendentry{Control}
\end{axis}
\end{tikzpicture}
\end{document}
答案1
不幸的是,我认为没有自动的方法(如果没有,也许可能是功能请求),但最好的选择可能是手动定位一个节点,然后手动添加条目。
我尝试了各种方法,但这似乎是唯一的方法。
输出
代码
\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10,
/pgfplots/patt/.style={
legend image code/.code={
\begin{scope}[yshift=-.1cm]
\draw[pattern=north west lines, pattern color=black] (0cm,0cm) rectangle (6mm,2mm);
\end{scope}
}
}
}
\usetikzlibrary{shapes,arrows,positioning,fit,patterns,fillbetween}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=8cm, height=8cm,
ybar,
bar width=35pt,
ymin=0,
ymax = 6,
xtick=data,
enlarge x limits=0.5,
xticklabels={{Did 1st},{Did 2nd}},
x tick label style={text width=2cm,align=center},
ylabel={Number of groups},
nodes near coords,
nodes near coords align={vertical},
area legend,
legend style={draw=none,cells={anchor=west},legend pos=outer north east},
]
\addplot [color=black,fill=cyan,point meta=explicit symbolic] coordinates{(1,4) [67\%] (2,3) [50\%]};
\addlegendentry{Experimental}
\addplot [color=black,fill=red,point meta=explicit symbolic] coordinates{(1,1) [17\%] (2,2) [33\%]};
\addlegendentry{Control}
\node[draw,anchor=south east,pattern color=black, pattern=north west lines, minimum width=35pt,minimum height=30.3pt,xshift=-.8pt] at (0,0) {};
\addlegendimage{patt}
\addlegendentry{Pattern}
\end{axis}
\end{tikzpicture}
\end{document}