我在 pgfplots 环境中根据长轴、短轴和相对于 x 轴的角度绘制椭圆时遇到了麻烦axis
。我设法使用 Tikz 绘制了这样的椭圆,但是,角度的旋转并没有正确地遵循轴之间的纵横比。我的意思是,如果对于具有固定宽度和高度且纵横比相等的图形,角度为 45°,则增加 x 轴的范围应该会“挤压”椭圆(并增加角度)。那么,我该如何相对于坐标系本身(而不仅仅是“屏幕”)旋转椭圆?谢谢!
梅威瑟:
\documentclass{minimal}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis} [
name=plot_left,
scale only axis,
height=35mm,
width=15mm,
xmin=0,
xmax=3,
ymin=0,
ymax=2,
]
\draw[draw=blue] (axis cs:1,1) ellipse [x radius=0.25, y radius=0.5, rotate=45];
\end{axis}
\begin{axis} [
name=plot_right,
at={(plot_left.north east)},
anchor=north west,
xshift=10mm,
scale only axis,
height=35mm,
width=90mm,
xmin=0,
xmax=3,
ymin=0,
ymax=2,
]
\draw[draw=blue] (axis cs:1,1) ellipse [x radius=0.25, y radius=0.5, rotate=45];
\end{axis}
\end{tikzpicture}
\end{document}
输出:
期望:
为了澄清起见,这就是我所寻找的:所以类似于Jasper Habicht 的解决方案(axis cs:1,1) ellipse [x radius=0.25*\a, y radius=0.5*\b, rotate=45*\c]
,其中\a
,\b
和\c
是根据纵横比和图像宽度计算的。
答案1
不确定这是否是您的意思,但您可以通过用轴单位除以宽度和高度来计算旋转角度,然后得到反正切,但我不确定这是否真的会产生正确的输出:
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis} [
name=plot_left,
scale only axis,
height=35mm,
width=15mm,
xmin=0,
xmax=3,
ymin=0,
ymax=2,
]
\pgfmathsetmacro{\a}{
atan(
(\pgfkeysvalueof{/pgfplots/width}/(\pgfkeysvalueof{/pgfplots/xmax}-\pgfkeysvalueof{/pgfplots/xmin}))/
(\pgfkeysvalueof{/pgfplots/height}/(\pgfkeysvalueof{/pgfplots/ymax}-\pgfkeysvalueof{/pgfplots/ymin})))
}
\draw[draw=blue] (1,1) ellipse [x radius=0.25, y radius=0.5, rotate=\a];
\end{axis}
\begin{axis} [
name=plot_right,
at={(plot_left.north east)},
anchor=north west,
xshift=10mm,
scale only axis,
height=35mm,
width=90mm,
xmin=0,
xmax=3,
ymin=0,
ymax=2,
]
\pgfmathsetmacro{\a}{
atan(
(\pgfkeysvalueof{/pgfplots/width}/(\pgfkeysvalueof{/pgfplots/xmax}-\pgfkeysvalueof{/pgfplots/xmin}))/
(\pgfkeysvalueof{/pgfplots/height}/(\pgfkeysvalueof{/pgfplots/ymax}-\pgfkeysvalueof{/pgfplots/ymin})))
}
\draw[draw=blue] (1,1) ellipse [x radius=0.25, y radius=0.5, rotate=\a];
\end{axis}
\end{tikzpicture}
\end{document}
最好的方法可能是以某种方式计算省略号的参数函数并绘制它。这将是这样的:
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
\begin{axis} [
name=plot_left,
scale only axis,
height=35mm,
width=15mm,
xmin=0,
xmax=3,
ymin=0,
ymax=2,
]
\addplot[domain=0:360, samples=200, blue](
{cos(x)*sqrt(0.33/(sin(2*x)+2))+1},
{sin(x)*sqrt(0.33/(sin(2*x)+2))+1}
);
\end{axis}
\begin{axis} [
name=plot_right,
at={(plot_left.north east)},
anchor=north west,
xshift=10mm,
scale only axis,
height=35mm,
width=90mm,
xmin=0,
xmax=3,
ymin=0,
ymax=2,
]
\addplot[domain=0:360, samples=200, blue](
{cos(x)*sqrt(0.33/(sin(2*x)+2))+1},
{sin(x)*sqrt(0.33/(sin(2*x)+2))+1}
);
\end{axis}
\end{tikzpicture}
\end{document}
答案2
我已经弄清楚了,这要感谢 Jasper Habicht 的建议,以及这帖子:事实证明它并不像我想象的那么复杂。
因此,使用下面的代码,您可以在pgfplots
轴环境中绘制一个椭圆,指定中心 x、y 以及相对于 x 轴的长轴、短轴和角度。
梅威瑟:
\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\newcommand{\addellipse}[5]{\addplot [domain=0:360, samples=50, draw=blue] ({(#3/2)*cos(x)*cos(#5) - (#4/2)*sin(x)*sin(#5) + #1}, {(#3/2)*cos(x)*sin(#5) + (#4/2)*sin(x)*cos(#5) + #2});}
\begin{document}
\begin{tikzpicture}
\begin{axis} [
name=plot_left,
scale only axis,
height=35mm,
width=15mm,
xmin=0,
xmax=3,
ymin=0,
ymax=2,
]
\addellipse{1}{1}{1}{0.5}{45}
);
\end{axis}
\begin{axis} [
name=plot_right,
at={(plot_left.north east)},
anchor=north west,
xshift=10mm,
scale only axis,
height=35mm,
width=90mm,
xmin=0,
xmax=3,
ymin=0,
ymax=2,
]
\addellipse{1}{1}{1}{0.5}{45}
);
\end{axis}
\end{tikzpicture}
\end{document}
输出: