I would like to have something like
legend pos= west, %insted of north west, south east...
because I don't wanna place it in the corner but vertically in the middle.
How could I do this?
pgfplots
I would like to have something like
legend pos= west, %insted of north west, south east...
because I don't wanna place it in the corner but vertically in the middle.
How could I do this?
You could perhaps use the \addlegendimage
command, as in this discussion from the pgfplots-features mailing list.
An example, with a small \hspace
hack to place the title more centered in the legend box:
\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[legend pos=south east]
\addlegendimage{empty legend}
\addplot {sqrt(x)};
\addplot {ln(x)};
\addlegendentry{\hspace{-.6cm}\textbf{A title}}
\addlegendentry{$\sqrt{x}$}
\addlegendentry{$\ln{x}$}
\end{axis}
\end{tikzpicture}
\end{document}
For variety, here's a couple of more manual approaches. In each case the legend and title are separate entities, and the frame drawn afterwards.
\documentclass[border=5mm,tikz]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{fit}
\begin{document}
\begin{tikzpicture}
\begin{axis}[legend style={at={(rel axis cs:0.9,0.1)},above left,name=legend,draw=none}]
\addplot {sqrt(x)};
\addplot {ln(x)};
\addlegendentry{$\sqrt{x}$}
\addlegendentry{$\ln{x}$}
\end{axis}
\node [above,font=\bfseries] (legendtitle) at (legend.north) {Legend title};
\node [fit=(legendtitle)(legend),draw,inner sep=0pt] {};
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[legend style={draw=none,legend to name=leg}]
\addplot {sqrt(x)};
\addplot {ln(x)};
\addlegendentry{$\sqrt{x}$}
\addlegendentry{$\ln{x}$}
% place legend
\node [above left] (L) at (rel axis cs:0.9,0.1) {\ref{leg}};
% Add title
\node [above,font=\bfseries] (LT) at (L.north) {Legend title};
% if needed, add frame
\node [fit=(L)(LT),draw,inner sep=0pt] {};
\end{axis}
\end{tikzpicture}
\end{document}
Regarding your first question:
How can I configure the legend such that it occupies a prescribed amount of space like the width of the figure?
To my knowledge, this is impossible by means of a PGF \matrix
-- and these kind of legends are a PGF \matrix
. As considered in your question, the remaining option would be to \label
every plot and generate a legend manually using \ref
to each plot, for example using
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\def\INNERSEP{2pt}
\def\WIDTH{12cm}
\begin{axis}[
ybar,
height=6cm,
title={Some plot},
width=\WIDTH,
scale only axis, % <------------
axis on top,
y axis line style={opacity=0},
axis x line*=bottom,
tick align=inside,
tick style={draw=none},
xtick=data,
ymajorgrids,
major grid style={draw=white},
xtick={1,2,3,4},
legend image code/.code={%
\draw[#1, draw=none] (0cm,-0.1cm) rectangle (0.6cm,0.1cm);
},
extra description/.code={
\node[
draw,
text depth=0pt,
at={(0.0,-0.15)},
anchor=north west,
text width=\WIDTH-2*\INNERSEP,
inner sep=\INNERSEP,
outer sep=0pt,
] {
% Some TeX/LaTeX construct which constructs
% the legend:
\hbox to \linewidth{%
\textbf{Legend:}
\ref{A} Legend a \hfill \ref{B} Legend b \hfill
}%
};
},
]
\addplot coordinates {
(1, 0.2064)
(2, 0.3961)
(3, 0.5961)
(4, 0.7961)};
\label{A}
\addplot coordinates {
(1, 0.3064)
(2, 0.4961)
(3, 0.6961)
(4, 0.8961)};
\label{B}
\end{axis}
\end{tikzpicture}
\end{document}
Regarding your second question:
How can I recondigure the appearance of the small images for each plot?
As pointed out in some comment, the small images shown in legends can be reconfigured using legend image code
. A suitable substitute for the small bars might be to use some rectangles (as in your edit).
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar,
height=6cm,
title={Some plot},
width=12cm,
axis on top,
y axis line style={opacity=0},
axis x line*=bottom,
tick align=inside,
tick style={draw=none},
xtick=data,
ymajorgrids,
major grid style={draw=white},
xtick={1,2,3,4},
legend image code/.code={%
\draw[#1, draw=none] (0cm,-0.1cm) rectangle (0.6cm,0.1cm);
},
legend style={
draw=none, % ?
text depth=0pt,
at={(0.0,-0.15)},
anchor=north west,
legend columns=-1,
% default spacing:
column sep=1cm,
% The text "Legend:"
/tikz/column 2/.style={column sep=0pt,font=\bfseries},
%
% the space between legend image and text:
/tikz/every odd column/.append style={column sep=0cm},
},
]
\addlegendimage{empty legend}
\addlegendentry{\textbf{Legend:}}
\addplot coordinates {
(1, 0.2064)
(2, 0.3961)
(3, 0.5961)
(4, 0.7961)}; \addlegendentry{legend a}
\addplot coordinates {
(1, 0.3064)
(2, 0.4961)
(3, 0.6961)
(4, 0.8961)}; \addlegendentry{lengend b}
\end{axis}
\end{tikzpicture}
\end{document}
Regarding the third question:
How can I reconfigure the legend such that it starts right next to the text 'Legend:'?
You can distribute the column sep
such that it does not apply to your first artificial legend entry if you configure /tikz/column 2/.style
. I did that in the example above.
You will note that I also added draw=none
due to the lacking support for your first question.
Best Answer
legend pos=south west
is equivalent toSo by analogy we can make a version anchored west by using