1

This is fairly simple. I want to expand the bounding box of my pgfplots without using enlargelimits or clip=false.

Why?

See Prevent pgfplot line clipping without extending axis

Why not enlargelimits?

Because I'd like it when there's a 'neat' domain for the tick marks to work out 'neatly' too.

the uglyness

MWE

\documentclass{article}

\usepackage{pgfplots}

\pgfplotsset{
    no marks,axis lines=middle,
    enlargelimits={abs=1pt},
    minor x tick num=1,
    every x tick/.style={color=black, thin},
    every y tick/.style={color=black, thin},
}

\begin{document}

\begin{center}
    \begin{tikzpicture}
        \begin{axis}[domain=1:2]
            \addplot+{x};
        \end{axis}
    \end{tikzpicture}
\end{center}

\end{document}

Some clarifications

I like to thicken the line, by default this causes issues with clipping (see the linked question). enlargelimits fixes this, but brings it's own issue with the tick marks as mentioned earlier. What I want to see is something like below when it should be a 'clean' (i.e. nice numbers) plot, e.g. y=x with domain=1:3 should produce something more like this

better

while still expanding the clipping bounding box.

I hope this makes things clearer.

A longer version (sorry)

To provide a fuller picture of the issue, here's a bigger example.

With enlargelimits (issues circled)

image3

Without enlargelimits (issues circled)

enter image description here

MWE 2

\documentclass{article}

\usepackage{pgfplots}
\usetikzlibrary{arrows.meta,calc}

\pgfplotsset{
    compat=newest,
    no marks,
    % -- AXIS
    axis lines=middle,
    axis on top,
    % -- AXIS ARROWS
    inner axis line style={draw=none},
    clip mode=individual,
    execute at end axis={
        \path[tips=proper] let \p1=($(rel axis cs:1,1)-(current axis.origin)$),
        \p2=($(current axis.origin)-(rel axis cs:0,0)$)
        in %\pgfextra{\typeout{\x1,\x2,\y1,\y2}}
        \ifdim\x1>1.5pt
            (current axis.origin) edge[-{Latex[round,length=\ifdim\x1<3mm 1.2mm\else 2mm\fi]}]
            ($({rel axis cs:1,0} |- {current axis.origin})+($(1.36mm,0)-(0,0)$)$)
        \fi
        \ifdim\x2>1.5pt
            (current axis.origin) edge[-{Latex[round,length=\ifdim\x2<3mm 1.2mm\else 2mm\fi]}]
            ($({rel axis cs:0,0} |- {current axis.origin})+($(-1.36mm,0)-(0,0)$)$)
        \fi
        \ifdim\y1>1.5pt
            (current axis.origin) edge[-{Latex[round,length=\ifdim\y1<3mm 1.2mm\else 2mm\fi]}]
            ($({rel axis cs:0,1} -| {current axis.origin})+($(0,1.36mm)-(0,0)$)$)
        \fi
        \ifdim\y2>1.5pt
            (current axis.origin) edge[-{Latex[round,length=\ifdim\y2<3mm 1.2mm\else 2mm\fi]}]
            ($({rel axis cs:0,0} -| {current axis.origin})+($(0,-1.36mm)-(0,0)$)$)
        \fi;
    },
    % % -- TICKS
    minor x tick num=1,
    every x tick/.style={color=black, thin},
    every y tick/.style={color=black, thin},
    tick align=outside,
    %
    every axis plot/.append style={
        very thick,
        line cap=round
    },
    samples=100,
    enlargelimits={abs=1pt}, % prevent clipping of plot lines
    % inner axis line style={shorten >=0.6pt, shorten <=0.6pt},
}

\begin{document}

\begin{center}
    \begin{tikzpicture}
        \begin{axis}[domain=1:2,small]
            \addplot+{x};
        \end{axis}
    \end{tikzpicture}
    \begin{tikzpicture}
        \begin{axis}[domain=1:2,small]
            \addplot+{x^2/2-x};
        \end{axis}
    \end{tikzpicture}
\end{center}

\end{document}
6
  • Do my clarifications help?
    – tecosaur
    Dec 7 '19 at 18:41
  • Not really. I get your target output with axis lines=left, and by dropping enlargelimits={abs=1pt},.
    – user194703
    Dec 7 '19 at 18:53
  • Sorry about that. While that does solve that specific solution, I'm looking for a general approach (i.e. axis still go through (0,0) if the domain is -1:1) which expands the clipping box without causing the issues of enlargelimits. Might it be worth emailing the package author to ask if such a thing is possible?
    – tecosaur
    Dec 8 '19 at 4:49
  • Ah, I see what you mean. Does the new "A longer version (sorry)" help?
    – tecosaur
    Dec 8 '19 at 5:24
  • Thanks! (It is very late here but now this looks like a very good question to me, +1)
    – user194703
    Dec 8 '19 at 5:26

Your Answer

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

Browse other questions tagged or ask your own question.