4

I have to draw below figure. But I am not able to put the lines inside the rectangular boxes, of respective colors as written inside the boxes. I also would like to have text, maybe color name here, but without any lines crossing the text(as I try to show it in case of RED and YELLOW).enter image description here My MWE

\documentclass[border=2mm]{standalone}

\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
    \begin{tikzpicture}
    \draw (0,0) rectangle (1,3);
    \shade[top color=yellow, bottom color=black] (0,0) rectangle (2,-1);
    \filldraw[fill=green!20!white, draw=green!40!black] (0,0) rectangle (2,1);
    \draw (0,0) rectangle (3,1);
    \draw (2,0) rectangle (5,1);
    \draw (2,0) rectangle (3,3);
    \end{tikzpicture}
\end{document}
6

Easy with the patterns library (section 60 of the pgfmanual).

\documentclass[margin=3.14mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,patterns}
\begin{document}
    \begin{tikzpicture}
    \shade[top color=yellow, bottom color=black] (0,0) rectangle (2,-1);
    \filldraw[fill=green!20!white, draw=green!40!black] (0,0) rectangle (2,1);
    \draw[pattern=north east lines,pattern color=red] (0,0) rectangle (1,3);
    \draw[pattern=north west lines,pattern color=blue] (0,0) rectangle (3,1);
    \draw[pattern=vertical lines] (2,0) rectangle (5,1);
    \draw[pattern=horizontal lines,pattern color=yellow] (2,0) rectangle (3,3);
    \end{tikzpicture}
\end{document}

enter image description here

EDIT: With annotations. You only need to modify the mynode style to change the behavior. I also made the boundaries have the same color as the lines.

\documentclass[margin=3.14mm]{standalone}
\usepackage{tikz}
\usepackage{contour}
\contourlength{1pt}
\tikzset{mynode/.style args={#1 | #2}{midway,%
node contents={\contour{white}{\textcolor{#1}{#2}}},%
font=\small,inner sep=0pt}}
\usetikzlibrary{calc,patterns}
\begin{document}
    \begin{tikzpicture}
    \shade[top color=yellow, bottom color=black] (0,0) rectangle (2,-1);
    \filldraw[fill=green!20!white, draw=green!40!black] (0,0) rectangle (2,1);
    \draw[pattern=north east lines,pattern color=red,draw=red] (0,0) rectangle (1,3)
    node[mynode=red | hello];
    \draw[pattern=north west lines,pattern color=blue,draw=blue] (0,0) rectangle (3,1)
    node[mynode=blue | ducks];
    \draw[pattern=vertical lines] (2,0) rectangle (5,1) 
    node[mynode=black | hello];
    \draw[pattern=horizontal lines,pattern color=yellow,draw=yellow] (2,0) rectangle (3,3)
    node[mynode=yellow | world];
    \end{tikzpicture}
\end{document}

enter image description here

  • Great one! I also would like to have text, maybe color name here, but without any lines crossing the text. Can it be done? – Shamina Jul 17 '18 at 11:43
  • @Shamina Sure. Text color black or the same color as the lines? – marmot Jul 17 '18 at 11:50
  • 1
    @Shamina Done. If you change your mind, you only need to adjust the mynode style. – marmot Jul 17 '18 at 12:00
  • 1
    I added another version which takes two arguments. – marmot Jul 17 '18 at 12:19
  • 1
    @Shamina That's easy, just add draw=red, say. I updated my answer accordingly. – marmot Jul 17 '18 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.