下面是两个通用网格的示例,一个使用axis
中的环境制作tikz
,另一个则没有:
\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}
输出如下:
我们可以看到,使用该axis
环境创建的网格比未使用该环境创建的网格要小。该网格在 x 轴和 y 轴上的范围均为 (-5,5),但如果我增加该区间(例如增加到 (-7,7)),使用该环境创建的网格axis
的整体大小不会改变,而未使用该环境创建的网格则会改变。以下是示例:
我的问题是,有没有办法缩小第二个,以便它始终具有相同的宽度,而不管轴间隔如何,与第一个类似?我知道您可以手动缩小轴,但缩放因子将根据轴的间隔而变化,因此这是不可取的。
编辑
这是我在玩@marmot 的答案时发生的一些奇怪的事情:
\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}
输出如下:
现在,我的理解是,如果改变图 3 的比例,那么图 4 的比例也会随之改变。但事实并非如此,实际上要改变的是图 2!
答案1
这将第二个元素的宽度设置tikzpicture
为与第一个元素的宽度相等。它使用以下方法这个帖子类似于这个答案。测量两个 s 的宽度tikzpicture
,并根据它们的比率计算出适当的比例因子。此比例因子写入辅助文件中并在下次运行中读取,这就是您需要多次运行该文件的原因。请注意,如果您知道/设置了第一个图的宽度(您可以使用设置它\pgfplotsset{width=...}
)和xmin
第二xmax
个图的宽度,则可以直接计算比例因子。这个答案假设您不走这条路。
\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}
如果我在我的计算机上添加scale=0.5
选项,就会发生这种情况axis
。
\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}
对您的附录的另一个回应:如果您有两个图表,则需要使用两个独立的比例因子。(并且和;
中缺少一些。)\node[red,fill=white] at (0,0) {\textsc{Graph 1}}
\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}
答案2
你可以做这样的事情:
\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}
PS:这只是代码中示例的一个想法...对于依赖于其他长度而不仅仅是已知轴宽度的更复杂的输出,您应该使用其中的最大宽度......所以,可能不起作用