調整 pgfplots

調整 pgfplots

我在控制 pgfplots 中的軸時遇到問題。我已經為圖表定義了一個環境

\newenvironment{graph}[3][]{\begin{figure}[htp]
\def\tempa{#1} %Saves caption since \end cannot take arguments
\begin{center}
\begin{tikzpicture}[scale=1] 
\draw node at (7,0) { \Large     $ #2 $ }; %variable on x-axis
\draw node at (0,6.1) { \Large $ #3 $}; %variable on y-axis
\begin{axis}[
      axis lines=left,
      axis equal,
      %ticks=none,
      %xlabel=$x$,
      %ylabel=$y$,
      %width=8cm,
      %height=8cm,
      domain=0:10.5,
      restrict y to domain=0:10.5
      samples=1000
  ]


}
{
\end{axis}
\end{tikzpicture}
\end{center}
\ifdefempty{\tempa}{}{\caption{\tempa}} %Creates caption if argument is present.
\end{figure}

} 

正如你所看到的,我一直在用軸進行一些實驗。我的問題是,它們的長度不相等。我希望它們具有相同的固定域 0:10.5,並且具有相同的精確長度 (cm)。通常 y 軸比 x 軸短。在第 5 行和第 6 行中,我定義了軸的名稱。正如您所看到的,變數放置在 6.1,而不是我定義軸尺寸的 10.5。我希望在使用繪圖命令時能夠使用軸的座標。

1

如果我將位置設為 10,則節點完全錯位。

\draw node at (10,0) { \Large    $ #2 $ }; %variable on x-axis

2

最後一個問題是繪製函數在環境內不起作用

\begin{graph}{x}{y}
\draw[dotted] (0,0) -- (2,2);
\end{graph}

這給我什麼都沒有:

3

提前致謝。

答案1

這是一個建議。我使用unit vector ratio=1 1 1而不是axis equal因為axis equal似乎稍微延長了 x 軸,不知道為什麼。軸標籤使用xlabel/放置ylabel,並透過變更樣式修改位置every axis x label,與 類似y。請注意,座標是相對於軸的,例如(0,1)位於 y 軸的頂部。

(我刪除了這些\caption東西,因為我不知道從哪裡來\ifdefempty,所以那部分不起作用。)

如果您想\draw在環境中使用 和 類似的東西graph,請使用axis cs座標系,如下面的範例所示。

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\newenvironment{graph}[3][]{\begin{figure}[htp]
\centering
\begin{tikzpicture}
\begin{axis}[
      axis lines=middle,
      unit vector ratio=1 1 1,
      every axis x label/.style={at={(1,0)},font=\Large,right},
      every axis y label/.style={at={(0,1)},font=\Large,above},
      xlabel=#2,
      ylabel=#3,
      domain=0:10.5,
      xmin=0,xmax=10.5,
      ymin=0,ymax=10.5,
      samples=100,
      no marks
  ]
}
{
\end{axis}
\end{tikzpicture}
\end{figure}
} 
\begin{document}
\begin{graph}{$x$}{$y$}
\addplot {x};
\addplot {x+1};
\addplot {x-1};
\draw [dotted] (axis cs:0,10.5) -- (axis cs:10.5,10.5) -- (axis cs:10.5,0);
\end{graph}

\begin{graph}{$X$}{$Y$}
\addplot {8*sin(deg(x))*sin(deg(x))};
\draw [thick,latex-latex] (axis cs:1.57,8) to[out=40,in=140] node[above]{random} (axis cs:3.14+1.57,8);
\end{graph}
\end{document}

在此輸入影像描述

相關內容