# How to align bars with the legend?

I'm able to draw bar chart using tex file mentioned below

\documentclass[landscape,a3paper,11pt]{article}
\usepackage{pgfplots, pgfplotstable}
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
ybar = 0.6,
ymajorgrids = true,
width=0.45\textwidth,
height=0.4\textheight,
enlarge y limits={upper, value=0.2},
ymin=0,
enlarge x limits = 0.2,
bar width=32pt,
title={ SCORE},
legend style={at={(0.5,-0.15)},
anchor=north,legend columns=0},
ylabel={Marks},
symbolic x coords={Topic},
xtick=data,
nodes near coords,
axis lines*=left,
y axis line style={opacity=0},
yticklabels={\empty},
ytick style={draw=none},
cycle list={
{fill=black!90,draw=black!90},
{fill=black!70,draw=black!70},
{fill=black!50,draw=black!50},
{fill=black!30,draw=black!30}
},
axis on top,
major grid style=white,
ymajorgrids,
legend style={draw=none,/tikz/every even column/.append style={column sep=0.5cm}}
%nodes near coords align={horizontal},
]
\legend{My Marks,Avg Marks,Topper Marks,Total Marks}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}


I want to align legend with the Bar as highlighted in RED color mentioned in the diagram below:

Can you please suggest how to add spaces between bars, to ensure it's aligned with the legend.

• If you're going to align the legend entries with the columns, wouldn't it be better to skip the legend entirely and label the columns directly? Do you have a particular reason for wanting to use a legend? – Jake Nov 5 '13 at 15:35
• @Jake; Thanks for your suggestion. I liked the presentation of Legend , so was looking for option to add space. Do you've any reference for alternate option? – Pawan Mude Nov 5 '13 at 15:53

Don't suppose you're still working with this one, but in case anyone would be interested in a (not very convenient) way of doing this: one can add a custom value for bar shift for each plot. I did that by modifying the cycle list.

If you'd prefer even spacing, remove all the bar shifts from the cycle list, and use ybar=1cm (or some other length) instead of ybar = 0.6.

\documentclass[landscape,a3paper,11pt]{article}
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{axis}[
ybar = 0.6,
ymajorgrids = true,
width=1.2\textwidth,
height=0.4\textheight,
enlarge y limits={upper, value=0.2},
ymin=0,
bar width=32pt,
title={ SCORE},
legend style={at={(0.5,-0.2)},
anchor=north,legend columns=0},
ylabel={Marks},
symbolic x coords={Topic},
xtick=data,
nodes near coords,
axis lines*=left,
y axis line style={opacity=0},
yticklabels={\empty},
ytick style={draw=none},
cycle list={
{fill=black!90,draw=black!90,bar shift=-4.5cm,},
{fill=black!70,draw=black!70,bar shift=-2cm,},
{fill=black!50,draw=black!50,bar shift=1cm,},
{fill=black!30,draw=black!30,bar shift=4cm,}
},
axis on top,
major grid style=white,
ymajorgrids,
legend style={draw=none,/tikz/every even column/.append style={column sep=0.5cm}}
%nodes near coords align={horizontal},
]
\legend{My Marks,Avg Marks,Topper Marks,Total Marks}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}


And just for the heck of it, a completely different approach:

\documentclass[landscape,a3paper,11pt]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usepgfplotslibrary{groupplots}
\begin{document}
\begin{center}
\begin{tikzpicture}
\begin{groupplot}[
group style={
group size=4 by 1,
group name=G,
},
ybar,
every axis/.append style={bar width=32pt},
width=0.28\textwidth,
height=0.4\textheight,
ymin=0,
ymax=80, % set same ymax for all groupplots
symbolic x coords={Topic},
xtick=data,
xticklabels={},
yticklabels={},
ytick style={draw=none},
ymajorgrids,
nodes near coords,
axis lines*=left,
axis line style={opacity=0},
cycle list={
{fill=black!90,draw=black!90},
{fill=black!70,draw=black!70},
{fill=black!50,draw=black!50},
{fill=black!30,draw=black!30}
},
axis on top,
major grid style=white,
legend style={
at={(0.5,-0.2)},
anchor=north,
draw=none
}
]
\nextgroupplot[ylabel={Marks}]
\legend{My Marks}

\nextgroupplot
\pgfplotsset{cycle list shift=1}
\legend{Avg Marks}

\nextgroupplot
\pgfplotsset{cycle list shift=2}