如何使用 tikz 获取此图形

如何使用 tikz 获取此图形

如何使用此代码来获得如图所示的效果?

\documentclass[10pt]{article}
\usepackage{mathtools,relsize,tikz}
\usetikzlibrary{calc,intersections,through} 
\begin{figure}[h]
  \def\GraphAndAxis{%
    \draw[->] (-3.0,0) -- (2.7,0) coordinate (x axis);
    \draw[smooth,semithick,fill=none,name path=graph,domain=-2.9:2.6]% 
    plot[id=poly] (\x,0.05*\x*\x*\x - 0.2*\x*\x - 0.2*\x + 3);} % end GraphAndAxis
    \begin{tikzpicture}
      \def\TagBox#1#2#3#4{% 1=x_i, 2=x*_i, 3=x_(i+1), 4=i
        % \useasboundingbox (0,0) rectangle (0,0);
        \path[name path=upward at tag] (#2,0.0) -- (#2,3.1);
        % In general the meaning of (p) |- (q) is "the intersection of a vertical line through p
        % and a horizontal line through q."
        \path[name intersections={of=graph and upward at tag, by=fofxstar}];
        \draw[fill=gray!40] (#3,0.0 |- fofxstar) rectangle (#1,0.0) 
        node[below,opacity=1.0] {\smaller\ensuremath{x^{\phantom{*}}_{#4}}};
        \draw[thin,dashed] (#2,0.0) node[below] {\smaller\ensuremath{x^*_{#4}}}-- (fofxstar);
      } % end TagBox
      \GraphAndAxis %draw it so \TagBox can find intersection with it
      \TagBox{-2.8}{-2.1}{-1.3}{0}
      \TagBox{-1.3}{-0.8}{0.2}{1}
      \TagBox{0.2}{0.7}{1.3}{2}
      \TagBox{1.3}{1.9}{2.5}{3}
      \GraphAndAxis
      \begin{scope}
        \path (2.5,1.0) -- (2.5,0.0) node [below] {\smaller\ensuremath{x^{\phantom{*}}_{4}}}; %ugly manual hack
      \end{scope}
    \end{tikzpicture}
    \end{figure}

在此处输入图片描述

答案1

如果您不局限于 TiKZ,我建议基于以下内容的解决方案pstricks

\documentclass[border =6pt, svgnames]{standalone}
\usepackage{amsbsy}
\usepackage{pstricks-add}
\usepackage{auto-pst-pdf}

\begin{document}

\psset{linejoin=1, arrowinset=0.12}
\begin{pspicture}*(-1,-3)(8.5,4)
\psaxes[ticks=none, labels=none]{->}(0,0)(-1,-3)(8.5, 4)%[$ x $,-120][ $ y $,-130]
\pnodes{A}(1.2, 0)(2.6, 0)(3.8,0)(5.1,0)(6.5,0)(7.7, 0)
\foreach \i/\s in {0/d, 1/d, 2/dl, 3/dr, 4/dl, 5/dr}{\uput[\s](A\i){$x_{\i}$}}
\pnodes{B}(1.2, 2.4)(2.6, 2.4)(2.6, 1.2)(3.8, 1.2)(5.1, 3.2)(6.5, 3.2)
\pnodes{C}(3.8, -1.3)(5.1, -1.3)(6.5, -0.8)(7.7, -0.8)
\pnodes{H}(0, 0)(0, 2.4)(0, 1.2)(0, -0.8)(0, 3.2)(0,-1.3)
\multido{\i=1+1}{5}{\uput[l](H\i){$\lambda_{\i}$}}
{\psset{linestyle=dashed, dash=2.5pt 2pt, linewidth=0.5pt}%
\foreach \i in {1, 2,4}{\psline[](B\i)(H\i)}
\psline(C2)(H3)\psline(C0)(H5) }%
\psset{hatchcolor=Gainsboro!60!Lavender,fillstyle=vlines}
\pspolygon(A0)(B0)(B1)(A1)
\pspolygon(A1)(B2)(B3)(A2)
\pspolygon(A3)(B4)(B5)(A4)
\psset{fillstyle=hlines}
\pspolygon(A2)(C0)(C1)(A3)
\pspolygon(A4)(C2)(C3)(A5)
%
\psset{linestyle=none, fillstyle=solid, fillcolor=white, framesep=0pt}
\ncline[offset=1.1cm]{A0}{A1} \ncput*{$\boldsymbol{+}$}
\ncline[offset=-0.6cm]{A2}{A3} \ncput*{$\boldsymbol{-}$}
\ncline[offset=0.1cm]{A4}{A5} \nbput*{$\boldsymbol{-}$}
\foreach\i in {2,4}{\ncline{A\i}{B\i}\ncput*{$\boldsymbol{+}$}}%}
\end{pspicture}

\end{document} 

在此处输入图片描述

相关内容