# Automatically set axis limits making them surpass all the drawings

The main idea behind this post is getting adapted tikzpicture xy axis. I mean, axis that surpass all the points drew (or pretended to be drew).

In my initial tikzpicture, the limit of the drawings is determined by the axis' lenghts. And I solved that using clip = false. I found that in this post. It seems that clip = false is something essential.

Even so, axis doesn't reach the limit of the drawings. I was looking for a shorter x+ axis (closer to the drawings right limit, 6), longer y+ axis (surpassing the top of the triangle a little bit: drawings upper limit), longer x- axis (surpassing circumference) and shorter y- axis (same reason than x+ axis).

In this post, I've read in the comments that "you do not have to set an ymax value. If you do not set it, pgfplots will make sure that all data points are shown". Also, in that comments it is said that using \addplot will solve the axis problem. I decided to do a few verifications: adding a drawing using \addplot and erasing xmin, xmax, ymin, ymax. This is the result:

Image 3 is simply a disaster. Image 4 doesn't have any difference with Image 2, \addplot didn't solve the axis-adapt problem.

In my opinion, xmin, xmax, ymin, ymax are the problem here. I'd like to draw multiple pictures based on parameters (which I've learned recently: own post) so modifying xmin, xmax, ymin, ymax is not viable. Keeping that in mind, I gave another try:

Image 5 is so compressed. I thought it was due to axis equal because ymax is way higher than |xmin|, xmax, but, as you can see in Image 6, I was wrong.

Finally, I found this post: Set vertical axis limits above/beneath the maximum/minimum resp. value. It seemed to be exactly what I wanted, so I used enlarge x limits=0.3 (and y). But using it without xmin, xmax, ymin, ymax returned me Image 3 again. And using it with xmin, xmax, ymin, ymax is not an option, because tikzpictures in newcommand will have variable values.

So now, my question is: how do I manage to get my y+ axis above the 'ymax drawings' value' and the y- axis below the 'ymin drawings' value'? And the same idea on x axis.

My MWE (Original Image) is:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc,math}
\usetikzlibrary{shapes.misc}
\usepackage{pgfplots}

\usepackage{vmargin}
\setmargins{2.5cm}
{1.5cm}
{15.5cm}
{23.42cm}
{10pt}
{1cm}
{0pt}
{2cm}

\begin{document}

\newcommand{\example}[3]{
\tikzmath{
\mya = #1;
\myb = #2;
\myr = #3;
}
\begin{tikzpicture}
\begin{axis}[
%   clip = false,
axis lines* = middle,
axis equal,
xmin = -5, xmax= 8, ymin= -5, ymax = 7,
xtick = \empty, ytick = \empty,
extra x ticks={\mya, 2,6},
extra y ticks={\myb, -1}
]
\filldraw [red] (axis cs:\mya,\myb) circle (0.1);
\filldraw [red] (axis cs:0,-1) circle (0.1);
\draw[dashed, red] (\mya, \myb) -- (0, -1);
\draw[purple] (-3,-3) -- (2,2);
\filldraw [dashed, green, fill opacity=0.1] (axis cs:\mya,\myb) circle(\myr);
\filldraw [blue, fill opacity=0.2](2,0) -- (6,0) -- (4,10) -- (2,0);
\node [label={(a,b)}] at (\mya, \myb) {};
\end{axis}
\end{tikzpicture}
}

\example{-7}{4}{2}

\end{document}

• I don't understand what your problem is. Aug 21, 2020 at 21:39

I think \addplot does solve your problem, but you need to draw all the elements in your diagram with it, not just plot some random parabola.

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc,math}
\usetikzlibrary{shapes.misc}
\usepackage{pgfplots}

\usepackage{vmargin}
\setmargins{2.5cm}
{1.5cm}
{15.5cm}
{23.42cm}
{10pt}
{1cm}
{0pt}
{2cm}

\begin{document}

\newcommand{\example}[3]{
\tikzmath{
\mya = #1;
\myb = #2;
\myr = #3;
}
\begin{tikzpicture}
\begin{axis}[
axis lines* = middle,
axis equal,
xtick={\mya, 2,6},
ytick={\myb, -1}
]


• @Iborra Yes, I think so. If you wanted to make a bit more specific, you could perhaps add the word "Automatically" at the start, to emphasize that just adjusting xmin etc. manually isn't good enough. Aug 22, 2020 at 14:14