32

I have a question regarding the alignment of the content of the legend. The default option is centering, but I want to have it aligned to the left. How can I change this?

\begin{figure}[H]
\begin{tikzpicture}
\begin{axis}[xbar stacked,
legend style={legend columns=1,at={(axis cs:350, 0.4)},anchor=south west,draw=none},
ytick={0,1,2,3,4,5,6,7},
axis y line*=left,
axis x line*=bottom,
tick label style={font=\footnotesize},
legend style={font=\footnotesize},
label style={font=\footnotesize, text left},
xtick={0,25,50,75,100,125,150,175,200,225,250,275,300,325,350,375,400,425,450,475,500},
width=15cm,
height=6cm,
bar width=3mm,
xlabel={Time in seconds},
ylabel={Pool sizes},
yticklabels={0,500,1000,1500,2000,3000,4000,5000},
xmin=0,
xmax=500,
area legend,
y=4mm,
enlarge y limits={abs=0.625},
]
\addplot[time RR,pattern=horizontal lines, pattern color = time RR] coordinates
% Time RR
{(0.00,0) (2.44,1) (8.48,2) (16.16,3) (22.26,4) (37.03, 5) (49.70,6) (68.45,7)};
\addplot[time Multi-Thread,pattern=north east lines, pattern color = time Multi-Thread] coordinates
% Time LS/SA
{(231.12,0) (223.00,1) (220.93,2) (219.75, 3) (222.66,4) (221.61,5) (222.67,6) (244.95,7)};
\addplot[time POOL,pattern=vertical lines, pattern color = time POOL] coordinates
% Time POOL
{(0.00, 0) (10.01,1) (25.65,2) (41.51,3) (57.02,4) (87.82,5) (121.88,6) (160.44,7)};
\addplot[Overhead time,pattern=grid, pattern color = Overhead time] coordinates
% Else
{(0.27,0) (0.15,1) (0.14,2) (0.14,3) (0.15,4) (0.15,5) (0.15,6) (0.17,7)};
\legend{time RR,time Multi-Thread,time POOL,Overhead time}
\end{axis}
\end{tikzpicture}
\caption{Analysis of computational time on the sizes of $POOL$}
\label{fig: Time division}
\end{figure}

enter image description here

2
  • You might want to post this question on the TeX - LaTeX site where the latex experts are.
    – Pang
    Jun 30 '16 at 2:00
  • Welcome to TeX.SE! Please expand you code sniped to complete document which can be copied to our computers and compile. Probably you need add align=left to legend style.
    – Zarko
    Jun 30 '16 at 10:15
52

Answer : add legend cell align={left} to the options of your axis environment:

\begin{axis}[
   ... ,
legend cell align={left},
]
{
   ...
}
\end{axis}

User code

The following code is a minimal working example:

\documentclass[]{article}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    title=Left-align Legend with pgfplots,
    xlabel={$x$},
    ylabel={$y$},
    legend cell align={left}, % The command for legend alignment
    legend entries={$y = x^2$,Tangent at $x=1$}
]

\addplot[color=blue,domain=0:2]{x^2};
\addplot[domain=0:2]{2*x-1};

\end{axis}
\end{tikzpicture}
\end{document}

which produces the following output when compiled with pdflatex (and all the required packages):

Minimal working example

1
  • Welcome to TeX.SX!! Rather than giving code fragments it is better to post a full minimal working example. Currently we have to guess what you packages etc you are using, which makes it is difficult reconstruct, and hence fix. A MWE should start with a \documentclass command, have a minimal preamble and then \begin{document}...\end{document}. The code should compile and be as small as possible to demonstrate your problem. This makes it much easier for people to help you --- and much more likely that they will!
    – user30471
    Jul 28 '16 at 21:58

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.