Tikz:为区域着色

Tikz:为区域着色

我使用以下代码绘制了一幅图。我想让 (0,7)、(0,11)、(3,8)、(3,4) 内的区域变为红色,让其上方的区域变为灰色,但我不知道该怎么做。

\documentclass{article}
\usepackage{tikz}
\begin{document}
\pagestyle{empty}
\begin{tikzpicture}[scale=0.5]

\draw (1.05cm,2pt) node[above];
 %  {$\displaystyle\int_0^{3/2} \!\!x^2\mathrm{d}x$};

\draw[style=help lines] (0,0) grid (3.9,11.9);
  % [step=0.25cm]      (1,2) grid +(1,1);

\draw[->] (-1,0) -- (4,0) node[right] {$i$};
\draw[->] (0,-1) -- (0,12) node[above] {$j$};

\foreach \x/\xtext in {1/1, 2/2, 3/3}
\draw[shift={(\x,0)}] (0pt,2pt) -- (0pt,-2pt) node[below] {$\xtext$};

\foreach \y/\ytext in {1/, 2/, 3/,4/,5/,6/,7/7,8/, 9/, 10/, 11/11}
\draw[shift={(0,\y)}] (2pt,0pt) -- (-2pt,0pt) node[left] {$\ytext$};

\draw  {(0,11) -- (3,8)} node[below right] ;
\draw  {(0,7) -- (3,4)} node[below right] ;
\draw  {(3,8) -- (3,4)} node[below right] ;
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案1

对于红色形状,您可以尝试一下。我不确定您在这种情况下所说的“上方”是什么意思。

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

\begin{tikzpicture}[scale=0.5]

\draw (1.05cm,2pt) node[above]{};
 %  {$\displaystyle\int_0^{3/2} \!\!x^2\mathrm{d}x$};

\draw[style=help lines] (0,0) grid (3.9,11.9);
  % [step=0.25cm]      (1,2) grid +(1,1);

\draw[->] (-1,0) -- (4,0) node[right] {$i$};
\draw[->] (0,-1) -- (0,12) node[above] {$j$};

\foreach \x/\xtext in {1/1, 2/2, 3/3}
\draw[shift={(\x,0)}] (0pt,2pt) -- (0pt,-2pt) node[below] {$\xtext$};

\foreach \y/\ytext in {1/, 2/, 3/,4/,5/,6/,7/7,8/, 9/, 10/, 11/11}
\draw[shift={(0,\y)}] (2pt,0pt) -- (-2pt,0pt) node[left] {$\ytext$};

\draw[fill=red]  (0,11) -- (3,8) -- (3,4) -- (0,7) -- cycle;
\fill[gray!20,nearly transparent] (0,11) -- (0,12) -- (3,12) -- (3,8) -- cycle;
% \fill[gray!20] (0,11) -- (0,12) -- (3,12) -- (3,8) -- cycle; %without transparency
\end{tikzpicture}

\end{document}

红色的

例如,您可以使用键。nearly transparent有关更多详细信息,请参阅 pgfmanual Ch. 20.2。opacityopacity=0.3

相关内容