4

I've copied an example bar plot from the manual. I had a little trouble to tweak the style of the legend. Here is what I've got.

\begin{tikzpicture}
\begin{axis}[
x tick label style={/pgf/number format/1000 sep=},
ylabel=\emph{Population},
xlabel=\emph{years},
enlargelimits=0.15,
% legend style={at={(0.5,-0.15)},
% anchor=north,legend columns=-1},
ybar,
bar width=9pt,
]

\addplot [black!100!white,fill=black!100!white] coordinates {
% \addplot [black!20!black,fill=black!50!white] coordinates {
(1930,50e6) (1940,33e6)
(1950,40e6) (1960,50e6) (1970,70e6)
};
\addplot [black!30!white,fill=black!30!white] coordinates {
(1930,38e6) (1940,42e6)
(1950,43e6) (1960,45e6) (1970,65e6)
};
\legend{Far,Near}
\end{axis}
\end{tikzpicture}

The plot look likes this

enter image description here

However, I want the legend to be

enter image description here

7

You can use legend image code to create whatever legend image you want.

\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16} %<- you may want to add this
\begin{document}
\begin{tikzpicture}
\begin{axis}[
x tick label style={/pgf/number format/1000 sep=},
ylabel=\emph{Population},
xlabel=\emph{years},
enlargelimits=0.15,
% legend style={at={(0.5,-0.15)},
% anchor=north,legend columns=-1},
ybar,
bar width=9pt,
legend image code/.code={
        \draw [#1] (0cm,-0.1cm) rectangle (0.2cm,0.25cm); },
]

\addplot [black!100!white,fill=black!100!white] coordinates {
% \addplot [black!20!black,fill=black!50!white] coordinates {
(1930,50e6) (1940,33e6)
(1950,40e6) (1960,50e6) (1970,70e6)
};
\addplot [black!30!white,fill=black!30!white] coordinates {
(1930,38e6) (1940,42e6)
(1950,43e6) (1960,45e6) (1970,65e6)
};
\legend{Far,Near}
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

A possible pitfall is that you need to place legend image code/.code={...} after ybar, which also sets the legend style. And you may move the position of the legend such that it does not cover some of the bars.

\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16} %<- you may want to add this
\begin{document}
\begin{tikzpicture}
\begin{axis}[
x tick label style={/pgf/number format/1000 sep=},
ylabel=\emph{Population},
xlabel=\emph{years},
enlargelimits=0.15,
legend style={at={(0.5,0.95)},
 anchor=north,legend columns=-1},
ybar,
bar width=9pt,
legend image code/.code={
        \draw [#1] (0cm,-0.1cm) rectangle (0.2cm,0.25cm); },
]

\addplot [black!100!white,fill=black!100!white] coordinates {
% \addplot [black!20!black,fill=black!50!white] coordinates {
(1930,50e6) (1940,33e6)
(1950,40e6) (1960,50e6) (1970,70e6)
};
\addplot [black!30!white,fill=black!30!white] coordinates {
(1930,38e6) (1940,42e6)
(1950,43e6) (1960,45e6) (1970,65e6)
};
\legend{Far,Near}
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.