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 have functions which have two intervals, a linear and non-linear. I want to have these inside the command \addplot in pgfplots, to get only one legend. I tried it with them in a separated write, but the legend output recognise each one as different function.

Basically, I have two functions to graph, and the legend have to depict that.

Please, help me.

I wrote in the preamble: \usepackage{tikz} and \usepackage{pgfplots}.

\begin{figure}[!h]
\centering
\caption{Espectro de pseudo-aceleraciones}
\begin{tikzpicture}[domain=0:5]
\begin{axis}[xlabel={Periodo $T$ (s)},ylabel={$S_a$ ($\times$g)}, xmin=0,xmax=5.5,ymin=0,ymax=2,extra y ticks={.3,1.8}]
\addplot[domain=.6:5.5,color=red,very thick]{.18/\x};
\addplot[domain=0:.6,color=red,very thick]{.3};
\addlegendentry{Espectro Inelástico}
\addlegendentry{}

\addplot[domain=.6:5.5,color=red,very thick,dashed]{1.08/\x};
\addplot[domain=0:.6,color=red,very thick,dashed]{1.8};
\addlegendentry{Espectro Elástico}

\end{axis}

\end{tikzpicture}
\end{figure}
share|improve this question

2 Answers 2

up vote 7 down vote accepted

You can use forget plot:

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

\begin{document}

\begin{figure}
\centering
\caption{Espectro de pseudo-aceleraciones}
\begin{tikzpicture}[domain=0:5]
\begin{axis}[
  xlabel={Periodo $T$ (s)},
  ylabel={$S_a$ ($\times$g)}, 
  xmin=0,
  xmax=5.5,
  ymin=0,
  ymax=2,
  extra y ticks={.3,1.8}
  ]
  \addplot[domain=.6:5.5,color=red,very thick,forget plot]{.18/\x};
  \addplot[domain=0:.6,color=red,very thick]{.3};
  \addlegendentry{Espectro Inelástico}

  \addplot[domain=.6:5.5,color=red,very thick,dashed]{1.08/\x};
  \addplot[domain=0:.6,color=red,very thick,dashed]{1.8};
  \addlegendentry{Espectro Elástico}
\end{axis}
\end{tikzpicture}
\end{figure}

\end{document}

enter image description here

share|improve this answer

It sounds like you need the forget plot option, as detailed in pgfplots: prevent single plot from being listed in legend

screenshot

% arara: pdflatex
% !arara: indent: {overwrite: yes}
\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}[domain=0:5]
    \begin{axis}[xlabel={Periodo $T$ (s)},ylabel={$S_a$ ($\times$g)}, xmin=0,xmax=5.5,ymin=0,ymax=2,extra y ticks={.3,1.8}]
        \addplot[domain=.6:5.5,color=red,very thick]{.18/\x};
        \addlegendentry{Espectro Inelástico}
        \addplot[domain=0:.6,color=red,very thick,forget plot]{.3};
        \addplot[domain=.6:5.5,color=red,very thick,dashed]{1.08/\x};
        \addplot[domain=0:.6,color=red,very thick,dashed]{1.8};
        \addlegendentry{Espectro Elástico}
    \end{axis}
\end{tikzpicture}

\end{document}
share|improve this answer

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.