2

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.

enter image description here

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:

enter image description here

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:

enter image description here

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}

\pgfplotsset{compat=newest}

\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}
    ]
%   \addplot[domain=-4:5, color=cyan]{x^2-2*x-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}
1
  • I don't understand what your problem is.
    – AndréC
    Aug 21 '20 at 21:39
2

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.

enter image description here

\documentclass{article}

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

\pgfplotsset{compat=newest}

\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}
    ]

    \addplot[red,mark=*,dashed, mark options={solid}] coordinates {(\mya,\myb)(0,-1)};
    \addplot[purple] coordinates{(-3,-3)(2,2)};
    \addplot[dashed, draw,fill,fill opacity=0.1,green,domain=0:360] ({\mya+cos(x)*\myr},{\myb+sin(x)*\myr});
    \addplot[blue, fill, fill opacity=0.2] coordinates {(2,0)(6,0)(4,10)(2,0)};
    \node [above] at (\mya, \myb) {(a,b)};
    \end{axis}
    \end{tikzpicture}
}

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

\end{document}
2
  • Do you think the title of this post fits with the problem I had? @Torbjørn T.
    – Á. Iborra
    Aug 22 '20 at 9:31
  • @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 '20 at 14:14

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.