I need to draw a tangent to a graph at x=0, i found here a code it works perfectly with any x excepet for x=0 , i tried to find a solution but i didn't if any one can help i'll be very grateful . thanks in advance.

enter image description here

here is my code :

% used PGFPlots v1.13 and TikZ v3.0.1a
    \tikzset{label node/.style={#1}}
    % define style for the points
        inner sep=0pt,
        minimum size=3pt,
    add node at x/.style 2 args={
        name path global=plot line,
        /pgfplots/execute at end plot visualization/.append={
            \path [name path global = position line #1-1]
            ({axis cs:#1,0}|-{rel axis cs:0,0}) --
            ({axis cs:#1,0}|-{rel axis cs:0,1});
            \path [xshift=1pt, name path global = position line #1-2]
            ({axis cs:#1,0}|-{rel axis cs:0,0}) --
            ({axis cs:#1,0}|-{rel axis cs:0,1});
            \path [
            name intersections={
                of={plot line and position line #1-1},
                name=left intersection
            name intersections={
                of={plot line and position line #1-2},
                name=right intersection
            label node/.append style={pos=1}
            ] (left intersection-1) -- (right intersection-1)
            node [label node]{\nodetext};
            % ---------------------------------------------------------
            % draw the tangent line from a bit right of the point on
            % the curve to the intersection with the ordinate
            % and draw the corresponding points
            \draw [Tangent,line width=0.6pt, color=red] let
            \p1=($ (left intersection-1) - (right intersection-1) $),
            \p2=($ (left intersection-1)!sign(#1)*60mm!(right intersection-1) $),
            \p3=($ ({axis cs:0,0}) - (\p2) $),
            (\p2) -- +($ {\n1}*(\x1,\y1) $)
            %                            node [Point,fill=Tangent] (origin intersection) {}
            %                            node [Point,fill=Curve] at (left intersection-1) {}
            %                    % ----------
            %                    % draw the horizontal line at the curve intersection point
            %                    % plus the label above/below the line
            %                    \tikzmath{
                %                        coordinate \c1;
                %                        \c1=(left intersection-1) - (right intersection-1);
                %                        \slope=\cy1/\cx1*sign(#1);
                %                    }
            %                    \pgfmathsetmacro{\AboveBelow}{ \slope>0 ? "above" : "below" }
            %                    \draw [dotted]
            %                        ([xshift=sign(#1)*2.5mm] left intersection-1) --
            %                        (left intersection-1) --
            %                            node [\AboveBelow,node font=\scriptsize] {$f(x)$}
            %                        (left intersection-1 -| origin intersection) --
            %                        +($ sign(#1)*(-2.5mm,0) $)
            %                            coordinate [pos=0.5] (a)
            %                    ;
            %                    % draw the horizontal line at the ordinate intersection point
            %                    \draw [dotted] (origin intersection)
            %                        +($ sign(#1)*(-2.5mm,0) $) --
            %                        (origin intersection);
            %                    % draw vertical line left/right of the ordinate
            %                    \pgfmathsetmacro{\LeftRight}{ #1<0 ? "right" : "left" }
            %                    \draw [stealth-stealth] (origin intersection)
            %                        +($ sign(#1)*(-1.25mm,0) $) -- (a)
            %                            node [midway,\LeftRight,node font=\scriptsize] {$p$}
            %                    ;
            %                    % ---------------------------------------------------------

            \begin{axis}[legend style={at={(0.65,0.65)},anchor=north},scaled ticks=false,
                %   y tick label style={/pgf/number format/sci},
                every axis y label/.style={at={(ticklabel* cs:0.95)},anchor=west,},
                yticklabel style = {font=\scriptsize},
                xticklabel style = {font=\scriptsize},
                %xlabel=$t\ (s)$,
                %   ylabel=$\mathrm{\left[  {{I}_{2}} \right]  {{.10}^{-3}}\ mol/l}$
                x= 0.95cm,
                y=2.8 cm,
                xmin=0, xmax=10,
                ymin=0, ymax=2.2,
                %xtick distance=40
                minor xtick={0.5,1.5,...,9.5},
                minor ytick={0.1,0.2,...,2.2},
                %minor tick num=4,
                %ylabel={$s(t)$ },
                tangent/.style={add node at x={#1}{},},scale only axis,]
                \addplot [thick,draw=Curve,line width =1.2pt,color=black,tangent=0.0001] {2*(1-exp(-x))};
                \addplot [line width =0.4pt,fill=blue, fill opacity=0.2]coordinates {
                    (0, 1.9) 
                    (0, 2.1)
                    (10, 2.1)
                    (10, 1.9)  };
                \addplot[mark=*] coordinates {(3,1.9)} ;
                \addplot[mark=*] coordinates {(1,1.26)} ;

2 Answers 2


Using the tzplot package:

enter image description here



\tzfn[thick]\Fx[-.00999:10] % name path = Fx
% tangent

  • Thank u very much Mar 15, 2022 at 19:02
width=11cm, height=8cm,
xmin=0, xmax=10,
ymin=0, ymax=2.2,
xtick distance=1, ytick distance=0.5,
minor x tick num=1, minor y tick num=4,
\addplot[thick, domain=0:10, smooth] {2*(1-exp(-x))};
\draw plot[only marks, mark=*] coordinates { (3,{2*(1-exp(-3))}) (1,{2*(1-exp(-1))})};
\addplot[red,thick, domain=0:10, samples=2] {2*(exp(-0))*(x-0)+2*(1-exp(-0)};
\addplot[red,thick, domain=0:10, samples=2] {2*(exp(-1))*(x-1)+2*(1-exp(-1)};
\addplot[red,thick, domain=0:10, samples=2] {2*(exp(-3))*(x-3)+2*(1-exp(-3)};
\fill[blue, opacity=0.2] (0, 1.9) rectangle  (10, 2.1);

Graph with curve and tangents

  • Thank you very much for answering but i think you didn't understand well my question. My problem is not how to draw a tangent. I know how to find the expression as u did. But in many cases find the expression and drawing take a lot of time. So i wanted something that can do this task automatically. I found a code here and it works perfectly all what i have to do is to give it the x where i want the tangent to be drawn and it draws it. The only problem is that this code is not working if i give the value 0 to x. And i'm asking if any one can review the code and figure out why is that. Thanks Feb 21, 2022 at 10:30

You must log in to answer this question.

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