我最近使用创建了一个特定图形pgfplots
,其代码如下:
\documentclass[12pt,fleqn,a4paper]{article}
\usepackage{amsmath,amsfonts,amssymb}
\usepackage[hang]{caption}
\usepackage{float}
\usepackage{pgfplots}
\usepackage[subrefformat=parens]{subcaption}
\usepackage{tikz}
\usetikzlibrary{%
arrows ,
calc ,
pgfplots.fillbetween,
positioning ,
}%
\usepackage[a4paper,textwidth=16cm,vscale=0.75,vcentering]{geometry}
% PGFPlot version compatibility settings
\pgfplotsset{compat=1.12}
\begin{document}
\begin{figure}[h!]
\centering
% Phase lag
\newcommand*{\Lag}{pi/2}
\begin{tikzpicture}
\begin{axis}[%
legend entries = {$x_{1}(t) = A\cos(\omega{t})$, $x_{2}(t) = A\cos(\omega{t} - \phi)$},
legend cell align = {left},
width = \textwidth,
view = {45}{30},
xlabel = $t$,
zlabel = $x(t)$,
xtick = {0, 1.57, 3.14, 4.71, 6.28},
ytick = \empty,
ztick = {-1, 1},
xticklabels = {$0$, $0.5\pi$, $\pi$, $1.5\pi$, $2\pi$},
zticklabels = {$-A$, $A$},
xmin = 0,
xmax = 2*pi,
ymin = -1.1,
ymax = 1.1,
zmin = -1.2,
zmax = 1.4,
axis lines = center,
axis line style = {->},
axis equal image = true,
hide y axis,
every axis x label/.style={%
at={(ticklabel* cs:1.02)},
anchor=west,
},
every axis z label/.style={%
at={(ticklabel* cs:1.02)},
anchor=south,
}]
\addplot3[%
domain=0:2*pi,
samples=100,
samples y=0,
color=black,
thick
]%
(x, 0, {cos(deg(x))});
\addplot3[%
domain=0:2*pi,
samples=100,
samples y=0,
color=black,
dashdotted,
thick
]%
(x, 0, {cos(deg(x - \Lag))});
\addplot3[%
domain=0:2*pi,
samples=100,
samples y=0,
color=black,
very thin
]%
(0, {cos(deg(x))}, {sin(deg(x))});
\addplot3[%
domain=0:2*pi,
samples=100,
samples y=0,
color=black,
very thin
]%
(\Lag, {cos(deg(x))}, {sin(deg(x))});
% Draw angle
\addplot3[%
domain=0.5*pi:pi,
samples=100,
samples y=0,
color=black
]%
(\Lag, {0.25*cos(deg(x))}, {0.25*sin(deg(x))});
\node at (axis cs:0.75*\Lag,0,0) {$\phi$};
% Clipping path A
\addplot3[%
name path=A,
draw=none,
domain=0.5*pi:pi,
samples=100,
samples y=0,
]%
(\Lag, {cos(deg(x))}, {sin(deg(x))});
% Clipping path B
\draw[%
name path=B,
->,
>=stealth
]%
(axis cs:\Lag,0,0) -- (axis cs:\Lag,-1,0);
\draw[%
dashed
]%
(axis cs:\Lag,0,0) -- (axis cs:\Lag,0,1);
\draw[%
<->,
>=stealth
]%
(axis cs:0,0,1) -- (axis cs:\Lag,0,1);
\draw[%
->,
>=stealth
]%
(axis cs:0,0,1) -- (axis cs:0,0,1);
\node at (axis cs:0.5*\Lag,0.75,1) {$t_{0} = \displaystyle\frac{\phi}{\omega}$};
% Fill area spanned by the angle \phi
\addplot[%
gray,
opacity=0.5
] fill between[%
of=A and B,
];
\end{axis}
\end{tikzpicture}
\vspace{-1.5cm}
\caption{Phase lag and time lag of a sinusoidal function relative to the cosine function.}
\label{fig:PLTLSF}
\end{figure}
\end{document}
单独编译该图或将其与多个副本一起编译成 PDF 文件是没有问题的。但是,问题在于,如果该图是\addplot3
第一个出现的图,即其代码位于其他所有图的代码之前,则该图会搞乱同一文档中使用的所有其他图的 x 轴标签的定位。否则,其自身的 x 轴标签就会被搞乱。在这两种情况下,编译器都会在日志中显示以下警告:
Package pgfplots Warning: the ticklabel anchor cannot be determined, the normal
vector -(-0.62286pt,0.78468pt) and the unit x vector (0.89452pt,-0.4472pt) are
almost parallel (abs(cos(angle)) = 0.90807pt)! on input line 227.
[warning /pgfplots/warning/ticklabel anchor undetermined]
例如,在下面的代码中,放置了上述图形后使用 的任意图形\addplot3
。因此,其自身的 x 轴标签的对齐预计会有错误:
\documentclass[12pt,fleqn,a4paper]{article}
\usepackage{amsmath,amsfonts,amssymb}
\usepackage[hang]{caption}
\usepackage{float}
\usepackage{pgfplots}
\usepackage[subrefformat=parens]{subcaption}
\usepackage{tikz}
\usetikzlibrary{%
arrows ,
calc ,
pgfplots.fillbetween,
positioning ,
}%
\usepackage[a4paper,textwidth=16cm,vscale=0.75,vcentering]{geometry}
% PGFPlot version compatibility settings
\pgfplotsset{compat=1.12}
\begin{document}
\begin{figure}[h!]
\centering
% Definition of plane
\newcommand*{\PointAx}{1}%
\newcommand*{\PointAy}{1}%
\newcommand*{\PointAz}{1}%
\newcommand*{\PointAc}{0}%
% Normal vector scaling
\newcommand*{\ScaleN}{1}%
% Endpoints of line segment spanned by the normal vector
\newcommand*{\ScaleB}{6}%
% Position of the label of plane
\newcommand*{\PointLx}{-0.9}%
\newcommand*{\PointLy}{-0.9}%
\begin{tikzpicture}
\begin{axis}[%
view = {115}{30},
xlabel = $x$,
ylabel = $y$,
zlabel = $z$,
xtick = \empty,
ytick = \empty,
ztick = \empty,
width = 15cm,
axis lines = center,
axis line style = {->, white!50!black},
axis equal = true,
]
% Equation of line
\coordinate (P1) at (axis cs: \ScaleB*\PointAx, \ScaleB*\PointAy, \ScaleB*\PointAz);
\coordinate (P2) at (axis cs:-\ScaleB*\PointAx, -\ScaleB*\PointAy, -\ScaleB*\PointAz);
\draw[white!25!green] (P1) -- (P2);
% Plane ax + by + cz = d <=> z = 1/c * (-ax - by + d)
\addplot3[%
fill = white!75!blue,
opacity = 0.5,
draw = blue
] coordinates {%
(2, 0, {(1/\PointAz)*(-(\PointAx)*2 - (\PointAy)*0 + \PointAc)})
(0, -2, {(1/\PointAz)*(-(\PointAx)*0 - (\PointAy)*(-2) + \PointAc)})
(-2, 0, {(1/\PointAz)*(-(\PointAx)*(-2) - (\PointAy)*0 + \PointAc)})
(0, 2, {(1/\PointAz)*(-(\PointAx)*0 - (\PointAy)*2 + \PointAc)})
(2, 0, {(1/\PointAz)*(-(\PointAx)*2 - (\PointAy)*0 + \PointAc)})
};
% Overpainting correction for the line and the z-axis
\coordinate (O) at (axis cs: 0, 0, 0);
\draw[white!25!green] (O) -- (P1);
\draw[->, white!50!black] (axis cs: 0, 0, \PointAc) -- (axis cs: 0, 0, \pgfkeysvalueof{/pgfplots/zmax});
% Specify normal vector coordinates
\coordinate (N) at (axis cs: \ScaleN*\PointAx, \ScaleN*\PointAy, \ScaleN*\PointAz);
% Draw vectors
\draw[->, >=stealth, thick] (O) -- (N);
% Draw labels
\draw (O) node [anchor=north] {$\boldsymbol{\mathbf{0}}$};
\draw (N) node [anchor=south] {$\boldsymbol{\mathbf{n}}$};
\draw (P1) node [anchor=south west] {$L$};
\draw ([xshift=-30mm] axis cs: \pgfkeysvalueof{/pgfplots/xmin}, \pgfkeysvalueof{/pgfplots/ymax}, \pgfkeysvalueof{/pgfplots/zmax}) node [anchor=north east] {$\mathbb{R}^{3}$};
\draw (axis cs: \PointLx, \PointLy, {(1/\PointAz)*(-(\PointAx)*\PointLx - (\PointAy)*\PointLy + \PointAc)}) node {$\Pi$};
\end{axis}
\end{tikzpicture}
\caption{Typical subspaces in $\mathbb{R}^{3}$---the entire space $\mathbb{R}^{3}$, the plane $\Pi$, the line $L$ spanned by the normal vector of $\Pi$, and the singleton consisting of the zero vector.}
\label{fig:subspaces_in_r3}
\end{figure}
\begin{figure}[h!]
\centering
% Phase lag
\newcommand*{\Lag}{pi/2}
\begin{tikzpicture}
\begin{axis}[%
legend entries = {$x_{1}(t) = A\cos(\omega{t})$, $x_{2}(t) = A\cos(\omega{t} - \phi)$},
legend cell align = {left},
width = \textwidth,
view = {45}{30},
xlabel = $t$,
zlabel = $x(t)$,
xtick = {0, 1.57, 3.14, 4.71, 6.28},
ytick = \empty,
ztick = {-1, 1},
xticklabels = {$0$, $0.5\pi$, $\pi$, $1.5\pi$, $2\pi$},
zticklabels = {$-A$, $A$},
xmin = 0,
xmax = 2*pi,
ymin = -1.1,
ymax = 1.1,
zmin = -1.2,
zmax = 1.4,
axis lines = center,
axis line style = {->},
axis equal image = true,
hide y axis,
every axis x label/.style={%
at={(ticklabel* cs:1.02)},
anchor=west,
},
every axis z label/.style={%
at={(ticklabel* cs:1.02)},
anchor=south,
}]
\addplot3[%
domain=0:2*pi,
samples=100,
samples y=0,
color=black,
thick
]%
(x, 0, {cos(deg(x))});
\addplot3[%
domain=0:2*pi,
samples=100,
samples y=0,
color=black,
dashdotted,
thick
]%
(x, 0, {cos(deg(x - \Lag))});
\addplot3[%
domain=0:2*pi,
samples=100,
samples y=0,
color=black,
very thin
]%
(0, {cos(deg(x))}, {sin(deg(x))});
\addplot3[%
domain=0:2*pi,
samples=100,
samples y=0,
color=black,
very thin
]%
(\Lag, {cos(deg(x))}, {sin(deg(x))});
% Draw angle
\addplot3[%
domain=0.5*pi:pi,
samples=100,
samples y=0,
color=black
]%
(\Lag, {0.25*cos(deg(x))}, {0.25*sin(deg(x))});
\node at (axis cs:0.75*\Lag,0,0) {$\phi$};
% Clipping path A
\addplot3[%
name path=A,
draw=none,
domain=0.5*pi:pi,
samples=100,
samples y=0,
]%
(\Lag, {cos(deg(x))}, {sin(deg(x))});
% Clipping path B
\draw[%
name path=B,
->,
>=stealth
]%
(axis cs:\Lag,0,0) -- (axis cs:\Lag,-1,0);
\draw[%
dashed
]%
(axis cs:\Lag,0,0) -- (axis cs:\Lag,0,1);
\draw[%
<->,
>=stealth
]%
(axis cs:0,0,1) -- (axis cs:\Lag,0,1);
\draw[%
->,
>=stealth
]%
(axis cs:0,0,1) -- (axis cs:0,0,1);
\node at (axis cs:0.5*\Lag,0.75,1) {$t_{0} = \displaystyle\frac{\phi}{\omega}$};
% Fill area spanned by the angle \phi
\addplot[%
gray,
opacity=0.5
] fill between[%
of=A and B,
];
\end{axis}
\end{tikzpicture}
\vspace{-1.5cm}
\caption{Phase lag and time lag of a sinusoidal function relative to the cosine function.}
\label{fig:PLTLSF}
\end{figure}
\end{document}
以下是包含上述警告的输出:
请注意,图 2 中 x 轴标签的位置不正确。我怀疑该图的代码有问题,但我无法准确指出问题所在。有人能告诉我代码有什么问题吗?
更新
好的,我注释掉了该行view = {45}{30}
,警告消失了,对齐的问题也消失了。
view = {45}{30}
所以,现在的问题是:如果这条线对于我的目的至关重要,我该如何规避 x 轴标签定位的问题?
更新 2
axis
进一步的测试表明,仅当选项axis lines = center
和view = {<azimuth>}{<elevation>}
在单个文档中的一组两个或多个图中同时使用,并且仅对第一个参数分配一定范围的值时,才会出现此问题,即<azimuth>
才会出现此问题。值得注意的是,问题确实不是发生于
<azimuth>
的价值全部图形在0<角度<90的范围内;<azimuth>
的价值全部数字超过90;- 某些图形的值
<azimuth>
超过 90,其余图形位于 0 < 角度 < 25 之间;或者 - 除了最后一个图形的轴外,所有图形的轴都是隐藏的,正如Torbjørn T.
如果出现以下情况,则会出现问题
- 前几位数字的值
<azimuth>
大于 90,其余数字的值介于 25 < angle < 90 之间,
如上述示例一样,其中view
图 1 的设置为view = {115}{30}
,而图 2 的设置为view = {45}{30}
。
答案1
这很奇怪。我不知道为什么会发生这种情况,但我注意到它似乎也与第一张图相连axis lines=center
。删除它,警告就会消失。因此,一个潜在的解决方法是使用hide axis
第一张图,然后使用手动绘制轴线\draw
。
\documentclass[12pt,fleqn,a4paper]{article}
\usepackage{amsmath,amsfonts,amssymb}
\usepackage[hang]{caption}
\usepackage{float}
\usepackage{pgfplots}
\usepackage[subrefformat=parens]{subcaption}
\usetikzlibrary{%
arrows ,
calc ,
pgfplots.fillbetween,
positioning ,
}%
\usepackage[a4paper,textwidth=16cm,vscale=0.75,vcentering]{geometry}
% PGFPlot version compatibility settings
\pgfplotsset{compat=1.12}
\begin{document}
\begin{figure}[h!]
\centering
% Definition of plane
\newcommand*{\PointAx}{1}%
\newcommand*{\PointAy}{1}%
\newcommand*{\PointAz}{1}%
\newcommand*{\PointAc}{0}%
% Normal vector scaling
\newcommand*{\ScaleN}{1}%
% Endpoints of line segment spanned by the normal vector
\newcommand*{\ScaleB}{6}%
% Position of the label of plane
\newcommand*{\PointLx}{-0.9}%
\newcommand*{\PointLy}{-0.9}%
\begin{tikzpicture}
\begin{axis}[%
view = {115}{30},
xlabel = $x$,
ylabel = $y$,
zlabel = $z$,
xtick = \empty,
ytick = \empty,
ztick = \empty,
width = 15cm,
hide axis, % <-- added
% axis lines = center,
% axis line style = {->, white!50!black},
axis equal = true,
]
% draw axes manually
\draw [->, white!50!black]
(\pgfkeysvalueof{/pgfplots/xmin},0,0) --
(\pgfkeysvalueof{/pgfplots/xmax},0,0) node[above]{$x$};
\draw [->, white!50!black]
(0,\pgfkeysvalueof{/pgfplots/ymin},0) --
(0,\pgfkeysvalueof{/pgfplots/ymax},0) node[above]{$y$};
\draw [->, white!50!black]
(0,0,\pgfkeysvalueof{/pgfplots/zmin}) --
(0,0,\pgfkeysvalueof{/pgfplots/zmax}) node[right]{$z$};
% % Equation of line
\coordinate (P1) at (axis cs: \ScaleB*\PointAx, \ScaleB*\PointAy, \ScaleB*\PointAz);
\coordinate (P2) at (axis cs:-\ScaleB*\PointAx, -\ScaleB*\PointAy, -\ScaleB*\PointAz);
\draw[white!25!green] (P1) -- (P2);
% Plane ax + by + cz = d <=> z = 1/c * (-ax - by + d)
\addplot3[%
fill = white!75!blue,
opacity = 0.5,
draw = blue
] coordinates {%
(2, 0, {(1/\PointAz)*(-(\PointAx)*2 - (\PointAy)*0 + \PointAc)})
(0, -2, {(1/\PointAz)*(-(\PointAx)*0 - (\PointAy)*(-2) + \PointAc)})
(-2, 0, {(1/\PointAz)*(-(\PointAx)*(-2) - (\PointAy)*0 + \PointAc)})
(0, 2, {(1/\PointAz)*(-(\PointAx)*0 - (\PointAy)*2 + \PointAc)})
(2, 0, {(1/\PointAz)*(-(\PointAx)*2 - (\PointAy)*0 + \PointAc)})
};
% Overpainting correction for the line and the z-axis
\coordinate (O) at (axis cs: 0, 0, 0);
\draw[white!25!green] (O) -- (P1);
\draw[->, white!50!black] (axis cs: 0, 0, \PointAc) -- (axis cs: 0, 0, \pgfkeysvalueof{/pgfplots/zmax});
% Specify normal vector coordinates
\coordinate (N) at (axis cs: \ScaleN*\PointAx, \ScaleN*\PointAy, \ScaleN*\PointAz);
% Draw vectors
\draw[->, >=stealth, thick] (O) -- (N);
% Draw labels
\draw (O) node [anchor=north] {$\boldsymbol{\mathbf{0}}$};
\draw (N) node [anchor=south] {$\boldsymbol{\mathbf{n}}$};
\draw (P1) node [anchor=south west] {$L$};
\draw ([xshift=-30mm] axis cs: \pgfkeysvalueof{/pgfplots/xmin}, \pgfkeysvalueof{/pgfplots/ymax}, \pgfkeysvalueof{/pgfplots/zmax}) node [anchor=north east] {$\mathbb{R}^{3}$};
\draw (axis cs: \PointLx, \PointLy, {(1/\PointAz)*(-(\PointAx)*\PointLx - (\PointAy)*\PointLy + \PointAc)}) node {$\Pi$};
\end{axis}
\end{tikzpicture}
\caption{Typical subspaces in $\mathbb{R}^{3}$---the entire space $\mathbb{R}^{3}$, the plane $\Pi$, the line $L$ spanned by the normal vector of $\Pi$, and the singleton consisting of the zero vector.}
\label{fig:subspaces_in_r3}
\end{figure}
\begin{figure}[h!]
\centering
% Phase lag
\newcommand*{\Lag}{pi/2}
\begin{tikzpicture}
\begin{axis}[%
legend entries = {$x_{1}(t) = A\cos(\omega{t})$, $x_{2}(t) = A\cos(\omega{t} - \phi)$},
legend cell align = {left},
width = \textwidth,
view = {45}{30},
xlabel = $t$,
zlabel = $x(t)$,
xtick = {0, 1.57, 3.14, 4.71, 6.28},
ytick = \empty,
ztick = {-1, 1},
xticklabels = {$0$, $0.5\pi$, $\pi$, $1.5\pi$, $2\pi$},
zticklabels = {$-A$, $A$},
xmin = 0,
xmax = 2*pi,
ymin = -1.1,
ymax = 1.1,
zmin = -1.2,
zmax = 1.4,
axis lines = center,
axis line style = {->},
axis equal image = true,
hide y axis,
every axis x label/.style={%
at={(ticklabel* cs:1.02)},
anchor=west,
},
every axis z label/.style={%
at={(ticklabel* cs:1.02)},
anchor=south,
}]
\addplot3[%
domain=0:2*pi,
samples=100,
samples y=0,
color=black,
thick
]%
(x, 0, {cos(deg(x))});
\addplot3[%
domain=0:2*pi,
samples=100,
samples y=0,
color=black,
dashdotted,
thick
]%
(x, 0, {cos(deg(x - \Lag))});
\addplot3[%
domain=0:2*pi,
samples=100,
samples y=0,
color=black,
very thin
]%
(0, {cos(deg(x))}, {sin(deg(x))});
\addplot3[%
domain=0:2*pi,
samples=100,
samples y=0,
color=black,
very thin
]%
(\Lag, {cos(deg(x))}, {sin(deg(x))});
% Draw angle
\addplot3[%
domain=0.5*pi:pi,
samples=100,
samples y=0,
color=black
]%
(\Lag, {0.25*cos(deg(x))}, {0.25*sin(deg(x))});
\node at (axis cs:0.75*\Lag,0,0) {$\phi$};
% Clipping path A
\addplot3[%
name path=A,
draw=none,
domain=0.5*pi:pi,
samples=100,
samples y=0,
]%
(\Lag, {cos(deg(x))}, {sin(deg(x))});
% Clipping path B
\draw[%
name path=B,
->,
>=stealth
]%
(axis cs:\Lag,0,0) -- (axis cs:\Lag,-1,0);
\draw[%
dashed
]%
(axis cs:\Lag,0,0) -- (axis cs:\Lag,0,1);
\draw[%
<->,
>=stealth
]%
(axis cs:0,0,1) -- (axis cs:\Lag,0,1);
\draw[%
->,
>=stealth
]%
(axis cs:0,0,1) -- (axis cs:0,0,1);
\node at (axis cs:0.5*\Lag,0.75,1) {$t_{0} = \displaystyle\frac{\phi}{\omega}$};
% Fill area spanned by the angle \phi
\addplot[%
gray,
opacity=0.5
] fill between[%
of=A and B,
];
\end{axis}
\end{tikzpicture}
\vspace{-1.5cm}
\caption{Phase lag and time lag of a sinusoidal function relative to the cosine function.}
\label{fig:PLTLSF}
\end{figure}
\end{document}