
axis
我在基於長軸、短軸和 x 軸角度的pgfplots 環境中繪製橢圓時遇到問題。我已經設法使用 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}
輸出: