如何使用 Tikz 包绘制以下图形(形状函数)?

如何使用 Tikz 包绘制以下图形(形状函数)?

我希望有人能帮助我制作下面这个使用 Tikz 包制作的图形,事实上我会非常感激。在此处输入图片描述

答案1

不使用 lua

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{arrows.meta}
    
\begin{document}
\begin{tikzpicture}

\draw[-Latex] (0,0) -- ++(90:3.5);
\draw[-Latex] (0,0) -- ++(0:12.5);

\begin{scope}
\path[clip] (0,0) rectangle (12,3.5);
\foreach \i/\j [count=\ni] in {0/orange,1/green,2/blue,3/red,4/brown}
    \draw[\j, xshift=-3cm] (3cm*\i,0)--++(3cm,3cm) node[right] {$\phi_\ni$}--++(3cm,-3cm);
\end{scope}
\node[left] at (0,3) {1};
\node[left] at (12,3) {$\phi_5$};
\foreach \i/\j [count=\ni] in {0/0,1/{1/4},2/{2/4},3/{3/4},4/1}
    \node[below] at (3cm*\i,0) {$x_\ni=\j$};
\foreach \i [count=\ni] in {1,...,4}
    \node[above] at ([xshift=-1.5cm]3cm*\i,0) {$I_\ni$};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

在支持下,Lua我想到了这样的事情:

\documentclass{standalone}

\usepackage{tikz}
\usepackage{luacode}

\begin{document}

\begin{tikzpicture}[
      x = 2cm, y = 2cm,
      plot label/.style = {above right},
      xticklabel/.style = {below}
   ]
      \begin{luacode}
         xmin, xmax, denom, step = 0, 40, 8, 8
         n = xmax - xmin
         color = { 'orange', 'green', 'blue', 'red', 'brown', 'purple' }
         for row = xmin - 1, xmax, step do
            xticklabel = {}
            yoff = - 2 * (row / step)
            tex.sprint([[\string\begin{scope}[local bounding box=u] ]])
            for x = row, row + step do
               i, coords = x, {}
               for xs = 0,2 do
                  xi, yi = x - row + xs, math.fmod(xs, 2) + yoff
                  coord = ( xi > xmin and xi <= xmin + step ) and 
                     string.format([[(%f, %f)]], xi, yi ) or nil

                  if yi - yoff == 1 and coord then
                      coord = string.format(
                        '%s node[plot label] {$\\phi_{%i}$}', 
                        coord, x - xmin + 2
                     )
                  end
                  table.insert(coords, coord)
               end
               xticklabel[i] = ( x - row) > 0 and x or nil
               tex.sprint(string.format(
                  [[\string\draw[%s] %s ;]], 
                  color[math.fmod(x-xmin+1,#color)+1],
                  table.concat(coords, '--')
               ))
            end
            tex.sprint([[
               \string\end{scope}
               \string\draw[->] (u.south west) -- (u.south east);
               \string\draw[->] (u.south west) -- (u.north west);
            ]])
            for k,v in pairs(xticklabel) do
               s = k / denom == math.floor(k / denom) and 
                    math.floor(k / denom) or k .. '/' .. denom
               tex.sprint(string.format(
                  [[\string\draw (%i,%f) node[xticklabel] {$x_{%i}=%s$};]], 
                  k - row, yoff, k, s
               ))
            end
         end
      \end{luacode}
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容