如何在 Tikz/PGF 中的 \draw 命令内使用填充?

如何在 Tikz/PGF 中的 \draw 命令内使用填充?

我对 Tikz 和 PGFplots 还不是很熟悉,所以如果这个问题比较幼稚,请原谅。我一直在尝试填充这个手动曲线下的区域,我尝试使用\clip\draw[clip...]\fillbetween以及我能找到的所有其他方法,但这是我能做的最好的,

在此处输入图片描述 其代码如下:

\documentclass[a4paper,11pt]{article}
\usepackage[top=1cm, bottom=1.5cm, left=0.7cm, right=0.7cm]{geometry}

\usepackage{tikz}
\usepackage{pgfplots}
\newcommand\bmmax{2}
\usepackage{graphicx,amsmath,amsfonts,bm}
\sloppy

\usepackage[T1]{fontenc}
\usepackage{scalefnt}
%\usepackage[scale]{tgheros}\usepackage{tgtermes}\usepackage[lite,subscriptcorrection,slantedGreek]{mtpro2}

\usepackage[]{arev}\renewcommand*\familydefault{\sfdefault}\usepackage[]{helvet}\usepackage[helvet]{sfmath}
%\usepackage[]{newtxmath}\renewcommand*\familydefault{\sfdefault}\usepackage[scaled=0.92]{helvet}\usepackage[helvet]{sfmath}

%% My shortcuts for frequently used symbols
\DeclareMathOperator{\dif}{d\!}
\newcommand{\pd}[3][]{\frac{\partial^{#1} #2}{\partial #3^{#1}}}
\newcommand{\od}[3][]{\frac{\dif{^{#1}}#2}{\dif{#3^{#1}}}}

\DeclareMathOperator{\Prsym}{\mathrm{Pr}}
\newcommand{\pr}[1]{\Prsym\mathopen{}\left(#1\right)\mathclose{}}
\DeclareMathOperator{\E}{\mathbb{E}}
\DeclareMathOperator{\var}{\mathrm{Var}}
\DeclareMathOperator{\mse}{\mathrm{MSE}}




%% Nice colors from ColorBrewer

\definecolor{cbs11}{RGB}{228,26,28}
\definecolor{cbs12}{RGB}{55,126,184}
\definecolor{cbs13}{RGB}{77,175,74}


% Define function to create length-like variable, use \nvar{\dx}{1pt}
% to declare \dx=1pt

\newcommand{\nvar}[2]{%
 \newlength{#1}
 \setlength{#1}{#2}
}

% For the main result (4-plot) 
\nvar{\gheight}{6.2cm}
\nvar{\gwidth}{7.5cm}
\nvar{\gsafedist}{0.25cm}


% Simple plot with near-square dimensions for dependencies
\nvar{\gbheight}{5.5cm}
\nvar{\gbwidth}{6.4cm}
\nvar{\gbsafedist}{0.2cm}

\pgfplotsset{compat=1.8}
\usetikzlibrary{pgfplots.external, chains, decorations.pathreplacing,
arrows, shapes, backgrounds, calc, positioning}
\tikzset{external/force remake}
\tikzexternalize
\usepgfplotslibrary{colormaps}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\begin{tikzpicture}[thick, x=\gwidth/12, y=\gheight/12]
\begin{scope}[yshift=9cm]

%FOR THE KERNEL ESTIMATE

\draw[thick, -] (2, -18.7) -- (2,-23);
\draw[thick, -] (2,-23) -- (22, -23);
\foreach \x/\xs in {-23/0, -21/30, -19/60}{\draw[thin] (2,\x) node[left]{\xs} -- (1.8,\x);}
\foreach \x/\xs in {2/0, 12/5, 22/10}{\draw[thin] (\x,-23) node[below]{\xs} -- (\x,-23);}
\draw[dashed, black, -] (2,-22.2) -- (3.6,-22.2) -- (3.6,-20) -- (7.8,-20) -- (7.8,-22.2) -- (09,-22.2) --(09,-20) -- (9.6,-20) --(9.6,-22.2) -- (10.6,-22.2) --(10.6, -20) -- (11, -20) --(11,-22.2) -- (22, -22.2);

%Density Function:
\begin{scope}
 \draw[fill=cbs12!20, fill opacity = 0.2, ultra thick, cbs12] plot [smooth, tension =0.5] coordinates{(2,-22.6) (2.6, -22.4) (3.2, -22.3) (3.6, -21.5) (3.8, -21) (4.0, -21) (4.5, -21.5) (4.8, -20.5) (5.5, -20.7) (5.8, -19.9) (6.3, -20.1) (6.6, -20.7) (6.7, -21.1) (7.1, -21.5) (7.5, -20.8) (7.8, -20.9) (8.2, -20.5) (8.7, -20.6) (9.4, -20.4) (9.8, -22.2) (10.2, -22.2) (10.6, -20.5) (10.8, -20.7) (11.2, -21.1) (11.5, -22.3) (12.0, -22) (12.5, -22) (13.2, -21.4) (13.6, -22.1) (13.9, -22) (14.2, -22) (14.5, -22.1) (14.9, -22.5) (15.4, -21.2) (15.6, -21.2) (15.9, -22.5) (16.6, -22.0) (17, -22.1) (17.4, -21.9) (17.7, -21.7) (18, -21.8) (18.4, -21.7) (18.9, -22.1) (19.3, -22.3) (19.7, -21.65) (20, -21.7) (20.4, -22.1) (20.6, -22) (21.0, -22.3) (21.3, -22.4) (21.7, -21.7) (22, -21.4)} ;


\end{scope}

\end{scope}



\end{tikzpicture}

\end{document}

我从这段代码中得到的图片确实填充了曲线下方(带有蓝色边界),但填充没有完全到达 x 轴,而是在曲线的一半处超出了曲线。我不知道如何纠正这个问题。请帮忙。

答案1

我得到了这个

在此处输入图片描述

\begin{scope}
 \draw[fill=cbs12!20, fill opacity = 0.2, ultra thick, cbs12] (2,-23) -- plot [smooth,
 tension =0.5] coordinates{(2,-22.6) (2.6, -22.4) (3.2, -22.3) (3.6, -21.5)
   (3.8, -21) (4.0, -21) (4.5, -21.5) (4.8, -20.5) (5.5, -20.7) (5.8, -19.9)
   (6.3, -20.1) (6.6, -20.7) (6.7, -21.1) (7.1, -21.5) (7.5, -20.8) (7.8,
   -20.9) (8.2, -20.5) (8.7, -20.6) (9.4, -20.4) (9.8, -22.2) (10.2, -22.2)
   (10.6, -20.5) (10.8, -20.7) (11.2, -21.1) (11.5, -22.3) (12.0, -22) (12.5,
   -22) (13.2, -21.4) (13.6, -22.1) (13.9, -22) (14.2, -22) (14.5, -22.1)
   (14.9, -22.5) (15.4, -21.2) (15.6, -21.2) (15.9, -22.5) (16.6, -22.0) (17,
   -22.1) (17.4, -21.9) (17.7, -21.7) (18, -21.8) (18.4, -21.7) (18.9, -22.1)
   (19.3, -22.3) (19.7, -21.65) (20, -21.7) (20.4, -22.1) (20.6, -22) (21.0,
   -22.3) (21.3, -22.4) (21.7, -21.7) (22, -21.4)} -- (22,-23) ;


\end{scope}

但正如你所看到的,我必须猜测相对于标签移动的坐标。

而且肯定还有更好的方法。


更新

我可以通过在源中进行一些重复来实现目标:

%Density Function:
\begin{scope}
  \fill[cbs12, opacity = 0.2]
  (2, -23)
  -- plot [smooth, tension =0.5] coordinates{(2,-22.6) (2.6, -22.4)
    (3.2, -22.3) (3.6, -21.5) (3.8, -21) (4.0, -21) (4.5, -21.5) (4.8, -20.5)
    (5.5, -20.7) (5.8, -19.9) (6.3, -20.1) (6.6, -20.7) (6.7, -21.1) (7.1,
    -21.5) (7.5, -20.8) (7.8, -20.9) (8.2, -20.5) (8.7, -20.6) (9.4, -20.4)
    (9.8, -22.2) (10.2, -22.2) (10.6, -20.5) (10.8, -20.7) (11.2, -21.1)
    (11.5, -22.3) (12.0, -22) (12.5, -22) (13.2, -21.4) (13.6, -22.1) (13.9,
    -22) (14.2, -22) (14.5, -22.1) (14.9, -22.5) (15.4, -21.2) (15.6, -21.2)
    (15.9, -22.5) (16.6, -22.0) (17, -22.1) (17.4, -21.9) (17.7, -21.7) (18,
    -21.8) (18.4, -21.7) (18.9, -22.1) (19.3, -22.3) (19.7, -21.65) (20,
    -21.7) (20.4, -22.1) (20.6, -22) (21.0, -22.3) (21.3, -22.4) (21.7, -21.7)
    (22, -21.4)} -- (22, -23) -- cycle;

  \draw[ultra thick, cbs12] 
    plot [smooth, tension =0.5] coordinates{(2,-22.6) (2.6, -22.4)
    (3.2, -22.3) (3.6, -21.5) (3.8, -21) (4.0, -21) (4.5, -21.5) (4.8, -20.5)
    (5.5, -20.7) (5.8, -19.9) (6.3, -20.1) (6.6, -20.7) (6.7, -21.1) (7.1,
    -21.5) (7.5, -20.8) (7.8, -20.9) (8.2, -20.5) (8.7, -20.6) (9.4, -20.4)
    (9.8, -22.2) (10.2, -22.2) (10.6, -20.5) (10.8, -20.7) (11.2, -21.1)
    (11.5, -22.3) (12.0, -22) (12.5, -22) (13.2, -21.4) (13.6, -22.1) (13.9,
    -22) (14.2, -22) (14.5, -22.1) (14.9, -22.5) (15.4, -21.2) (15.6, -21.2)
    (15.9, -22.5) (16.6, -22.0) (17, -22.1) (17.4, -21.9) (17.7, -21.7) (18,
    -21.8) (18.4, -21.7) (18.9, -22.1) (19.3, -22.3) (19.7, -21.65) (20,
    -21.7) (20.4, -22.1) (20.6, -22) (21.0, -22.3) (21.3, -22.4) (21.7, -21.7)
    (22, -21.4)} ;


\end{scope}

我尝试从 TikZ 手册中自学“路径”,但由于它有效,我推迟了进一步的学习。

在此处输入图片描述

相关内容