2

Does someone know how can i change the position of my legend to right-side of my graph? I try many things but i can't. Please give me some help! Here is my code!

\resizebox{\textwidth}{!}{
\begin{minipage}{1.3\textwidth}
\begin{center}
\begin{tikzpicture}

\begin{axis}[ /pgf/number format/.cd, use comma, 1000 sep={}, scale only axis, xmin=2006,xmax=2012, ymin=65, ymax=125, axis y line*=left, xlabel=Year, ylabel=\color{black}$Percentage(\%)$]
    \addplot[black] coordinates {
    %Government Debt
(2006, 68.3608)
(2007, 68.0186)
(2008, 74.963)
(2009, 89.5022)
(2010, 102.7844)
(2011, 114.9962)
(2012, 121.9426)};
\end{axis}
%
\begin{axis}[ scale only axis, xmin=2006,xmax=2012, ymin=-15, ymax=5, axis y line*=right, axis x line=none, ylabel=\color{blue}$Percentage (\%)$]%
\addlegendimage{/pgfplots/refstyle=Hplot}
\addlegendentry{$Debt$}
    \addplot[blue, mark=+] coordinates {
    %Total Budget Balance
    (2006, -1.5862)
(2007, -1.9186)
(2008, -5.624)
(2009, -9.7638)
(2010, -13.0882)
(2011, -8.0618)
(2012, -6.4806)};
\addlegendentry{$TBB$}
%
    \addplot[blue,mark=o] coordinates {
%Real per capita GDP growth rate
(2006, 3.7266)
(2007, 3.3016)
(2008, -0.5192)
(2009, -4.1474)
(2010, -0.4744)
(2011, -1.2876)
(2012, -2.4802)    
    };
\addlegendentry{$GDPpc GR$}
\end{axis}
\end{tikzpicture}
\end{center}
\end{minipage}}

My graph

4
1

As darthbith says, in general you would add legend pos=outer north east to place the legend outside the axis on the top right. In this case it will overlap with the top ticklabel on the right y-axis. A simple fix for that here is to also add legend style={xshift=1em}, thereby shifting it to the right a bit.

Other changes I made:

  • On the second axis, change from axis y line*=right to axis y line=right. The latter also makes the ylabel move to the right side, which I would think you want, considering that you changed the colour of it.
  • Instead of \color{blue}, I added ylabel style={blue}. Not a big deal I think, mostly a matter of preference.
  • Don't write the labels and legend entries in math mode, unless they're actual mathematical variables. Percentage and Debt for example are text, and should not be written as math.

enter image description here

\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}

\begin{axis}[
  /pgf/number format/.cd, use comma,
  1000 sep={},
  scale only axis, 
  xmin=2006,
  xmax=2012, 
  ymin=65,
  ymax=125,
  axis y line*=left,
  xlabel=Year,
  ylabel=Percentage (\%)]

\addplot[black] coordinates {
    %Government Debt
(2006, 68.3608)
(2007, 68.0186)
(2008, 74.963)
(2009, 89.5022)
(2010, 102.7844)
(2011, 114.9962)
(2012, 121.9426)};
\end{axis}
%
\begin{axis}[
  scale only axis,
  xmin=2006,
  xmax=2012,
  ymin=-15,
  ymax=5,
  axis y line=right,
  axis x line=none,
  ylabel=Percentage (\%),
  ylabel style={blue},
  yticklabel style={blue},
  legend pos=outer north east,
  legend style={xshift=1em}]%
\addlegendimage{/pgfplots/refstyle=Hplot}
\addlegendentry{Debt}

\addplot[blue, mark=+] coordinates {
    %Total Budget Balance
    (2006, -1.5862)
(2007, -1.9186)
(2008, -5.624)
(2009, -9.7638)
(2010, -13.0882)
(2011, -8.0618)
(2012, -6.4806)};
\addlegendentry{TBB}
%
\addplot[blue,mark=o] coordinates {
%Real per capita GDP growth rate
(2006, 3.7266)
(2007, 3.3016)
(2008, -0.5192)
(2009, -4.1474)
(2010, -0.4744)
(2011, -1.2876)
(2012, -2.4802)    
    };
\addlegendentry{GDPpc GR}
\end{axis}
\end{tikzpicture}
\end{document}

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.