有没有办法填充我使用 tikz 制作的两条曲线之间的区域,而无需使用 addplot-fillbetween 包?
\usepackage{xltxtra}
\usepackage{xgreek}
\usepackage{mathtools}
\usepackage{amscd}
\usepackage{amsthm}
\usepackage[mathup=sym]{unicode-math}
\usepackage{tikz}
\usepackage{makeidx}
\usepackage{graphicx}
\usepackage{pgf,tikz}
\usepackage{tkz-tab}
\usetikzlibrary{shapes,arrows,backgrounds}
\usetikzlibrary{scopes,svg.path,shapes.geometric,shadows}
\usepackage[left=1.5cm,right=1.5cm,top=2cm,bottom=1.5cm]{geometry}
\usepackage{kmath,kerkis}
\setmainfont[Mapping=tex-text]{Kerkis}
\setmathfont[Scale=MatchUppercase]{STIX Two Math}
\begin{tikzpicture}[scale=5.6]
\draw[very thick,dashed, opacity=0.7] (1,0)--(1,0.36784);
\draw[very thick,dashed, opacity=0.7] (2,0)--(2,0.25);
\draw[very thick, ->] (-0.5,0) -- (2.3,0)node[pos=1,below]{$x$};
\draw[very thick, ->] (0,-0.5) -- (0,0.85)node[pos=1,left]{$y$};
\foreach \x in {1,...,2}\draw[shift={(\x,0)},color=black]
(0pt,0.8pt) -- (0pt,-0.8pt);
\foreach \i in {1,2}
\draw[shift={(\i,0)},color=black](0pt,0pt) -- (0pt,-0.8pt)node[below]{$\i$};
\node[below right](o) at (0,0){$0$};
\draw[ultra thick, samples=100, draw=red, domain=-0.35:2] plot (\x,{\x*e^(-\x)});
\draw[ultra thick, samples=100, draw=ballblue!60!black, domain=-0:1] plot (\x,{sqrt(\x)*e^(-sqrt(\x))});
\node[circle, draw=black, inner sep=1.7pt, fill=white](a) at (2,0.27){};
\node[circle, draw=black, inner sep=1.4pt,fill=black!50!white](a) at (1,0.36787){};
\node[circle, draw=black, inner sep=1.4pt,fill=black!50!white](a) at (0,0){};
\node[red]() at (1.7,0.4){\begin{LARGE}
$C_{\,f}$
\end{LARGE}};
\node[ballblue!60!black,below]() at (0.12,0.45){\begin{LARGE}
$C_{\,g}$
\end{LARGE}};
\end{tikzpicture}
答案1
清理了大量代码后,我添加了几行自己的代码,以便得到一个可以回答你的问题的工作程序:
\documentclass[a4paper]{article}
\usepackage{tikz}
\usepackage[left=1.5cm,right=1.5cm,top=2cm,bottom=1.5cm]{geometry}
\begin{document}
\thispagestyle{empty}
\begin{tikzpicture}[scale=4]
\draw[very thick,dashed, opacity=0.7] (1,0)--(1,0.36784);
\draw[very thick,dashed, opacity=0.7] (2,0)--(2,0.25);
\draw[very thick, ->] (-0.5,0) -- (2.3,0)node[pos=1,below]{$x$};
\draw[very thick, ->] (0,-0.5) -- (0,0.85)node[pos=1,left]{$y$};
\foreach \x in {1,...,2}\draw[shift={(\x,0)},color=black]
(0pt,0.8pt) -- (0pt,-0.8pt);
\foreach \i in {1,2}
\draw[shift={(\i,0)},color=black](0pt,0pt) -- (0pt,-0.8pt)node[below]{$\i$};
\node[below right](o) at (0,0){$0$};
\draw[ultra thick, samples=100, draw=red, domain=-0.35:2] plot (\x,{\x*e^(-\x)});
\draw[ultra thick, samples=100, draw=blue!60!black, domain=-0:1] plot (\x,{sqrt(\x)*e^(-sqrt(\x))});
\node[circle, draw=black, inner sep=1.7pt, fill=white](a) at (2,0.27){};
\node[circle, draw=black, inner sep=1.4pt,fill=black!50!white](a) at (1,0.36787){};
\node[circle, draw=black, inner sep=1.4pt,fill=black!50!white](a) at (0,0){};
\node[red] at (1.7,0.4) () {$C_{f}$};
\node[blue!60!black,below] at (0.12,0.45) () {$C_{g}$};
\foreach \x in {0.000,0.001,...,1.000} % <--- added
\draw[cyan] (\x,{\x*e^(-\x)})--(\x,{sqrt(\x)*e^(-sqrt(\x))}); % <--- added
\end{tikzpicture}
\end{document}
输出:
添加:如果将“填充”代码放在“绘图”代码之前,结果会更好:
\foreach \x in {0.000,0.001,...,1.000} % <--- added
\draw[cyan] (\x,{\x*e^(-\x)})--(\x,{sqrt(\x)*e^(-sqrt(\x))}); % <--- added
\draw[ultra thick, samples=100, draw=red, domain=-0.35:2] plot (\x,{\x*e^(-\x)});
\draw[ultra thick, samples=100, draw=blue!60!black, domain=-0:1] plot (\x,{sqrt(\x)*e^(-sqrt(\x))});
\node[circle, draw=black, inner sep=1.7pt, fill=white](a) at (2,0.27){};
\node[circle, draw=black, inner sep=1.4pt,fill=black!50!white](a) at (1,0.36787){};
\node[circle, draw=black, inner sep=1.4pt,fill=black!50!white](a) at (0,0){};
\node[red] at (1.7,0.4) () {$C_{f}$};
\node[blue!60!black,below] at (0.12,0.45) () {$C_{g}$};
在这种情况下,输出是:
答案2
大部分内容与主题无关,但还是有帮助的 :-)
- 我会花一点精力来定义图像元素样式,这样就可以为图表编写更短的代码:
- 为了填充,使用@John Kormylo 评论,其中我将蓝色曲线的域定义为
samples at={0,0.01,...,0.2,0.3,...,1}
。 fill
如果先绘制图表,那么图表看起来会比曲线更美观(在我看来) 。- MWE(最小工作示例)是:
\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\begin{document}
\thispagestyle{empty}
\begin{tikzpicture}[scale=4,
arr/.style = {-Straight Barb},
wdt/.style = {circle, draw, solid, fill=white, inner sep=2pt},
tck/.style = {rounded corners, inner sep=2pt, fill=white, anchor=north}
]
% axis
\draw[arr] (-0.5,0) -- (2.3,0) node[below left] {$x$};
\draw[arr] (0,-0.5) -- (0,0.8) node[below left] {$y$};
% fill between curves
\fill[cyan] plot[samples at={0,0.01,...,0.2,0.3,...,1}] (\x,{sqrt(\x)*e^(-sqrt(\x))}) --
plot[samples=100, domain=1:0] (\x,{\x*e^(-\x)});
% curves
\draw[ultra thick, samples=100, red] plot[domain=-0.35:2] (\x,{\x*e^(-\x)});
\draw[ultra thick, samples=100, blue!60!black] plot[domain=-0:1] (\x,{sqrt(\x)*e^(-sqrt(\x))});
% circles + dashed line
\foreach \x in {0,1,2}
\draw[dashed] (\x,{\x*e^(-\x)}) node (\x) [wdt] {} -- (\x |- 0,-1pt) node[tck] {\x};
\end{tikzpicture}
\end{document}
答案3
附有 John Kormylo 的评论和帖子使用TikZ填充曲线和圆之间的区域 我试过:
\fill[cyan,draw=black] plot[smooth, domain=0:1] (\x,{\x*e^(-\x)}) plot [smooth, domain=1:0](\x,{sqrt(\x)*e^(-sqrt(\x))});
\documentclass[border=3mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
% initial answer
% \fill[cyan,draw=black] plot[smooth, samples=2000,domain=0:1] (\x,{\x*e^(-\x)}) plot [smooth, samples=2000, domain=1:0](\x,{sqrt(\x)*e^(-sqrt(\x))});
%
% EDIT2
%
\fill[cyan,draw=black] plot[domain=0:1] (\x,{\x*e^(-\x)}) -- plot [ domain=1:0](\x,{sqrt(\x)*e^(-sqrt(\x))})--cycle;
\end{tikzpicture}
\end{document}
EDIT2:通过更加关注 Jasper Habicht 的评论,问题不在于数量,samples
而在于曲线之间的联系--
。--cycle
编辑3:有了Jasper Habicht的第二条评论,我终于明白了一切(呃,我想)。
\documentclass{article}
\usepackage{tikz}
\begin{document}
\section{Without \texttt{--} between the two curves}
\subsection{Without $samples=2^n+1$}
\begin{itemize}
\item $samples=25$ (by default)
\begin{tikzpicture}[scale=2]
\fill[cyan,draw=black] plot[domain=0:1] (\x,{\x*e^(-\x)}) plot [ domain=1:0](\x,{sqrt(\x)*e^(-sqrt(\x))});
\end{tikzpicture}
\item $samples=258$
\begin{tikzpicture}[scale=2]
\fill[cyan,draw=black] plot[samples=258,domain=0:1] (\x,{\x*e^(-\x)}) plot [ samples=258,domain=1:0](\x,{sqrt(\x)*e^(-sqrt(\x))});
\end{tikzpicture}
\item $samples=2000$
\begin{tikzpicture}[scale=2]
\fill[cyan,draw=black] plot[samples=2000,domain=0:1] (\x,{\x*e^(-\x)}) plot [ samples=2000,domain=1:0](\x,{sqrt(\x)*e^(-sqrt(\x))});
\end{tikzpicture}
It seems to work, but the reasoning is not good.
\end{itemize}
\subsection{With $samples=2^n+1$}
\begin{itemize}
\item $samples=33$
\begin{tikzpicture}[scale=2]
\fill[cyan,draw=black] plot[samples=33,domain=0:1] (\x,{\x*e^(-\x)}) plot [ samples=33,domain=1:0](\x,{sqrt(\x)*e^(-sqrt(\x))});
\end{tikzpicture}
\item $samples=129$
\begin{tikzpicture}[scale=2]
\fill[cyan,draw=black] plot[samples=129,domain=0:1] (\x,{\x*e^(-\x)}) plot [ samples=129,domain=1:0](\x,{sqrt(\x)*e^(-sqrt(\x))});
\end{tikzpicture}
\item $samples=257$
\begin{tikzpicture}[scale=2]
\fill[cyan,draw=black] plot[samples=257,domain=0:1] (\x,{\x*e^(-\x)}) plot [ samples=257,domain=1:0](\x,{sqrt(\x)*e^(-sqrt(\x))});
\end{tikzpicture}
\end{itemize}
\section{With \texttt{--} and \texttt{-cycle}, it always works}
\begin{itemize}
\item $samples=25$ (by default)
\begin{tikzpicture}[scale=2]
\fill[cyan,draw=black] plot[domain=0:1] (\x,{\x*e^(-\x)}) -- plot [ domain=1:0](\x,{sqrt(\x)*e^(-sqrt(\x))})--cycle;
\end{tikzpicture}
\end{itemize}
\begin{itemize}
\item $samples=258$ (by default)
\begin{tikzpicture}[scale=2]
\fill[cyan,draw=black] plot[samples=258,domain=0:1] (\x,{\x*e^(-\x)}) -- plot [ samples=258,domain=1:0](\x,{sqrt(\x)*e^(-sqrt(\x))})--cycle;
\end{tikzpicture}
\end{itemize}
\section{What I remember}
\begin{itemize}
\item Read comments carefully (A big thank you to Jasper Habicht)
\item Do not forget \texttt{--} and \texttt{-cycle}
\item Why not take $samples=2^n+1$
\end{itemize}
\end{document}
答案4
纯粹为了比较,下面是使用元帖子。
这已包含在内,luamplib
因此您需要使用 进行编译lualatex
。我在评论中添加了一些注释。
\documentclass[border=5mm]{standalone}
\usepackage{luamplib}
\begin{document}
\mplibtextextlabel{enable}
\begin{mplibcode}
beginfig(1);
numeric u; u = 144; % define a unit for scaling
path xx, yy; % define the axes
xx = (left -- 5 right) scaled 1/2 u;
yy = (down -- up) scaled 1/2 u;
% define the two curves
path ff, gg;
% in MP mexp(256x) is the same as exp(x) in the rest of the world
vardef f(expr x) = x * mexp(-256 x) enddef;
vardef g(expr x) = sqrt(x) * mexp(-256 sqrt(x)) enddef;
numeric s; s = 1/8;
ff = (for x = -3/8 step s until 2-s: (x, f(x)) .. endfor (2, f(2))) scaled u;
gg = (for x = 0 step s until 1-s: (x, g(x)) .. endfor (1, g(1))) scaled u;
% since we have set s=1/8, path ff will have 19 points, hence
% subpath (3, 11) gives us the segment between 0 and 1
% which can be spliced to gg running backwards and closed with cycle
fill subpath (3, 11) of ff & reverse gg & cycle withcolor 7/8[blue, white];
% draw and label the curves in colour
drawoptions(withcolor 2/3 blue);
draw gg; label.ulft("$C_g$", point 2 of gg);
drawoptions(withcolor 3/4 red);
draw ff; label.urt("$C_f$", point 17 of ff);
drawoptions();
% draw the axes, with slim arrows, and labels
interim ahangle := 30;
drawarrow xx; label.rt("$x$", point 1 of xx);
drawarrow yy; label.top("$y$", point 1 of yy);
% add fancy labels at 0, 1, and 2.
forsuffixes $ = 0, 1, 2:
z$ = ($, f($)) scaled u;
if $ > 0:
draw (x$,0) -- z$ dashed evenly scaled 1/2;
label.bot("$" & str $ & "$", (x$, 0));
else:
label.lrt("$" & str $ & "$", origin);
fi
draw z$ withpen pencircle scaled dotlabeldiam;
draw z$ withpen pencircle scaled 3/4 dotlabeldiam withcolor white;
endfor
endfig;
\end{mplibcode}
\end{document}