pgfplot 堆积图

pgfplot 堆积图

关于下图所示的图表,我有几个问题:

  1. 我怎样才能以显示列顶部的方式放置图例?
  2. 如何在数据中添加 % 符号?
  3. 我怎样才能提高前两个数据以避免与 x 轴相交?

我发布了我实际拥有的图片和代码。

感谢您的支持!

在此处输入图片描述

 \usepackage{pgfplots, pgfplotstable}
 \begin{tikzpicture}
 \begin{axis}[
  title={Porcentaje anual sobre el total afiliados a la Seg. Social seg\'un    nacionalidad},
  ybar stacked, ymin=0, 
bar width=10mm,
symbolic x coords={2003, 2004, 2005, 2006, 2007},
xtick=data,
yticklabel=\pgfmathprintnumber{\tick}\,$\%$,
nodes near coords, 
nodes near coords align={anchor=north},%Move values in bar
totals/.style={nodes near coords align={anchor=south}},
x tick label style={anchor=south,yshift=-0.5cm},
%every node near coord/.style={
  %},
  ]
 %Active
 \addplot [fill=blue!50] coordinates {
({2003},5.6)
({2004},6.3)
({2005},9.3)
({2006},9.7)
({2007},10.3)};
%Inactive
\addplot [fill=red!50, point meta=explicit] coordinates {
({2003},94.4) [94.4]
({2004},93.7) [93.7]
({2005},90.7) [90.7]
({2006},90.3) [90.3]
({2007},89.7) [89.7]};
\legend{Extranjeros afiliados a la S. Social s/total,Espa\~noles afiliados a la S. Social s/total}
 %Dummy stacked plot to produce totals
 \addplot[totals] coordinates {
 ({2003},0)
 ({2004},0)
 ({2005},0)
 ({2006},0)
 ({2007},0)};
  \end{axis}
  \end{tikzpicture}

答案1

我希望我已经解决了您的所有问题。请参阅代码中的注释。

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=1.16}
\begin{document}
 \begin{tikzpicture}
 \begin{axis}[
  title={Porcentaje anual sobre el total afiliados a la Seg. Social seg\'un    nacionalidad},
  ybar stacked, ymin=0, 
bar width=10mm,
symbolic x coords={2003, 2004, 2005, 2006, 2007},
xtick=data,
yticklabel=\pgfmathprintnumber{\tick}\,$\%$,
nodes near coords, 
nodes near coords align={anchor=north},%Move values in bar
totals/.style={nodes near coords align={anchor=south}},
x tick label style={anchor=south,yshift=-0.5cm},
legend style={at={(1,1.2)}}, %<-place the legend outside of the plot 
title style={yshift=1cm}, %<-move the title up
ymin=-10,%<-move the axis up
yticklabel={$\pgfmathprintnumber{\tick}$\%}%<- add percent
%every node near coord/.style={
  %},
  ]
 %Active
 \addplot [fill=blue!50] coordinates {
({2003},5.6)
({2004},6.3)
({2005},9.3)
({2006},9.7)
({2007},10.3)};
%Inactive
\addplot [fill=red!50, point meta=explicit] coordinates {
({2003},94.4) [94.4]
({2004},93.7) [93.7]
({2005},90.7) [90.7]
({2006},90.3) [90.3]
({2007},89.7) [89.7]};
\legend{Extranjeros afiliados a la S. Social s/total,Espa\~noles afiliados a la S. Social s/total}
 %Dummy stacked plot to produce totals
 \addplot[totals] coordinates {
 ({2003},0)
 ({2004},0)
 ({2005},0)
 ({2006},0)
 ({2007},0)};
  \end{axis}
  \end{tikzpicture}
\end{document}  

在此处输入图片描述

相关内容