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 need to plot the constant y = pi function with the plot touching both sides of the plot.

I have had a search through the pgfplots manual without much luck. I have tried \addplot{3.141...} which produces a small line spanning very little of the x axis, and manually plotting points for the range of my graph.

share|improve this question
    
You can use the addplot command as you are currently doing. Just add enlargelimits=false as an option to your axis environments. –  Roelof Spijker Feb 7 '12 at 15:39
    
While code snippets are useful in explanations, it is always best to compose a fully compilable MWE that illustrates the problem. This would also ensure that the solutions provided would be compatible with the way you are using them. –  Peter Grill Feb 7 '12 at 16:11
    
How about \addplot expression[domain=-10:10]{pi}; ` –  cmhughes Feb 7 '12 at 16:40
add comment

2 Answers

up vote 2 down vote accepted

I see two ways to achieve what you want, depending on what you mean by "touching both sides of the plot".

You can add an extra y tick. Or, you can add a plot that is constant either by using coordinates or function. The coordinates one needs the xmin and xmax to be specified.

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[xlabel={$x$},
    ylabel={$\arctan(x)$},
    extra y ticks={0.785},
    extra y tick labels={$\pi/4$},
    extra y tick style={grid=major},
    minor y tick num=1,
    domain=-5:5,
    samples=100]
    \addplot+[no marks] function {atan(x)};
    \addplot+[no marks] function {pi/2};
    \addplot+[no marks] coordinates {(-5,pi) (5,pi)};
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
    
Just what I was after, Cheers. –  Jonathan Evans Feb 7 '12 at 18:23
add comment

You have either of the following two as an option:

\begin{axis}
  \begin{pgfplotsinterruptdatabb}
    \addplot[forget plot] coordinates {(-16000,3.14) (16000,3.14)};
  \end{pgfplotsinterruptdatabb}
\end{axis}

or

\begin{axis}
  \addplot[forget plot,update limits=false] coordinates {(-16000,3.14) (16000,3.14)};
\end{axis}

The first I would prefer. You can then easily do:

\newcommand\addplotpi[1][]{
  \begin{pgfplotsinterruptdatabb}
    \addplot[forget plot,#1] coordinates {(-16000,3.14) (16000,3.14)};
  \end{pgfplotsinterruptdatabb}
}

and simply use that in every axis environment you wish to utilize it in.

The forget plot allows you to add it in the beginning of an axis environment without disturbing the color-lists and legends.

The only thing you should worry about is the lower and upper limit of x. But just do it large enough so that it wont bother you! :)

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.