5

Ok so I have been trying to draw this figure: enter image description here

enter image description here

I have made it till the above diagram. The three captions that I made in the topmost picture is what that is remaining. 1. The outer boundary of the rectangle should also be colored blue or some color. 2. The small circles should be labeled with a matrix. 3. The rectangle should be filled with horizontal lines

Here is the code for what I have done:

    \documentclass{article} % say
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw  (0,5) node (yaxis) [above] {$y$}
|- (5,0) node (xaxis) [right] {$x$};
\draw (-0.5,0) -- (4,0);
\draw (0,-0.5) -- (0,4);
\definecolor{mycolor}{RGB}{178,181,255}
\filldraw[fill=mycolor] (0,0) rectangle (2,2);
\filldraw [cyan] (0,2) circle [radius=2pt];
\filldraw [cyan] (2,0) circle [radius=2pt];

\end{tikzpicture}
\end{document}

Any help would be appreciated.

7

Adding the labels is fairly standard: just add node[...]{label} to the points.

To draw your lines in the square using the tikz patterns library and adding pattern=horizontal lines to your draw command. This gives you the following:

enter image description here

Here is the code:

\documentclass{article} % say
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{patterns}
\definecolor{mycolor}{RGB}{178,181,255}

\begin{document}
  \begin{tikzpicture}
    \draw  (0,5) node (yaxis) [above] {$y$}
    |- (5,0) node (xaxis) [right] {$x$};
    \draw (-0.5,0) -- (4,0);
    \draw (0,-0.5) -- (0,4);
    \draw[preaction={fill=mycolor},
          pattern=horizontal lines,
          pattern color=cyan] (0,0) rectangle (2,2);
    \draw[cyan, thick](0,2)--(2,2)--(2,0);
    \draw[fill=cyan](0,2)circle[radius=2pt]node[left]{$\begin{bmatrix}1\\0\end{bmatrix}$};
    \draw[fill=cyan](2,0)circle[radius=2pt]node[below]{$\begin{bmatrix}0\\1\end{bmatrix}$};
  \end{tikzpicture}
\end{document}

A few comments.

  • I have used a preaction to put in the background/fill colour of the square before the pattern is drawn
  • pattern color=cyan is used to set the colour of the horizontal lines
  • rather than using \filldraw[cyan] for your "coordinate dots" I have used \draw[fill=cyan] because otherwise the node labels are also coloured cyan.
  • your x and y axes are probably a bit long but I haven't doctored these.
  • I have draw the cyan boundary of the square after the square is drawn so as not to colour the boundary of the square on the axes.

The horizontal lines inside the square strike me as being a little "patchy". If this bothers you then rather than using a pattern you could instead draw the lines "by hand" using a for loop. This gives

enter image description here

with the code now looking like:

\documentclass{article} % say
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{patterns}
\definecolor{mycolor}{RGB}{178,181,255}

\begin{document}
  \begin{tikzpicture}
    \draw  (0,5) node (yaxis) [above] {$y$}
    |- (5,0) node (xaxis) [right] {$x$};
    \draw (-0.5,0) -- (4,0);
    \draw (0,-0.5) -- (0,4);
    \draw[fill=mycolor] (0,0) rectangle (2,2);
    \foreach \y in {0,...,9}{
        \draw[cyan,very thin](0,0.\y)--++(2,0);
        \draw[cyan,very thin](0,1.\y)--++(2,0);
    }
    \draw[cyan, thick](0,2)--(2,2)--(2,0);
    \draw[fill=cyan](0,2)circle[radius=2pt]node[left]{$\begin{bmatrix}1\\0\end{bmatrix}$};
    \draw[fill=cyan](2,0)circle[radius=2pt]node[below]{$\begin{bmatrix}0\\1\end{bmatrix}$};
  \end{tikzpicture}
\end{document}

The only additional comment worth making here is that because \foreach seems to prefer integers, the loop above is just a way of "tricking" tikz to draw lines of heights 0, 0.1, 0.2, ..., 1.9 and 2. (I'm actually slightly surprised, and impressed, that tikz/pgf parses 0.\y and 1.\y correctly!)

  • Oh my God you are a life savior , Thank you for such a good explanation. :) appreciated – Saad Oct 6 '16 at 6:21

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.