2

By default pgfplots has a nasty habit of cutting peaks off enter image description here Adding enlarge y limits={abs=0.6pt} seems to fix this enter image description here But now I have an unsightly stub at the ends of my axis

enter image description here

I'd rather like to get rid of this, but can't see how. If someone could point my in the right direction that would be very much appriciated :)

MWE

\documentclass{article}

\usepackage{pgfplots}

\pgfplotsset{
    compat=newest,
    no marks,
    axis lines=middle,
    axis on top,
    every axis plot/.append style={very thick},
    enlarge y limits={abs=0.6pt}, % prevent clipping of plot lines
}

\begin{document}

\begin{center}
    \begin{tikzpicture}
        \begin{axis}[domain=0:pi]
            \addplot+{sin(deg(x))};
        \end{axis}
    \end{tikzpicture}
\end{center}

\end{document}

Issue with the clip=false solution

This was suggested, and does indeed seem to solve this issue, however it creates another. For asymptotic functions (e.g. changing to 1/x with ymax=5) then produce their own set of issues. Given that this is going inside a global setting, I'd rather not have to worry about this.

enter image description here

Issue with shorten inner axis line

When plotting some other functions (e.g. 1/(x+1)), the origin develops a 'gap'.

enter image description here enter image description here

8
  • Does enlarge y limits={abs=0.6pt, upper} work? Sep 28 '19 at 18:08
  • It would, however with something exact (line this sine graph) there's still a visual niggle (just at the top). Also, I'd rather (if possible) have this as a "set and forget" type thing, so I won't have to switch for graphs with a negative minimum, or positive maximum.
    – tecosaur
    Sep 28 '19 at 18:12
  • Could you add the code of that example to your question? Makes it easier to test. Sep 28 '19 at 18:15
  • Sure :) I'll cut it down a bit for practicality though
    – tecosaur
    Sep 28 '19 at 18:19
  • NB: For the visual niggle with the top, by exact I mean the maximum is exactly one of the tick-marks. I.e. for sine max value=1, so there's a little stub beyond that tick mark.
    – tecosaur
    Sep 28 '19 at 18:35
2

I don't know if this will work for all cases, but possibly the combination

    enlargelimits={abs=0.6pt},
    inner axis line style={shorten >=0.6pt, shorten <=0.6pt}

will work. The first will extend the axis limits by a bit more than half the line width, which will also extend the axis lines. The second line will shorten the axis lines by the same amount.

\documentclass{article}

\usepackage{pgfplots}

\pgfplotsset{
    compat=newest,
    no marks,
    axis lines=middle,
    axis on top,
    every axis plot/.append style={very thick},
    enlargelimits={abs=0.6pt},
    inner axis line style={shorten >=0.6pt, shorten <=0.6pt}
}

\begin{document}

\begin{center}
    \begin{tikzpicture}
        \begin{axis}[domain=0:pi]
            \addplot+{sin(deg(x))};
        \end{axis}
    \end{tikzpicture}
\end{center}

\end{document}
7
  • Seems to work! Same issue as my other question then, but this one seems to be solved!
    – tecosaur
    Sep 28 '19 at 18:58
  • @tecosaur Your second question is kind of a duplicate of this, yes. Sep 28 '19 at 18:59
  • I may have been better of combining them, I just wasn't sure if there'd have different enough solutions to be worth separating.
    – tecosaur
    Sep 28 '19 at 19:01
  • @tecosaur Well, depends a bit on how you look it. The question of how to set the line cap for plot lines is not a duplicate of this. But the line cap wasn't really the problem, the problem was the same as here: that pgfplots clips off everything outside the axis limits, and when the axis limits are the same as the data limits, about half the line width is cut off. Sep 28 '19 at 19:08
  • Good answer....with specific details...thererefore...upvote.
    – Sebastiano
    Sep 28 '19 at 19:43

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.