如何绘制和阴影集合?

如何绘制和阴影集合?

假设$x$$z$是区间内的变量$[0,1]^2$。如何绘制以下集合并对其进行着色?

$c1 \quad if 0<x<0.25,0<z<0.25$
$c2 \quad if 0.25<x<0.5,0.25<z<0.5$ 
$c3 \quad if 0.5<x<0.75,0.5<z<0.75$
$c4 \quad if 0.75<x<1,0.75<z<1$

答案1

这些是具有左下角坐标(x_min,z_min)和右上角坐标的简单矩形(x_max,z_max)

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[scale=pi]
 \draw[stealth-stealth] (0,1.1) node[below left]{$z$} |- (1.1,0) node[below left]{$x$}; 
 \foreach \Color[count=\X] in {blue,red,orange,cyan}
 {\fill[\Color] (\X*0.25-0.25,\X*0.25-0.25) rectangle (\X*0.25,\X*0.25);}
\end{tikzpicture}
\end{document}

在此处输入图片描述

至于评论中的附加项目:是的,您可以轻松添加这些。

\documentclass[tikz,border=3mm]{standalone}
\begin{document}
\begin{tikzpicture}[scale=4]
 \draw[stealth-stealth] (0,1.2) node[below left]{$z$} |- node[below left]{$0$}(1.2,0) node[below left]{$x$}; 
 \foreach \Color[count=\X] in {blue,red,orange,cyan}
 {\fill[\Color] (\X/4-1/4,\X/4-1/4) rectangle (\X/4,\X/4)
 node[midway,white]{$C_\X$};
 \draw (\X/4,0.05) -- ++ (0,-0.1)
    node[below]{$\pgfmathparse{\X/4}\pgfmathprintnumber\pgfmathresult$}
    (0.05,\X/4) -- ++ (-0.1,0)
    node[left]{$\pgfmathparse{\X/4}\pgfmathprintnumber\pgfmathresult$};}
\end{tikzpicture}
\end{document}

在此处输入图片描述

但是,如果您计划绘制多个这样的轴,则切换到 是有意义的pgfplots

答案2

根据您的问题,您可以使用链中的方形来绘制图表:

\documentclass[tikz, margin=3.141592]{standalone}
\usetikzlibrary{arrows.meta,
                chains}

\begin{document}
    \begin{tikzpicture}[
node distance = 0pt,
  start chain = going above right,
     N/.style = {fill=#1, minimum size=10mm, outer sep=0pt, 
                 anchor=south west, on chain},
    tl/.style = {% tick label
                 font=\footnotesize, inner sep=2pt}
                        ]
\foreach \i [count=\j] in {teal,cyan,orange,red}
{
\node[N=\i] {$C_{\j}$};
% ticks
\pgfmathsetmacro{\k}{0.25*\j}
\draw   (\j,2pt) -- ++ (0,-4pt) node[tl, below] {\k};
\draw   (2pt,\j) -- ++ (-4pt,0) node[tl,  left] {\k};
}
% axis
\draw[Stealth-Stealth]  (0,4.8) node[below left]{$z$} |-
                        (4.8,0) node[below left]{$x$}
                                node[tl,pos=0.5, below left] {0};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容