使用 tikz 包填充区域

使用 tikz 包填充区域

我想用 tikz 填充这些部分 在此处输入图片描述

Tex 代码:

\documentclass[a4paper,pdflatex,openany,12pt]{book}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
\draw[->] (-0.5,0) -- (9,0) node[right] {$x$};
\draw[->] (0,-0.5) -- (0,5) node[above] {$y$};
\draw[-] (2,0) -- (2,2) ;
\draw[-] (4,0) -- (4,1) ; 
\draw[-] (6,0) -- (6,0.67) ;
\draw[-] (2,1) -- (4,1) ;
\draw[-] (4,0.67) -- (6,0.67) ; 
\draw[scale=2,domain=1/2:4,smooth,variable=\x,black] plot ({\x},{1/\x}) node[right] {$y=1/x$};
\draw[scale=1,domain=1/2:4,smooth,variable=\y,black] ;
\draw (2,0)  node[below] {$2$};
\draw (4,0)  node[below] {$4$};
\draw (6,0)  node[below] {$6$};
\end{tikzpicture}

\end{document}

答案1

在此处输入图片描述

\documentclass[a4paper,pdflatex,openany,12pt]{book}
\usepackage{pgfplots}

\begin{document}
    \begin{tikzpicture}[yscale=3]
% axes
\draw[->] (-0.25,0) -- (7.5,0) node[right] {$x$};
\draw[->] (0,-0.125) -- (0,2) node[above] {$y$};
\foreach \i in {2, 4, 6}
    \node[below] at (\i,0) {$\i$};
% filed area
\fill[domain=2:4,smooth,blue!30] plot (\x,1/\x) -| (2,0.5);
\fill[domain=4:6,smooth,blue!30] plot (\x,1/\x) -| (4,0.25);
% curve 1/x
\draw[domain=1/2:6.5,smooth,variable=\x,black,thick] 
            plot ({\x},{1/\x}) node[right] {$y=1/x$};
% auxiliary lines
\draw[thin] (2,0) -- (2,0.5) node[above] {A}
            (4,0) -- (4,0.25) node[above] {B}
            (6,0) -- (6,0.166) node[above] {H}
            (2,0.25) node[left] {C} -- (4,0.25)
            (4,0.166) node[left] {D} -- (6,0.166) ;

    \end{tikzpicture}
\end{document}

答案2

您可以考虑使用pgfplots基于 TikZ 的包。

\documentclass{article} 

\usepackage{tikz,pgfplots}
\pgfplotsset{width=7cm,compat=1.12}
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}
  \begin{axis} 
    [axis x line=center,
    axis y line=center,
    xmin = 0,
    xmax = 8,
    ymin = 0,
    ymax = 4.5,
    xtick={2,4,6},
    ytick=\empty,
    xlabel=$x$,
    ylabel=$y$,
    x label style={anchor=west},
    y label style={anchor=south},
    clip=false,]

    \addplot[name path=A,domain=0.5:7.5,samples=100] {2/x} node [right] {$y=1/x$};
    \draw[name path=B] (2,0.5) -- (4,0.5);
    \draw[name path=C] (4,2/6) -- (6,2/6);
    \draw[] (2,0) -- (2,1);
    \draw[] (4,0) -- (4,2/4); 
    \draw[] (6,0) -- (6,2/6);

    \addplot[blue!30] fill between[of=A and B, soft clip={domain=2:4}];
    \addplot[blue!30] fill between[of=A and C, soft clip={domain=4:6}];
  \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述


替代方法(由 Torbjørn T. 添加),使用不同的绘图类型而不是\draw语句来绘制水平和垂直线,输出如上:

\documentclass{article} 

\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.12}
\usepgfplotslibrary{fillbetween}

\begin{document}
\begin{tikzpicture}[
   declare function={f(\x)=2/\x;}
]
  \begin{axis} 
    [axis x line=center,
    axis y line=center,
    xmin = 0,
    xmax = 8,
    ymin = 0,
    ymax = 4.5,
    xtick={2,4,6},
    ytick=\empty,
    xlabel=$x$,
    ylabel=$y$,
    x label style={anchor=west},
    y label style={anchor=south},
    clip=false
    ]

    \addplot [name path=A,domain=0.5:7.5,samples=100] {f(x)} node [right] {$y=2/x$};
    \addplot [ycomb,mark=none,samples at={2,4,6}] {f(x)};
    \addplot [name path=B,const plot,samples at={6,4,2}] {f(x)};
    \addplot [blue!30] fill between[of=A and B, soft clip={domain=2:6}];

    \end{axis}
\end{tikzpicture}
\end{document}

相关内容