![pgfplots 中的線條和軸標籤](https://rvso.com/image/392330/pgfplots%20%E4%B8%AD%E7%9A%84%E7%B7%9A%E6%A2%9D%E5%92%8C%E8%BB%B8%E6%A8%99%E7%B1%A4.png)
我對下面的程式碼有兩個問題:
如何將虛線放在正確的位置(即從 (0, 2) 到 (3, 2) 到 (3, 0))?目前他們處於錯誤的位置。
我怎樣才能移動X和y軸標籤分別位於軸的下方和左側?
\documentclass{article}
\usepackage{amsmath,tikz,graphicx,pgfplots,xcolor,float}
\begin{document}
\pgfplotsset{
standard/.style={
every axis x label/.style={at={(current axis.right of origin)},anchor=north west},
every axis y label/.style={at={(current axis.above origin)},anchor=north east}
}
}
\begin{figure}
\begin{tikzpicture}
\pgfmathsetmacro{\n}{sqrt(13)}
\begin{axis}[axis equal,
xlabel=$x$,
x label style = {below=5mm},
ylabel=$y$,
axis lines=middle,
xmin=-3,xmax=8,
xtick={3},
ymin=-3,ymax=8,
ytick={2},]
\addplot[very thick, red!50, domain=0:360, samples=100, variable=\t] ({3+\n*cos(t)},{2+\n*sin(t)});
\end{axis}
\coordinate (A) at (0,2);
\coordinate (B) at (3,2);
\coordinate (C) at (3,0);
\draw[dashed] (A)--(B)--(C);
\end{tikzpicture}
\end{figure}
\end{document}
答案1
至 1.:
在這裡您放置了\coordinate
s外部環境axis
,因此您引用的是 TikZ 座標係而不是axis
1。但即使你把它們放在裡面環境axis
也會導致相同的結果。程式碼中的註解解釋了為什麼以及如何防止/克服這種情況。
To 2.:
這裡您將axis
選項放置在「錯誤」的順序中。您的x label style
嘗試沒有成功,因為之後您加載了樣式,axis lines=middle
該樣式本身有一個x label style
聲明,因此“否決”了您的聲明。切換按鍵的順序即可使其運作。但是有一個比你的更好的解決方案,也可以在下面的程式碼中找到。
除此之外,我對您的程式碼做了一些其他改進...
% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
% use this `compat` level or higher to make `axis cs:` the default
% coordinate system for TikZ coordinates
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
% (this has to be written near the beginning, thus other styles are
% not overwritten again)
axis lines=middle,
axis equal,
xmin=-3,
xmax=8,
xtick={3},
ymin=-3,
ymax=8,
ytick={2},
xlabel=$x$,
ylabel=$y$,
% % (this didn't work because it was overwritten by `axis lines`,
% % which originally was below this statement)
% x label style={below=5mm},
xlabel style={
anchor=north east,
},
ylabel style={
anchor=north east,
},
]
\pgfmathsetmacro{\n}{sqrt(13)}
\addplot [
very thick,
red!50,
domain=0:360,
% (by using smooth the default `samples=25` is perfectly fine)
smooth,
] (
{3+\n*cos(x)},
{2+\n*sin(x)}
);
% when `compat` level is 1.11 or higher, TikZ coordinates don't have
% to be prepended by `axis cs:` (any more) because then this is the
% default coordinate system
\draw [dashed] (0,2) -| (3,0);
% % ... thus, otherwise you have to write
% \draw [dashed] (axis cs:0,2) -| (axis cs:3,0);
\end{axis}
\end{tikzpicture}
\end{document}
答案2
解決方案使用純的鈦kZ,代碼似乎是很多較短:
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\draw[-stealth] (0,-3)--(0,8) node[left] {$y$};
\draw[-stealth] (-3,0)--(8,0) node[below] {$x$};
\draw[dashed] (0,2) node[left] {2}--(3,2)--(3,0) node[below] {3};
\draw[red!50,very thick] plot[smooth,variable=\t,samples=100,domain=0:360] ({3+sqrt(13)*cos(\t)},{2+sqrt(13)*sin(\t)});
\end{tikzpicture}
\end{document}
scale=...
如果您發現數字太大(我認為是這樣),您可以添加選項。