如何绘制以下图形:一些 xticks 在 X 轴上方,一些在下方?

如何绘制以下图形:一些 xticks 在 X 轴上方,一些在下方?

我想重新创建以下图表 在此处输入图片描述

这是我的 MWE:

\documentclass[tikz]{standalone}%{article}
\usepackage{amsmath} 
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[tick label style={font=\scriptsize},minor x tick num=1, axis y line=middle,axis x line=middle,ymin=-5,ymax=21,xmin=-1,xmax=5,xlabel=$x$,ylabel=$y$]
\addplot[blue,smooth,domain=-.5:4,samples=30] {x^3 - 3*x^2 + 1}; 
\filldraw [thick,blue] (axis cs:2,-3) circle (1.5pt);
\filldraw [thick,blue] (axis cs:4,17) circle (1.5pt); 
\end{axis}
\end{tikzpicture}  
\end{document}

我的主要问题是轴上的标签。

已编辑:我想将xtick标签$1$和$2$放在$x$轴上方,如上图所示。

答案1

像这样?

在此处输入图片描述

\documentclass[tikz]{standalone}%{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15} % <--- added

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    minor x tick num=1,
    axis lines=middle,    % <--- changed
    tick label style={font=\scriptsize},
    ymin=-4.9,ymax=23,
    xmin=-1,xmax=4.9,
    xtick={3,4},         % <--- added
    extra x ticks={1,2}, % <--- added
    extra x tick style={tick label style={yshift=1mm,anchor=south}},% <--- added
    x label style={anchor=north east}, % <--- added
    y label style={anchor=north east}, % <--- added
    xlabel=$x$,
    ylabel=$y$
            ]
\addplot[blue,smooth,domain=-.5:4,samples=30] {x^3 - 3*x^2 + 1};
\filldraw [thick,blue] (4,17) circle (1.5pt) node[right,font=\scriptsize] {$(4,17)$}; % <--- added node
\filldraw [thick,blue] (2,-3) circle (1.5pt) node[below,font=\scriptsize] {$(2,-3)$}; % <--- added node
\node [right] at (1,20) {$x^3 - 3x^2 + 1$};% <--- added
\end{axis}
\end{tikzpicture}
\end{document}

答案2

使用 MetaPost 完成,可能会有人感兴趣。

借助该luamplib包,可以将其集成到 LuaLaTeX 程序中。(luatex85加载该包只是为了与standalone类兼容)。

\documentclass[border=3mm]{standalone}
\usepackage{luatex85,luamplib}
    \mplibtextextlabel{enable}
\begin{document}
\begin{mplibcode}
    u = cm; v = .2cm; len = 2bp;
    xmin = -1.9; xmax = 5.5; ymin = -9; ymax = 24; xstep = .1;
    vardef f(expr x) = x**3 - 3*(x**2) + 1 enddef;
    beginfig(1);
        % Axes and labels
        drawarrow (xmin*u, 0) -- (xmax*u, 0); label.bot("$x$", (xmax*u, 0));
        drawarrow (0, ymin*v) -- (0, ymax*v); label.lft("$y$", (0, ymax*v));
        % Marks and labels
        for i = -1 upto xmax: 
            if i<>0: draw (i*u, -len) -- (i*u, len) fi;
            label.if i=0: llft elseif (i=1) or (i=2): top else: bot fi
                ("$" & decimal i & "$", (i*u, 0));
        endfor
        for j = -5 step 5 until ymax:
            if j<>0:
                draw (-len, j*v) -- (len, j*v);
                label.lft("$" & decimal j & "$", (0, j*v));
            fi;
        endfor;
        % Labels on curve
        label.bot("$(2, -3)$", (2u, -3v)); label.top("$(4, 17)$", (4u, 17v));
        label.top("$y = x^3 - 3x^2 + 1$", (2u, 20v));
        % Curve and dots
        drawoptions(withcolor blue);
        draw ((-.5, f(-.5))
            for x = -.5 + xstep step xstep until 4+.5xstep: .. (x, f(x)) endfor)
            xscaled u yscaled v withpen pencircle scaled bp;
        pickup pencircle scaled 3bp;
        drawdot(2u, -3v); drawdot(4u, 17v);
    endfig;
\end{mplibcode}
\end{document}

在此处输入图片描述

相关内容