Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm currently experimenting tikz-pgf, but facing some strong behaviour.

  1. How is it possible to have only 1 legend for a groupplot?
  2. Why is the scale 10^(-3) appearing below the first plot?

Here is the MWE :

\documentclass{article}
\usepackage{tikz,pgfplots}
\usepgfplotslibrary{groupplots}

\begin{document}
\begin{tikzpicture}
\begin{groupplot}[group style={
        group name=my plots,
        group size=1 by 2,
        xlabels at=edge bottom,
        xticklabels at=edge bottom,
        vertical sep=0pt
    },enlarge x limits=false,height=3cm, width=.8\textwidth,grid=major,scaled x ticks=base 10:3,legend cell align=left,legend pos=outer north east]
  \nextgroupplot
  \addplot[domain=0:0.05,samples=200] {1.2*sin(2*pi*50*\x r)};
  \addplot[red,domain=0:0.05,samples=200] {0.8*sin(2*pi*50*(\x r-0.25))};
  \legend{U [V],I [A]};
  \nextgroupplot[xlabel={t [s]}]
  \addplot[green,domain=0:0.05,samples=200] {0.8*1.2*sin(2*pi*50*\x r)*sin(2*pi*50*(\x r-0.25))};
  \legend{S [VA]};
\end{groupplot}
\end{tikzpicture}
\end{document}

Here is an illustration of the output : illustration

share|improve this question
    
The 10^(-3) is indeed a bug in the groupplots library. As a workaround, add scaled x ticks=false to the first \nextgroupplot options. –  Jake Mar 5 '13 at 16:20
    
@Jake's comment is correct. And the request to get just one legend should probably become a feature request; the groupplots lib currently only supports separate legends per plot. –  Christian Feuersänger Mar 8 '13 at 19:39
    
Yep i adopted Jake's fix. the fact of having only one legend is purely aesthetic.. –  s__C Mar 8 '13 at 20:31
    
@Jake Would you write an answer or should ask for closing it as a bug report? –  percusse Mar 22 '13 at 14:51
add comment

1 Answer 1

up vote 2 down vote accepted
  1. To create a common legend, add all legend entries (using \addlegendimage) in one of the \nextgroupplots. To place it at an arbitrary position (e.g., centered horizontally or verticallr, which is IMHO the best option for a common legend), use legend to name and then create a node with a reference.
  2. To remove the ·10-3 mark, you can add xtick scale label code/.code={} option to the \nextgroupplot definition.

Please find attached an example:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\usepgfplotslibrary{groupplots}

\setlength{\parindent}{0pt}

\begin{document}
\begin{tikzpicture}
\begin{groupplot}[group style={
        group name=my plots,
        group size=1 by 2,
        xlabels at=edge bottom,
        xticklabels at=edge bottom,
        vertical sep=0pt
    }, enlarge x limits=false, height=3cm, width=.8\textwidth, grid=major,
    scaled x ticks=base 10:3, legend cell align=left]
  \nextgroupplot[xtick scale label code/.code={}, legend to name=fig:my plots:grouplegend]
  \addplot[domain=0:0.05,samples=200] {1.2*sin(2*pi*50*\x r)};
  \addplot[red,domain=0:0.05,samples=200] {0.8*sin(2*pi*50*(\x r-0.25))};
  \addlegendimage{green, line legend}
  \legend{U [V],I [A],S [VA]}
  \nextgroupplot[xlabel={t [s]}]
  \addplot[green,domain=0:0.05,samples=200]
    {0.8*1.2*sin(2*pi*50*\x r)*sin(2*pi*50*(\x r-0.25))};
\end{groupplot}
\node [right] at ($(my plots c1r1.east)!.5!(my plots c1r2.east)$)
    {\ref{fig:my plots:grouplegend}};
\end{tikzpicture}
\end{document} 

compiled code

If I may suggest something, it may be more readable if you scale the x ticks but inform the reader about it by prefixing the unit. You can do it by adding scaled x ticks=manual:{}{\pgfmathparse{#1*1000}}. In that case, no buggy ·10-3 marks appear:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\usepgfplotslibrary{groupplots}

\setlength{\parindent}{0pt}

\begin{document}
\begin{tikzpicture}
\begin{groupplot}[group style={
        group name=my plots 2,
        group size=1 by 2,
        xlabels at=edge bottom,
        xticklabels at=edge bottom,
        vertical sep=0pt
    }, enlarge x limits=false, height=3cm, width=.8\textwidth, grid=major,
    scaled x ticks=manual:{}{\pgfmathparse{#1*1000}}, legend cell align=left]
  \nextgroupplot[legend to name=fig:my plots 2:grouplegend]
  \addplot[domain=0:0.05,samples=200] {1.2*sin(2*pi*50*\x r)};
  \addplot[red,domain=0:0.05,samples=200] {0.8*sin(2*pi*50*(\x r-0.25))};
  \addlegendimage{green, line legend}
  \legend{U [V],I [A],S [VA]}
  \nextgroupplot[xlabel={t [ms]}]
  \addplot[green,domain=0:0.05,samples=200]
    {0.8*1.2*sin(2*pi*50*\x r)*sin(2*pi*50*(\x r-0.25))};
\end{groupplot}
\node [right] at ($(my plots 2 c1r1.east)!.5!(my plots 2 c1r2.east)$)
    {\ref{fig:my plots 2:grouplegend}};
\end{tikzpicture}
\end{document} 

compiled code

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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