这段代码的“xmin”和“xmax”哪里错误?

这段代码的“xmin”和“xmax”哪里错误?

在下面的代码中,我使用了xmin=-2.5, xmax=4.2,但输出结果不正确。我该如何修复?

\documentclass[tikz,12pt]{standalone}
    \usepackage{pgfplots}
    \pgfplotsset{compat=1.16}
    \usepackage{fouriernc}
    \begin{document}
        \begin{tikzpicture}[
        declare function={
            f(\x)=(1/2)*\x^3+3/2*(\x^2)-4;
            g(\x)= -2*\x^2+8*x-2;
                }
        ]
        \begin{axis}[axis equal,
        width=10cm, 
        grid=major, 
        axis x line=middle, axis y line=middle,
        axis line style = very thick,
        grid style={gray!30},
        ymin=-5, ymax=7, yticklabels={}, ylabel=$y$,
        xmin=-2.5, xmax=4.2, xticklabels={}, xlabel=$x$,
        samples=500,
        ]
        \addplot[blue, very thick,domain=-3:2, smooth]{f(x)};
        \addplot[blue, very thick,domain=2:4, smooth]{g(x)};
        %   \node[below left] at (-3, 0) {$-3$};
        \node[above] at (-2, 0) {$-2$};
        %\node[left,below] at (-1, 0) {$-1$};
        \node[below left] at ( 0,-4) {$-4$};
        \node[below left ] at ( 0,-2) {$-2$};
        %\node[above ] at (1,0) {$1$};
        \node[ left ] at (0,6) {$6$};
        \node[below right] at (0, 0) {$O$};
        \node[above] at (4, 0) {$4$};
        \node[below] at (2, 0) {$2$};
        %\node[left  ] at (0, -2) {$-2$};
        %\addplot [mark=*,only marks,samples at={-1,1,0}] {f(x)};
        ;
        \draw[dashed, thick] (2,0) -- (2,6) -- (0,6)
        (-2,0) -- (-2,-2) -- (4,-2) -- (4,0)
            ;
            \end{axis}
        \end{tikzpicture}
    \end{document} 

在此处输入图片描述

答案1

我很乐意删除它,但您是否想删除超出限制的轴,您可能需要使用axis equal image

\documentclass[tikz,12pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepackage{fouriernc}
\begin{document}
    \begin{tikzpicture}[
    declare function={
        f(\x)=(1/2)*\x^3+3/2*(\x^2)-4;
        g(\x)= -2*\x^2+8*x-2;
            }
    ]
    \begin{axis}[axis equal image,
    width=10cm, 
    grid=major,
    axis x line=middle, axis y line=middle,
    axis line style = very thick,
    grid style={gray!30},
    ymin=-5, ymax=7, yticklabels={}, ylabel=$y$,
    xmin=-2.5, xmax=4.2, xticklabels={}, xlabel=$x$,
    samples=500]
    \addplot[blue, very thick,domain=-3:2, smooth]{f(x)};
    \addplot[blue, very thick,domain=2:4.2, smooth]{g(x)};
    %   \node[below left] at (-3, 0) {$-3$};
    \node[above] at (-2, 0) {$-2$};
    %\node[left,below] at (-1, 0) {$-1$};
    \node[below left] at ( 0,-4) {$-4$};
    \node[below left ] at ( 0,-2) {$-2$};
    %\node[above ] at (1,0) {$1$};
    \node[ left ] at (0,6) {$6$};
    \node[below right] at (0, 0) {$O$};
    \node[above] at (4, 0) {$4$};
    \node[below] at (2, 0) {$2$};
    %\node[left  ] at (0, -2) {$-2$};
    %\addplot [mark=*,only marks,samples at={-1,1,0}] {f(x)};
    ;
    \draw[dashed, thick] (2,0) -- (2,6) -- (0,6)
    (-2,0) -- (-2,-2) -- (4,-2) -- (4,0)
        ;
        \end{axis}
    \end{tikzpicture}
\end{document} 

在此处输入图片描述

或者对于xmin=-3, xmax=5和稍微扩展的域

在此处输入图片描述

相关内容