1

I wanted to replicate the following graph in latex. I'm using the pgfplots package. The main areas I'm struggling with is how to implement vertical lines with labels, how to make the tick-marker on the y-axis be absolute (i.e. have no negatives) and how to present the legend below the figure. Also disregard any slope discrepancies, my data is not equivalent to that pictured.

Here is the code:

\begin{tikzpicture}
\begin{axis}[
    axis x line=middle,
    axis y line=middle,
    axis on top,
    smooth,
    xlabel = $x$,
    ylabel = {$f(x)$},
    width=0.5\textwidth,
    height=0.5\textwidth,
    every axis plot/.append style={ultra thick}
]
\addplot [
    domain=200:207, 
    samples=100, 
    color=red,
]
{80};
\addplot [
    domain=207:220, 
    samples=100, 
    color=red,
]
{-80/13*x+17600/13};
\addplot [
    domain=220:250, 
    samples=100, 
    color=red,
]
{0};
\addplot [
    domain=250:265, 
    samples=100, 
    color=red,
]
{-16/3*x+4000/3};
\end{axis}
\end{tikzpicture}

enter image description here

1

This makes the y ticks positive and adds the vertical lines. There are many ways you can make the output more appealing.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%axis lines=middle,
    axis x line=middle,
    axis y line=left,
    axis on top,
    smooth,
    xlabel = $x$,
    %ylabel = {$f(x)$},
    width=0.5\textwidth,
    height=0.5\textwidth,
    xmax=280,
    every axis plot/.append style={ultra thick},
    ytick={-80,-40,0,0,40,80},
    yticklabel=$\pgfmathparse{abs(\tick)}\pgfmathprintnumber{\pgfmathresult}\,\%$
]
\addplot [
    domain=200:207, 
    samples=100, 
    color=red,
]
{80};
\addplot [
    domain=207:220, 
    samples=100, 
    color=red,
]
{-80/13*x+17600/13};
\addplot [
    domain=220:250, 
    samples=100, 
    color=red,
]
{0};
\addplot [
    domain=250:265, 
    samples=100, 
    color=red,
]
{-16/3*x+4000/3};
\pgfplotsinvokeforeach{207,220,250,265}{\path (#1,0) coordinate (p-#1);}
\end{axis}
\foreach \X [count=\Y] in {207,220,250,265} 
{\draw (current axis.south-|p-\X) -- (current axis.north-|p-\X)
node[above,font=\sffamily]{v\Y};}
\end{tikzpicture}
\end{document}

enter image description here

  • This is great thanks. One small issue is I can't work out how I can make the top line intersect at the y-axis. Extending it extends the domain of the axes. But otherwise this is great. – Wertea May 25 at 15:15
  • @Wertea Just remove enlargelimits=0.1,. You can add xmax=280 instead. – marmot May 25 at 15:17
  • Ah right, thanks for that. One last thing I notice even though you include 0 in the y-tick, it doesn't actually display 0 on the axis, do I need to add something to include 0 on the y-axis? – Wertea May 25 at 15:23
  • @Wertea An ad hoc fix: ytick={-80,-40,0,0,40,80}, (use 0 twice). – marmot May 25 at 15:33
  • 1
    @Wertea On my machine it does. I added the full code. – marmot May 25 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.