改变堆积条形图的颜色-pgfplot,tikz

改变堆积条形图的颜色-pgfplot,tikz

我正在尝试创建一个堆叠条形图,其中条形具有不同的灰色/黑色色调。我的代码如下。这确实会改变条形的颜色。但是,条形的轮廓仍保留在标准的红色/蓝色方案中,这也是图例中显示的内容。我如何完全更改配色方案?谢谢,Polly。

\begin{figure}[h]
\caption{Graph showing the proportion of children born into each partnership 
scenario by ethnic group (Table 5, Kiernan and Smith, 2003)}
\centering
\begin{tikzpicture}
\begin{axis}[
ybar stacked,
legend style={at={(0.5,-0.35)},
  anchor=north,legend columns=-1},
  x axis line style = { opacity = 0 }, 
  y axis line style = { opacity = 0 },
  tickwidth = 0pt,
bar width=10pt,
 ylabel={\% of births},
symbolic x coords={White British, Mixed Ethnicity, Indian, Pakistani, 
Bangladeshi, African Caribbean, Other Black, Other},
xtick=data,
x tick label style={rotate=45,anchor=east},
]
\addplot+[ybar] plot coordinates {(White British,14.4) (Mixed Ethnicity,38) 
(Indian,7.3)  (Pakistani,7) (Bangladeshi,9.3)(African Caribbean,51.6) (Other 
Black,39.4) (Other,14.3)} [fill=gray!90];
\addplot+[ybar] plot coordinates {(White British,27.1) (Mixed 
Ethnicity,21.4) 
(Indian,1.9)  (Pakistani,1.1) (Bangladeshi,2.9)(African Caribbean,16.3) 
(Other Black,14.2) (Other,9.8)} [fill=gray!50];
\addplot+[ybar] plot coordinates {(White British,58.5) (Mixed 
Ethnicity,40.6) 
(Indian,90.0)  (Pakistani,91.9) (Bangladeshi,87.8)(African Caribbean,32) 
(Other Black,46.5) (Other,75.9)}  [fill=gray!10];
\legend{Single, Cohabiting, Married}
\end{axis}
\end{tikzpicture}
\end{figure}

答案1

不要使用

\addplot +[<options>] plot coordinates {...} [<other options>];

你应该使用

\addplot +[<all options>] coordinates {...};

(无论如何哪个教程会教用户这样做\addplot plot?)

要设置边框颜色,draw=<colour>除了 之外,还要使用fill=<colour>。例如\addplot [fill=gray!90,draw=black!70] coordinates。因为您ybar stacked在选项中有axis,所以不必ybar为每个图指定。

代码输出

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{figure}[h]
\caption{Graph showing the proportion of children born into each partnership 
scenario by ethnic group (Table 5, Kiernan and Smith, 2003)}
\centering
\begin{tikzpicture}
\begin{axis}[
ybar stacked,
legend style={at={(0.5,-0.35)},
  anchor=north,legend columns=-1},
  x axis line style = { opacity = 0 }, 
  y axis line style = { opacity = 0 },
  tickwidth = 0pt,
bar width=10pt,
 ylabel={\% of births},
symbolic x coords={White British, Mixed Ethnicity, Indian, Pakistani, 
Bangladeshi, African Caribbean, Other Black, Other},
xtick=data,
x tick label style={rotate=45,anchor=east},
]
\addplot [fill=gray!90,draw=black!70]  coordinates {(White British,14.4) (Mixed Ethnicity,38) 
(Indian,7.3)  (Pakistani,7) (Bangladeshi,9.3)(African Caribbean,51.6) (Other 
Black,39.4) (Other,14.3)} ;
\addplot [fill=gray!50,draw=gray!80]  coordinates {(White British,27.1) (Mixed 
Ethnicity,21.4) 
(Indian,1.9)  (Pakistani,1.1) (Bangladeshi,2.9)(African Caribbean,16.3) 
(Other Black,14.2) (Other,9.8)};
\addplot [fill=gray!10,draw=gray!40]  coordinates {(White British,58.5) (Mixed 
Ethnicity,40.6) 
(Indian,90.0)  (Pakistani,91.9) (Bangladeshi,87.8)(African Caribbean,32) 
(Other Black,46.5) (Other,75.9)} ;
\legend{Single, Cohabiting, Married}
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

或者,cycle listaxis选项中定义一个自定义,并且不为每个设置任何选项\addplot。即添加

cycle list={
  {fill=gray!90,draw=black!70},
  {fill=gray!50,draw=gray!80},
  {fill=gray!10,draw=gray!40}
  }

axis选项,然后使用

\addplot coordinates {...};

输出如上。

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{figure}[h]
\caption{Graph showing the proportion of children born into each partnership 
scenario by ethnic group (Table 5, Kiernan and Smith, 2003)}
\centering
\begin{tikzpicture}
\begin{axis}[
  ybar stacked,
  legend style={at={(0.5,-0.35)},
  anchor=north,legend columns=-1},
  x axis line style = { opacity = 0 }, 
  y axis line style = { opacity = 0 },
  tickwidth = 0pt,
  bar width=10pt,
  ylabel={\% of births},
  symbolic x coords={
    White British,
    Mixed Ethnicity,
    Indian,
    Pakistani, 
    Bangladeshi,
    African Caribbean,
    Other Black,
    Other
  },
  xtick=data,
  x tick label style={rotate=45,anchor=east},
  cycle list={
    {fill=gray!90,draw=black!70},
    {fill=gray!50,draw=gray!80},
    {fill=gray!10,draw=gray!40}
    }
]
\addplot  coordinates {(White British,14.4) (Mixed Ethnicity,38) 
(Indian,7.3)  (Pakistani,7) (Bangladeshi,9.3)(African Caribbean,51.6) (Other 
Black,39.4) (Other,14.3)};

\addplot  coordinates {(White British,27.1) (Mixed 
Ethnicity,21.4) 
(Indian,1.9)  (Pakistani,1.1) (Bangladeshi,2.9)(African Caribbean,16.3) 
(Other Black,14.2) (Other,9.8)};

\addplot  coordinates {(White British,58.5) (Mixed 
Ethnicity,40.6) 
(Indian,90.0)  (Pakistani,91.9) (Bangladeshi,87.8)(African Caribbean,32) 
(Other Black,46.5) (Other,75.9)} ;

\legend{Single, Cohabiting, Married}
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}

相关内容