I'm having trouble plotting a secant function. I get Dimension too large error but I can't solve it.




                    [axis line style = very thick,
                    extra y ticks={-1,1},
                    extra x ticks={
                        -1.5708, %-pi/2
                        0, %0
                        1.5708, %pi/2
                        4.71239}, %3pi/2
                    extra x tick labels={
                    $-\frac{\pi}{2}$, %-pi/2
                    $0$, %0
                    $\frac{\pi}{2}$, %pi/2
                    $\frac{3\pi}{2}$}, %3pi/2
                    every extra x tick/.style={
                        xticklabel style={anchor=south west},
                        major grid style={thick,dashed,black}
                    axis lines = center,
                    \addplot [blue,domain=-pi/2:pi/2,ultra thick] {sec(deg(x))};
                        \addplot [blue, domain=pi/2:3*pi/2,ultra thick] {sec(deg(x))};
                        %\node at (axis cs:0.2, -0.28) {$O$} ;
                        %\node at (axis cs:3.14, -0.28) {$\pi$} ;
                    \addplot[only marks, color=orange] coordinates {(0,1)};
                    \addplot[only marks, color=orange] coordinates {(0,-1)};

I have read several posts but I can not find what is the problem that makes my code fail. Changing the domain of the function, everything works, but I need to paint that domain.

The error is:

<recently read> \pgf@yy 
l.59                 \end{axis}
I can't work with sizes bigger than about 19 feet.
Continue and I'll use the largest value I can.
  • 1
    Your function has a singularity at pi/2 (plus multiples of pi, but never mind). Change the domain of your plot to avoid a small neighborhood of the singular point.
    – mickep
    Aug 30, 2022 at 11:15
  • "Changing the domain of the function, everything works, but I need to paint that domain." Can you do this on paper? I'm guessing not. You have to avoid the singularity, or the plot would be, as pgf has said, too large. Just change your domain ever so slightly, like -1.5:1.5, to avoid the error. You'll get the same plot. Since you are trying to draw 200 points in this domain, the points will be virtually the same (at least in the visual sense). Aug 31, 2022 at 22:19

1 Answer 1


You can use restrict y to domain like this:

\documentclass[tikz, border=1cm]{standalone}
axis line style=very thick,
xmin=-2, xmax=5.5,
ymin=-4, ymax=4,
xtick={-pi/2, 0, pi/2, 3*pi/2},
xticklabels={$-\frac{\pi}{2}$, 0, $\frac{\pi}{2}$, $\frac{3\pi}{2}$}, 
xticklabel style={anchor=north west},
major grid style={thick, dashed, black},
axis lines=center,
xlabel=$x$, ylabel=$y$,
restrict y to domain=-10:10,
\addplot[blue, domain=-pi/2:3*pi/2, ultra thick, samples=100, smooth] {sec(deg(x))};
\addplot[only marks, color=orange, forget plot] coordinates {(0,1) (0,-1)};

Graph of trig function sec

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .