PGF - 如何对齐不同类型的图例?

PGF - 如何对齐不同类型的图例?

我制作了以下图表:

在此处输入图片描述

如您所见,第二列图例条目看起来非常丑陋,因为两个条形图例(私人消费,GFCF)距离文本太远。

我如何使它们与文本的距离与第一列相同?

生成上述内容的代码:

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.8}

%colors
\usepackage{color} % colors
\usepackage{xcolor} 
\definecolor{cone}{RGB}{18,32,132} % blue
\definecolor{ctwo}{RGB}{202,211,43} % orange
\definecolor{ctwol}{RGB}{255, 192, 0} % lighter orange
\definecolor{cthree}{RGB}{0, 176, 80} % green 
\definecolor{cfour}{RGB}{255, 0, 0} % red
\definecolor{cfive}{RGB}{0, 176, 240} % light blue

\begin{document}

\pgfplotstableread[col sep=comma]{
date, pvtcons, pubcons, gfcf, netex, stocks, rgdp
1Q19, 4.3, 0.7, -0.9, 0.9, -0.5, 4.5
2Q19, 4.4, 0.0, -0.2, 1.4, -0.7, 4.9
3Q19, 4.1, 0.1, -0.9, 1.0, 0.0, 4.4
4Q19, 4.5, 0.2, -0.2, -0.7, -0.2, 3.6
1Q20, 0.0, 0.1, -3.1, -0.6, -1.9, -5.5
}\chartfive

\pgfplotsset{/pgfplots/ybar legend/.style={
        /pgfplots/legend image code/.code={%
            \draw[##1,/tikz/.cd,yshift=1cm, xshift = -0.1cm]
            (0cm,0cm) rectangle (0.6em,0.6em);},},
}

\begin{tikzpicture}
\small
\begin{axis}[
x=15mm,
bar width=3mm,
axis lines=left,
axis x line shift=-6.1,
enlarge x limits=0.1,
%
% y ticks style and label
ylabel={Annual change (\%)},
ymin = -6.1,
ylabel shift = 1pt,
ytick distance = 1,
%
% x axis ticks and style
xticklabel shift=3cm,
xtick=data,
xticklabels from table={\chartfive}{date},  
table/x expr = \coordindex,                     
x tick label style = {rotate=0},
%
% legends and labels
legend cell align={left},
legend style = {fill = none, draw=none,
    legend columns=2,
    at={(0.5,1.3)},
    anchor=north,
    /tikz/every even column/.append style={column sep=2em},
},
]
%
% done with the axis, now the plots
\addplot [ybar stacked,  bar width=3mm, cthree, fill=cthree]
table [y=pubcons]  {\chartfive};
\addlegendentry{Public consumption};
\addplot [ybar stacked,  bar width=3mm, ctwol, fill=ctwol]               
table [y=pvtcons]  {\chartfive};
\addlegendentry{Private consumption};
\addplot [ybar stacked,  bar width=3mm, cfour, fill=cfour]               
table [y=netex]  {\chartfive};
\addlegendentry{Net exports};
\addplot [ybar stacked,  bar width=3mm, cone, fill=cone]               
table [y=gfcf]  {\chartfive};
\addlegendentry{GFCF};
\addplot [ybar stacked,  bar width=3mm, cfive, fill=cfive]               
table [y=stocks]  {\chartfive};
\addlegendentry{Change in stocks};
\addplot [draw = black, thick, mark = x]               
table [y=rgdp]  {\chartfive};
\addlegendentry{Real GDP};
\end{axis}
\end{tikzpicture}

\end{document}

答案1

您的 MWE 中已经有一些了legend image code。我只是根据当前需求进行了调整。

\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.17}%<- use a smaller version if you have an older installation

\definecolor{cone}{RGB}{18,32,132} % blue
\definecolor{ctwo}{RGB}{202,211,43} % orange
\definecolor{ctwol}{RGB}{255, 192, 0} % lighter orange
\definecolor{cthree}{RGB}{0, 176, 80} % green 
\definecolor{cfour}{RGB}{255, 0, 0} % red
\definecolor{cfive}{RGB}{0, 176, 240} % light blue

\begin{document}

\pgfplotstableread[col sep=comma]{
date, pvtcons, pubcons, gfcf, netex, stocks, rgdp
1Q19, 4.3, 0.7, -0.9, 0.9, -0.5, 4.5
2Q19, 4.4, 0.0, -0.2, 1.4, -0.7, 4.9
3Q19, 4.1, 0.1, -0.9, 1.0, 0.0, 4.4
4Q19, 4.5, 0.2, -0.2, -0.7, -0.2, 3.6
1Q20, 0.0, 0.1, -3.1, -0.6, -1.9, -5.5
}\chartfive

\pgfplotsset{/pgfplots/new ybar legend/.style={
        /pgfplots/legend image code/.code={%
            \draw[##1,/tikz/.cd,yshift=-0.1cm, xshift = +0.2cm
            ]
            (0cm,0cm) rectangle (0.6em,0.8em);},},              
}

\begin{tikzpicture}
\small
\begin{axis}[ybar stacked/.append style={new ybar legend},
x=15mm,
bar width=3mm,
axis lines=left,
axis x line shift=-6.1,
enlarge x limits=0.1,
%
% y ticks style and label
ylabel={Annual change (\%)},
ymin = -6.1,
ylabel shift = 1pt,
ytick distance = 1,
%
% x axis ticks and style
xticklabel shift=3cm,
xtick=data,
xticklabels from table={\chartfive}{date},  
table/x expr = \coordindex,                     
x tick label style = {rotate=0},
%
% legends and labels
legend cell align={left},
legend style = {fill = none, draw=none,
    legend columns=2,
    at={(0.5,1.3)},
    anchor=north,
    /tikz/every even column/.append style={column sep=2em},
},
]
%
% done with the axis, now the plots
\addplot [ybar stacked, bar width=3mm, cthree, fill=cthree]
table [y=pubcons]  {\chartfive};
\addlegendentry{Public consumption};
\addplot [ybar stacked, bar width=3mm, ctwol, fill=ctwol]               
table [y=pvtcons]  {\chartfive};
\addlegendentry{Private consumption};
\addplot [ybar stacked, bar width=3mm, cfour, fill=cfour]               
table [y=netex]  {\chartfive};
\addlegendentry{Net exports};
\addplot [ybar stacked, bar width=3mm, cone, fill=cone]               
table [y=gfcf]  {\chartfive};
\addlegendentry{GFCF};
\addplot [ybar stacked, bar width=3mm, cfive, fill=cfive]               
table [y=stocks]  {\chartfive};
\addlegendentry{Change in stocks};
\addplot [draw = black, thick, mark = x]               
table [y=rgdp]  {\chartfive};
\addlegendentry{Real GDP};
\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容