縮放“pgfplots”中的軸以繪製平方根函數

縮放“pgfplots”中的軸以繪製平方根函數

這段程式碼有什麼問題。我用unit vector ratio={2 1}它來更好地視覺化平方根函數和立方根函數。

\documentclass{amsart}
\usepackage{amsmath}
\usepackage{amsfonts}

\usepackage{tikz}
\usetikzlibrary{calc,positioning,intersections}

\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}

\noindent \hspace*{\fill}
\begin{tikzpicture}
\begin{axis}[height=4.5in,width=4.5in, clip=false,
    unit vector ratio={2 1},
    xmin=-100,xmax=100,
    ymin=-5,ymax=10,
    restrict y to domain=-5:10,
    xtick={\empty},ytick={\empty},
    enlargelimits={abs=1cm},
    axis lines=middle,
    axis line style={latex-latex},
    xlabel=$x$,ylabel=$y$,
    xlabel style={at={(ticklabel* cs:1)},anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},anchor=south west}
    ]

\addplot[samples=501, domain=0:100, blue] {x^(1/2)} node[anchor=north west, pos=0.75, font=\footnotesize]{$y = \sqrt{x}$};
\addplot[samples=501, domain=-100:0, green] {-(-x)^(1/3)}
node[anchor=south east, pos=0.25, font=\footnotesize]{$y = \sqrt[\uproot{1} \leftroot{-1} n]{x}$};
\addplot[samples=501, domain=0:100, green] {x^(1/3)};

\end{axis}
\end{tikzpicture}

\end{document}

答案1

unit vector ratio={2 1}- 方向的單位向量是- 方向x的單位向量的兩倍長y。但圖中 y 方向只有 15 個單位,而x- 方向有 200 個單位。因此,如果y- 軸應為 1 厘米長,則x- 軸必須為 1 厘米*(200/15)*2=26.7 厘米長!

我建議使用類似的unit vector ratio={1 4} 結果

在此輸入影像描述

代碼:

\documentclass{amsart}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}

\begin{document}
\noindent \hspace*{\fill}
\begin{tikzpicture}
\begin{axis}[height=4.5in,width=4.5in, clip=false,
    unit vector ratio={1 4},
    xmin=-100,xmax=100,
    ymin=-5,ymax=10,
    restrict y to domain=-5:10,
    xtick={\empty},ytick={\empty},
    enlargelimits={abs=1cm},
    axis lines=middle,
    axis line style={latex-latex},
    xlabel=$x$,ylabel=$y$,
    xlabel style={at={(ticklabel* cs:1)},anchor=north west},
    ylabel style={at={(ticklabel* cs:1)},anchor=south west}
    ]
\addplot[samples=501, domain=0:100, blue] {x^(1/2)} node[anchor=north west, pos=0.75, font=\footnotesize]{$y = \sqrt{x}$};
\addplot[samples=501, domain=-100:0, green] {-(-x)^(1/3)}
node[anchor=south east, pos=0.25, font=\footnotesize]{$y = \sqrt[\uproot{1} \leftroot{-1} n]{x}$};
\addplot[samples=501, domain=0:100, green] {x^(1/3)};
\end{axis}
\end{tikzpicture}
\end{document}

因為評論裡的問題:

你設定width=4.5inheight=4.5in。如果你不設置unit vector ratioyminymax會得到一個正方形

在此輸入影像描述

\begin{axis}[height=4.5in,width=4.5in, clip=false,
    %unit vector ratio={1 4},
    xmin=-100,xmax=100,
    %ymin=-5,ymax=10,
    %restrict y to domain=-5:10,
    %xtick={\empty},ytick={\empty},
    ...
    ]

隨著- 軸unit vector ratio={1 4}的縮放y發生變化,但仍然有一個正方形

在此輸入影像描述

\begin{axis}[height=4.5in,width=4.5in, clip=false,
    unit vector ratio={1 4},
    xmin=-100,xmax=100,
    %ymin=-5,ymax=10,
    %restrict y to domain=-5:10,
    %xtick={\empty},ytick={\empty},
    ...
    ]

但隨後您y使用ymin和限制顯示的 - 範圍,ymax因此 - 軸的高度y減少。

在此輸入影像描述

\begin{axis}[height=4.5in,width=4.5in, clip=false,
    %unit vector ratio={1 4},
    xmin=-100,xmax=100,
    ymin=-5,ymax=10,
    %restrict y to domain=-5:10,
    %xtick={\empty},ytick={\empty},
    ...
    ]

答案2

給定的程式碼給出了預期的結果。主要問題是您縮放了錯誤的參數,因此擠壓了錯誤的軸。

此外,您還提供widthheight以及所有軸限制(即xminxmaxyminymax),所以問題是,什麼具有更高的優先權執行,或取決於給定的鍵順序。

這是一個簡化的程式碼,顯示一切都按預期工作。希望這有助於根據您的需求修改您的程式碼,但我在這裡無法提供更多幫助,因為您的問題相當「模糊」。

\documentclass[border=2mm]{standalone}
\usepackage{amsmath}
\usepackage{tikz}
    \usetikzlibrary{calc,positioning,intersections}
\usepackage{pgfplots}
    \pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
        % define a scaling factor for `unit vector ratio'
        \pgfmathsetmacro{\factor}{10}
        % define a lenght to draw in y direction for testing, 
        % if `unit vector ratio' is working as expected
        \pgfmathsetmacro{\Ydirection}{5}
    \begin{axis}[
        clip=false,
        unit vector ratio={1 \factor},
        restrict y to domain=-5:10,
        xlabel=$x$,ylabel=$y$,
    ]
        \addplot[samples=51, domain=0:100, blue] {x^(1/2)}
            node[anchor=north west, pos=0.75, font=\footnotesize]
                {$y = \sqrt{x}$};
        \addplot[samples=51, domain=0:100, green] {x^(1/3)};

        % draw some lines for testing, if the `unit vector ratio' is
        % working as expected and save the beginning and ending coordinates
        \draw [red] (0,0) -- +(axis direction cs: \factor*\Ydirection,0)
            coordinate [pos=0] (origin)
            coordinate [pos=1] (x)
        ;
        \draw [red] (0,0) -- +(axis direction cs: 0,\Ydirection)
            coordinate [pos=1] (y)
        ;
    \end{axis}
    \path   let
                % calculate "dummy" coordinates giving the coordinates
                % of the difference between the points
                % (because the one is at the origin it should give
                %  the same values as the first coordinate)
                \p1 = ($ (x) - (origin) $),
                \p2 = ($ (y) - (origin) $),
                % calculate the vector lengths of the "dummy points"
                \n1 = {veclen(\x1,\y1)},
                \n2 = {veclen(\x2,\y2)}
            in
                % plot the calculated length of the vectors, which should
                % be identical (if there are no rounding errors)
                node [anchor=west]  at (x) {\n1}
                node [anchor=south] at (y) {\n2}
    ;
\end{tikzpicture}
\end{document}

顯示上述程式碼結果的圖像

相關內容