2

Here is an example of two generic grids, one made using the axis environment in tikz, and the other without:

\documentclass{article}
\usepackage{pgfplots}

\begin{document}

\begin{figure}

\centering

\begin{tikzpicture}[scale=1,line width=1pt]

\begin{axis}[
color= black, 
thick,
xmin=-4.9, 
xmax=4.9, 
ymin=-4.9, 
ymax=4.9, 
axis equal image, 
axis lines=middle, 
font=\scriptsize,
xtick distance=1,
ytick distance=1,
inner axis line style={stealth-stealth},
xlabel = {},
ylabel = {},
grid=major,
ticks=none
]

\end{axis}

\end{tikzpicture}

\end{figure}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{figure}

\centering

\begin{tikzpicture}
\draw[black, opacity=0.25] (-4.9,-4.9) grid (4.9,4.9);
\draw[<->,black,thick] (0,-5) -- (0,5);
\draw[<->,black,thick] (-5,0) -- (5,0);
\end{tikzpicture}

\end{figure}

\end{document}

And here is the output:

enter image description here

As we can see, the grid made using the axis environment is smaller than the one made without it. This grid ranged from (-5,5) on both the x and y axis, but if I increase this interval (say to like (-7,7)), the grid using axis will not change its overall size, while the grid made without it will. Here is an example:

enter image description here

My question is, is there a way to scale down the second one so that it will always be the same width, regardless of the axis interval, similar to the first? I know you could manually scale the axis down yourself, but the scaling factor will change depending on the interval of the axis, so that is undesirable.

EDIT

Here is something curious that happened to me when playing around with @marmot's answer below:

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{calc}

\begin{document}

%First "figure" with graphs 1 and 2 paired up
\begin{figure}[ht]

\centering

\begin{tikzpicture}[line width=1pt]

\begin{axis}[
scale=1,
color= black!50, 
thick,
xmin=-4.9, 
xmax=4.9, 
ymin=-4.9, 
ymax=4.9, 
axis equal image, 
axis lines=middle, 
font=\scriptsize,
xtick distance=1,
ytick distance=1,
inner axis line style={stealth-stealth},
xlabel = {},
ylabel = {},
grid=major,
ticks=none
]

\node[red,fill=white] at (0,0) {\textsc{Graph 1}}

\end{axis}

\path let \p1=($(current bounding box.east)-(current bounding box.west)$)
in \pgfextra{\xdef\mywidth{\x1}};

\end{tikzpicture} \hspace{1cm}
\ifdefined\myscale
\else
\typeout{Please\space recompile\space your\space file!}
\def\myscale{1}
\fi
\begin{tikzpicture}[scale=\myscale]

 \clip (-5,-5) rectangle (5,5);
 \draw[black, opacity=0.10] (-4.9,-4.9) grid (4.9,4.9);
 \draw[<->,black!25,-latex] (0,-4.9) -- (0,4.9);
 \draw[<->,black!25,-latex] (-4.9,0) -- (4.9,0);
 \node[red,fill=white] at (0,0) {\textsc{Graph 2}}

\path let \p1=($(current bounding box.east)-(current bounding box.west)$)
in \pgfextra{\pgfmathsetmacro{\myscale}{\mywidth/\x1}\xdef\myscale{\myscale}};
\makeatletter
\immediate\write\@mainaux{\xdef\string\myscale{\myscale}\relax}
\makeatother

\end{tikzpicture}

\end{figure}

%Second "figure" with graphs 3 and 4 paired up
\begin{figure}[ht]

\centering

\begin{tikzpicture}[line width=1pt]

\begin{axis}[scale=0.75,
color= black, 
thick,
xmin=-4.9, 
xmax=4.9, 
ymin=-4.9, 
ymax=4.9, 
axis equal image, 
axis lines=middle, 
font=\scriptsize,
xtick distance=1,
ytick distance=1,
inner axis line style={stealth-stealth},
xlabel = {},
ylabel = {},
grid=major,
ticks=none
]

\node[red,fill=white] at (0,0) {\textsc{Graph 3}};

\end{axis}
\path let \p1=($(current bounding box.east)-(current bounding box.west)$)
in \pgfextra{\xdef\mywidth{\x1}};
\end{tikzpicture}

\end{figure}
\begin{figure}[ht]

\centering
\ifdefined\myscale
\else
\typeout{Please\space recompile\space your\space file!}
\def\myscale{1}
\fi

\begin{tikzpicture}[scale=\myscale]
\draw[black, opacity=0.25] (-4.9,-4.9) grid (4.9,4.9);
\draw[<->,black,thick] (0,-5) -- (0,5);
\draw[<->,black,thick] (-5,0) -- (5,0);

\node[red,fill=white] at (0,0) {\textsc{Graph 4}};

\path let \p1=($(current bounding box.east)-(current bounding box.west)$)
in \pgfextra{\pgfmathsetmacro{\myscale}{\mywidth/\x1}\xdef\myscale{\myscale}};
\makeatletter
\immediate\write\@mainaux{\xdef\string\myscale{\myscale}\relax}
\makeatother

\end{tikzpicture}
\end{figure}

\end{document}

And here is the output:

enter image description here

Now, it was to my understanding that if the scale of graph 3 was changed, then the scale of graph 4 would change accordingly. However, that is not the case, graph 2 is actually the one to change!

1

This sets the width of the second tikzpicture equal to the one of the first. It uses methods from this post and is similar to this answer. The widths of both tikzpictures are measure and an appropriate scale factor is computed from their ratio. This scale factor is written in the aux file and read in the next run, which is why you need to run the file more than once. Notice that if you know/set the width of the first plot (you can set it with \pgfplotsset{width=...}) and xmin and xmax of the second, you could just compute the scale factor directly. This answer assumes you do not go this route.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{calc}
\begin{document}

\begin{figure}

\centering

\begin{tikzpicture}[scale=1,line width=1pt]

\begin{axis}[
color= black, 
thick,
xmin=-4.9, 
xmax=4.9, 
ymin=-4.9, 
ymax=4.9, 
axis equal image, 
axis lines=middle, 
font=\scriptsize,
xtick distance=1,
ytick distance=1,
inner axis line style={stealth-stealth},
xlabel = {},
ylabel = {},
grid=major,
ticks=none
]

\end{axis}
\path let \p1=($(current bounding box.east)-(current bounding box.west)$)
in \pgfextra{\xdef\mywidth{\x1}};
\end{tikzpicture}

\end{figure}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{figure}

\centering
\ifdefined\myscale
\else
\typeout{Please\space recompile\space your\space file!}
\def\myscale{1}
\fi
\begin{tikzpicture}[scale=\myscale]
\draw[black, opacity=0.25] (-4.9,-4.9) grid (4.9,4.9);
\draw[<->,black,thick] (0,-5) -- (0,5);
\draw[<->,black,thick] (-5,0) -- (5,0);
\path let \p1=($(current bounding box.east)-(current bounding box.west)$)
in \pgfextra{\pgfmathsetmacro{\myscale}{\mywidth/\x1}\xdef\myscale{\myscale}};
\makeatletter
\immediate\write\@mainaux{\xdef\string\myscale{\myscale}\relax}
\makeatother
\end{tikzpicture}
\end{figure}
\end{document}

enter image description here

This happens if I add scale=0.5 to the options of the axis on my machine.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{calc}
\begin{document}

\begin{figure}

\centering

\begin{tikzpicture}[line width=1pt]

\begin{axis}[scale=0.5,
color= black, 
thick,
xmin=-4.9, 
xmax=4.9, 
ymin=-4.9, 
ymax=4.9, 
axis equal image, 
axis lines=middle, 
font=\scriptsize,
xtick distance=1,
ytick distance=1,
inner axis line style={stealth-stealth},
xlabel = {},
ylabel = {},
grid=major,
ticks=none
]

\end{axis}
\path let \p1=($(current bounding box.east)-(current bounding box.west)$)
in \pgfextra{\xdef\mywidth{\x1}};
\end{tikzpicture}

\end{figure}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{figure}

\centering
\ifdefined\myscale
\else
\typeout{Please\space recompile\space your\space file!}
\def\myscale{1}
\fi
\begin{tikzpicture}[scale=\myscale]
\draw[black, opacity=0.25] (-4.9,-4.9) grid (4.9,4.9);
\draw[<->,black,thick] (0,-5) -- (0,5);
\draw[<->,black,thick] (-5,0) -- (5,0);
\path let \p1=($(current bounding box.east)-(current bounding box.west)$)
in \pgfextra{\pgfmathsetmacro{\myscale}{\mywidth/\x1}\xdef\myscale{\myscale}};
\makeatletter
\immediate\write\@mainaux{\xdef\string\myscale{\myscale}\relax}
\makeatother
\end{tikzpicture}
\end{figure}
\end{document}

enter image description here

Another response to your addendum: If you have two graphs, you need to use two independent scale factors. (And there were some ; missing in \node[red,fill=white] at (0,0) {\textsc{Graph 1}} and \node[red,fill=white] at (0,0) {\textsc{Graph 2}}.)

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\usetikzlibrary{calc}

\begin{document}

%First "figure" with graphs 1 and 2 paired up
\begin{figure}[ht]

\centering

\begin{tikzpicture}[line width=1pt]

\begin{axis}[
scale=1,
color= black!50, 
thick,
xmin=-4.9, 
xmax=4.9, 
ymin=-4.9, 
ymax=4.9, 
axis equal image, 
axis lines=middle, 
font=\scriptsize,
xtick distance=1,
ytick distance=1,
inner axis line style={stealth-stealth},
xlabel = {},
ylabel = {},
grid=major,
ticks=none
]

\node[red,fill=white] at (0,0) {\textsc{Graph 1}};

\end{axis}

\path let \p1=($(current bounding box.east)-(current bounding box.west)$)
in \pgfextra{\xdef\mywidth{\x1}};

\end{tikzpicture} \hspace{1cm}
\ifdefined\myscaleOne
\else
\typeout{Please\space recompile\space your\space file!}
\def\myscaleOne{1}
\fi
\begin{tikzpicture}[scale=\myscaleOne]

 \clip (-5,-5) rectangle (5,5);
 \draw[black, opacity=0.10] (-4.9,-4.9) grid (4.9,4.9);
 \draw[<->,black!25,-latex] (0,-4.9) -- (0,4.9);
 \draw[<->,black!25,-latex] (-4.9,0) -- (4.9,0);
 \node[red,fill=white] at (0,0) {\textsc{Graph 2}};

\path let \p1=($(current bounding box.east)-(current bounding box.west)$)
in \pgfextra{\pgfmathsetmacro{\myscaleOne}{\mywidth/\x1}\xdef\myscaleOne{\myscaleOne}};
\makeatletter
\immediate\write\@mainaux{\xdef\string\myscaleOne{\myscaleOne}\relax}
\makeatother

\end{tikzpicture}

\end{figure}

%Second "figure" with graphs 3 and 4 paired up
\begin{figure}[ht]

\centering

\begin{tikzpicture}[line width=1pt]

\begin{axis}[scale=0.75,
color= black, 
thick,
xmin=-4.9, 
xmax=4.9, 
ymin=-4.9, 
ymax=4.9, 
axis equal image, 
axis lines=middle, 
font=\scriptsize,
xtick distance=1,
ytick distance=1,
inner axis line style={stealth-stealth},
xlabel = {},
ylabel = {},
grid=major,
ticks=none
]

\node[red,fill=white] at (0,0) {\textsc{Graph 3}};

\end{axis}
\path let \p1=($(current bounding box.east)-(current bounding box.west)$)
in \pgfextra{\xdef\mywidth{\x1}};
\end{tikzpicture}

\end{figure}
\begin{figure}[ht]

\centering
\ifdefined\myscaleTwo
\else
\typeout{Please\space recompile\space your\space file!}
\def\myscaleTwo{1}
\fi

\begin{tikzpicture}[scale=\myscaleTwo]
\draw[black, opacity=0.25] (-4.9,-4.9) grid (4.9,4.9);
\draw[<->,black,thick] (0,-5) -- (0,5);
\draw[<->,black,thick] (-5,0) -- (5,0);

\node[red,fill=white] at (0,0) {\textsc{Graph 4}};

\path let \p1=($(current bounding box.east)-(current bounding box.west)$)
in
\pgfextra{\pgfmathsetmacro{\myscaleTwo}{\mywidth/\x1}\xdef\myscaleTwo{\myscaleTwo}};
\makeatletter
\immediate\write\@mainaux{\xdef\string\myscaleTwo{\myscaleTwo}\relax}
\makeatother

\end{tikzpicture}
\end{figure}

\end{document}

enter image description here

  • For some reason, the second grid does not scale with the first. So, for example, if I change scale=1 to scale=0.5, only the size of the first grid changes, and not the second. Although this does assure that the two are the same size when scale=1, which is honestly good enough for me, but I'm just curious as to why they both don't scale. – Aiden Kenny Mar 29 at 17:54
  • @AidenKenny It does scale if I put the scale information into the axis, e.g. \begin{axis}[scale=0.5,.... Notice that one should scale the axis, not the tikzpicture, because otherwise you can confuse pgfplots. There have been issues e.g. when filling some curves and scaling the tikzpicture instead of the axis. – marmot Mar 29 at 17:58
  • it still does not scale for me, even with the scale=0.5 inside the axis environment. – Aiden Kenny Mar 29 at 18:06
  • 1
    @AidenKenny I made another update. If you have two pictures you want to adjust, you need two independent scale factors. Once this is done, it does work on my machine as it should. (BTW, your code did not compile because of missing semicolons.) – marmot Mar 29 at 19:04
  • 1
    IT WORKS! Another fantastic job, marmot! – Aiden Kenny Mar 29 at 19:23
1

You could do something like this:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15} 
\xdef\defsize{4}
\xdef\cursize{5}

\newcommand\myscale[1]{\defsize/#1}

\begin{document}


\begin{figure}

\centering

\begin{tikzpicture}[scale=\myscale{7}]
\draw[black, opacity=0.25] (-7,-7) grid (7,7);
\draw[<->,black,thick] (0,-7) -- (0,7);
\draw[<->,black,thick] (-7,0) -- (7,0);
\end{tikzpicture}

\begin{tikzpicture}[scale=\myscale{3}]
\draw[black, opacity=0.25] (-3,-3) grid (3,3);
\draw[<->,black,thick] (0,-3) -- (0,3);
\draw[<->,black,thick] (-3,0) -- (3,0);
\end{tikzpicture}
\end{figure}

\end{document}

enter image description here

PS: This is just an idea for the examples in your code ... For more complex output that depends on other lengths and not just the known axis widths you should use the maximum widths in there... so, could possibly not work

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.