Tikz:由几条曲线包围的阴影区域

Tikz:由几条曲线包围的阴影区域

我知道我下面的努力还有很大的改进空间。特别是,必须有一种方法可以在一个范围内对区域进行着色,而不是像我所做的那样进行细分(因此需要隐藏的灰色边界)。我也不喜欢样本点,但这是我的研究成果。感谢您的建设性批评。

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
%graph
\draw[draw=gray!50!white,fill=gray!50!white] 
    plot[smooth,samples=100,domain=0:1] (\x,{0}) -- 
    plot[smooth,samples=100,domain=1:0] (\x,{1});
\draw[fill=gray!50!white] plot[smooth,samples=100,domain=1:2.71828] (\x,{ln(\x)}) -- 
    plot[smooth,samples=100,domain=2.71828:1] (\x,{1});
\draw[domain=0:4] plot (\x,{1});
\draw[samples=100,domain=.25:4] plot (\x,{ln(\x)}) node[above left]{$y=\ln x$};
%coordinate grid
\draw (-.5,0)--(4.5,0) node[right]{$x$};
\draw (0,-1.5)--(0,2.5) node[above]{$y$};
\foreach \x in {1,2,3,4}
    \draw (\x,2pt)--(\x,-2pt) node[below] {$\x$};
\foreach \y/\ytext in {1,2}
    \draw (2pt,\y)--(-2pt,\y) node[left] {$\y$};    
%labels 
\node at (.75,.5) {$\mathcal{D}$};
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案1

仅通过剪辑你就能做很多事情(如果有的话)。

如果您知道具体点,您可以在一整个路径中执行此操作:

\draw[fill=gray!50] plot[smooth, samples=100, domain=1:e] (\x,ln \x) -| (0,0) -- cycle;

如果您不知道点,您将需要使用剪辑甚至交叉点。

代码

\documentclass[tikz]{standalone}
\tikzset{
  saveuse path/.code 2 args={
    \pgfkeysalso{#1/.style={insert path={#2}}}%
    \global\expandafter\let\csname pgfk@\pgfkeyscurrentpath/.@cmd\expandafter\endcsname
      % not optimal as it is now global through out the document
                           \csname pgfk@\pgfkeyscurrentpath/.@cmd\endcsname
    \pgfkeysalso{#1}},
  /pgf/math set seed/.code=\pgfmathsetseed{#1}}
\begin{document}
\tikz
  \draw[fill=gray!50] plot[smooth, samples=100, domain=1:e] (\x,ln \x) -| (0,0) -- cycle;
\begin{tikzpicture}
\begin{scope}
  \clip [saveuse path={plot path}{plot[smooth, samples=100, domain=.25:4] (\x, ln \x)}]
     -| (0,0) -- cycle;
  \clip[preaction={draw,fill=gray!50}] (0,0) rectangle (4,1);
\end{scope}
\draw [plot path];
\end{tikzpicture}
\begin{tikzpicture}
\begin{scope}[overlay]
  \clip [saveuse path={plot path}{plot[smooth, samples=100, domain=.25:4] (\x, ln \x)}]
     -- (0,5) -- (0,-5) -- cycle ;
  \clip [saveuse path={zigzag}{[math set seed=150, rounded corners=1.5pt] (0,3)
    \foreach \cnt in {1,...,8} {-- ++ (rnd, -.5*rnd)}}]
    [sharp corners] -- (0, -5) -- cycle;
  \clip (2,1) circle [radius=1.9] [draw, preaction={draw,fill=gray}];
\end{scope}
\draw [plot path, zigzag];
\end{tikzpicture}
\end{document}

输出

在此处输入图片描述

在此处输入图片描述 在此处输入图片描述

答案2

不管怎样,在 Asymptote 中获取现有路径的子路径相对容易:使用方法intersect查找两个路径相交的路径时间,然后使用subpath方法使用这些路径时间生成现有路径的子路径。

有关更多详细信息,包括对“路径时间”的解释,请参阅本教程. 如果将来修订时页码发生变化:请在索引中查找“路径时间”。

\documentclass{standalone}
% to compile, if this is saved in foo.tex:
% pdflatex foo.tex
% asy foo-*.asy
% pdflatex foo.tex
\usepackage{asymptote}
\begin{document}
\begin{asy}
import graph;
real unit = 1cm;
unitsize(unit);

// Set the font size to match the document.
defaultpen(fontsize(10pt));

// Compute the desired paths.
path lngraph = graph(log, 0.25, 4);
path xaxis = (-.5,0) -- (4.5,0);
path yaxis = (0,-1.5) -- (0,2.5);
path yEqualsOne = (0,1) -- (4,1);

// Compute the path times of the intersection points.
real lowerisection = intersect(lngraph, xaxis)[0];
real upperisection = intersect(lngraph, yEqualsOne)[0];

// Fill the region.
fill((0,0) --
     subpath(lngraph, lowerisection, upperisection)
     -- (0,1) -- cycle,
     0.5*gray + 0.5*white);

// Draw the paths.
draw(yEqualsOne);
draw(lngraph, L=Label("$y=\ln x$", EndPoint, align=NW));

// Draw the axes.
draw(xaxis, L=Label("$x$", EndPoint));
draw(yaxis, L=Label("$y$", EndPoint));

// Add the ticks.
real ticksize = 2pt / unit;
for (int x = 1; x <= 4; ++x)
  draw((x,ticksize) -- (x,-ticksize), L=Label("$"+(string)x+"$",EndPoint));
for (int y = 1; y <= 2; ++y)
  draw((ticksize,y) -- (-ticksize,y), L=Label("$" + (string)y + "$", EndPoint));

// And the final label.
label("$\mathcal{D}$", (.75,.5));
\end{asy}
\end{document}

结果:

答案3

以下是 pgfplots 的示例。由于v1.10 它提供了fillbetween,可用于填充曲线之间的区域。由于v1.11 你不需要说axis cs:不再,对于像这里这样的自定义注释。

我只是用它用白色填充 ln 曲线下方的区域,因此从填充的矩形区域中删除该部分以获得所需的形状。

\documentclass[margin=5pt]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween,decorations.softclip}
\pgfplotsset{compat = newest}
\begin{document}
\begin{tikzpicture}
  \pgfdeclarelayer{pre main}
  \pgfsetlayers{pre main,main}
  \begin{axis}[
      axis lines = middle,
      axis equal,
      enlargelimits,
      domain  = 0:4,
      xlabel  = {x},
      ylabel  = {y},
      xmin    = -0.5,
      xmax    = 4,
      ymin    = -1,
      ymax    = 2,
      samples = 300,
      mark    = none,
    ]
    \pgfonlayer{pre main}
    \fill[black!30] (0,0) rectangle (e,1);
    \endpgfonlayer
    \addplot [name path=ln, thin]  {ln(x)};
    \addplot [thin]  {1};
    \addplot [name path=null, draw=none]  {0};
    \addplot[color=white] fill between[of=null and ln,
      soft clip={domain=1:e}];
    \node at (0.7,0.5) {$\mathcal{D}$};
    \node at (3.2,1.6) {$y = \ln(x)$};
  \end{axis}
\end{tikzpicture}
\end{document}

填充区域

答案4

使用 PSTricks。

\documentclass[preview,border=12pt]{standalone}
\usepackage{pst-plot,pst-eucl}
\psset{saveNodeCoors}
\def\f{x ln}
\begin{document}
\begin{pspicture}(-.5,-1.5)(5,3)
    \pstInterFF[PointSymbol=none,PointName=none]{\f}{1}{3}{A}
    \pscustom*[linecolor=gray]{\psplot{0}{N-A.x}{1}\psplot{N-A.x}{1}{\f}\psplot{1}{0}{0}\closepath}
    \psaxes(0,0)(-.5,-1.5)(4.5,2.5)[$x$,0][$y$,90]
    \psplot{.3}{4}{\f}
    \psplot{0}{4}{1}
\end{pspicture}
\end{document}

在此处输入图片描述

最新更新

带有中缀形式。

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot,pst-eucl}

\def\f{(ln(x))}

\pstVerb{/I2P {AlgParser cvx exec} def}

\begin{document}
\begin{pspicture}[saveNodeCoors](-.5,-1.5)(5,3)
    \pstInterFF[PointSymbol=none,PointName=none]{\f I2P}{1}{3}{A}
    \pscustom*[linecolor=gray]{\psplot{0}{N-A.x}{1}\psplot{N-A.x}{1}{\f I2P}\psplot{1}{0}{0}\closepath}
    \psaxes(0,0)(-.5,-1.5)(4.5,2.5)[$x$,0][$y$,90]
    \psplot{.3}{4}{\f I2P}
    \psplot{0}{4}{1}
\end{pspicture}
\end{document}

相关内容