我想问您有关条形图和位置图例的问题。
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[ xbar,
%enlargelimits=0.14,width=8cm,height=6cm,,
bar width=10pt,area legend,legend pos=south east,
legend style={cells={anchor=west}}, area legend,
tick align=outside,xlabel=\textbf{EUR Billion},
ytick={1,...,3},
yticklabel style={text width=3.3cm,align=right},
yticklabels={Automotive \& Aircraft,
Building Sector,ICT}]
\addplot[draw=blue,fill=blue!15]
coordinates {(42,1) (34.5,2) (42,3)};
\addlegendentry{\textsc{2016}}
\addplot[draw=blue,fill=blue!50]
coordinates {(16.5,1) (15,2) (11.25,3)};
\addlegendentry{\textsc{2006}}
\end{axis}
\end{tikzpicture}
提前感谢您的时间和帮助。第二个图形在 Paint 中被更改了。
答案1
图例的顺序与图的顺序相对应,可以通过选项反转reverse legend
。图例的位置可以通过 进行修改legend style={at={(x,y)}}
。请注意x
,y
是这里的标准化坐标。请参阅下面的代码。
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
[ xbar,
bar width=10pt,area legend,legend pos=south east,
reverse legend,
legend style={cells={anchor=west},at={(0.99,0.42)}}, area legend,
tick align=outside,xlabel=\textbf{EUR Billion},
ytick={1,...,3},
yticklabel style={text width=3.3cm,align=right},
yticklabels={Automotive \& Aircraft,
Building Sector,ICT}]
\addplot[draw=blue,fill=blue!15]
coordinates {(42,1) (34.5,2) (42,3)};
\addlegendentry{\textsc{2016}}
\addplot[draw=blue,fill=blue!50]
coordinates {(16.5,1) (15,2) (11.25,3)};
\addlegendentry{\textsc{2006}}
\end{axis}
\end{tikzpicture}
\end{document}