Search code examples
latexpdflatexoverleaf

Vertical line in 2d plot using tikzpicture


After plotting on overleaf the following right line

\begin{tikzpicture}
\begin{axis}[
    axis lines = left,
    xlabel = $x$,
    ylabel = {$f(x)$},
]
%Below the red parabola is defined
\addplot [
    domain=-10:10, 
    samples=100, 
    color=red,
]
{((6*x - 48)/8)};
\addlegendentry{$6x + 8y = 48$}

I need to add a vertical line for x = 6 on the same plot. Could you help me with that?


Solution

  • Having a look here, one possible solution is the following:

    \documentclass{standalone}
    \usepackage{tikz}
    \usepackage{pgfplots}
    
    \begin{document}
    \begin{tikzpicture}
    \begin{axis}[
        axis lines = left,
        xlabel = $x$,
        ylabel = {$f(x)$},
    ]
    %Below the red parabola is defined
    \addplot [
        domain=-10:10, 
        samples=100, 
        color=red,
    ]
    {((6*x - 48)/8)};
    
    %% This is the vertical line
    \addplot[thick, samples=50, smooth,domain=0:6,magenta] coordinates {(6,0)(6,-15)};
    
    \addlegendentry{$6x + 8y = 48$}
    % Added to the legend
    \addlegendentry{$x = 6$}
    \end{axis}
    \end{tikzpicture}
    
    \end{document}