2

I want to draw such a rectangle. -> enter image description here

  1. Empty One
  2. Crosshatching

Thank you in advance.

 \documentclass{article}
    \usepackage{pgfplots}
    %\pgfplotsset{compat=1.14}
    \begin{document}
    \begin{tikzpicture}
      \begin{semilogxaxis}
        [
            enlarge x limits=false,
            no marks,
            grid=none,
            xmin=1e4, xmax=22260785,
            ymin=0, ymax=120,
            ylabel={$\sigma_{a}$},
            xlabel={$N$},
            samples=400 
         ]
           \begin{scope}[green] 
             \draw[orange,dashed] ({axis cs:50045,0}|-{rel axis cs:0,1}) -- ({axis cs:50045,0}|-{rel axis cs:0,0});
             \draw[dashed,green] ({rel axis cs:1,0}|-{axis cs:0,18.385735235}) -- ({rel axis cs:0,0}|-{axis cs:0,18.385735235});
           \end{scope}
      \end{semilogxaxis}
    \end{tikzpicture}
    \end{document}
1

Well, knowing about axis cs and rel axis cs you're kind of halfway to the solution. Note that if you have \pgfplotsset{compat=1.11} or higher, axis cs is default, so you don't have to specify it.

Anyways, to draw a rectangle, use

\draw (x1,y1) rectangle (x2,y2);

To fill a rectangle with diagonal lines, add \usetikzlibrary{patterns}, and

\fill [pattern=north east lines] (x1,y1) rectangle (x2,y2);

pattern=north west lines will give you lines tilted the other way. If you want to draw the border as well, use \filldraw, change the color of the border with draw=<color>.

To cross hatch the lower left corner for example, you can use

\filldraw [draw=red,pattern=north east lines] (rel axis cs:0,0) rectangle (50045,18.385735235); 

Note that if compat is set to 1.10 or lower, or you have a version of pgfplots that is older than 1.11, you have to use axis cs: for the second coordinate, i.e.

\filldraw [draw=red,pattern=north east lines] (rel axis cs:0,0) rectangle (axis cs:50045,18.385735235); 

Some other examples:

enter image description here

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14} %don't need axis cs: with 1.11 or higher
\usetikzlibrary{patterns}
\begin{document}
\begin{tikzpicture}
  \begin{semilogxaxis}
    [
        enlarge x limits=false,
        no marks,
        grid=none,
        xmin=1e4, xmax=22260785,
        ymin=0, ymax=120,
        ylabel={$\sigma_{a}$},
        xlabel={$N$},
        samples=400 
     ]

         \draw[orange,dashed] ({50045,0}|-{rel axis cs:0,1}) -- ({50045,0}|-{rel axis cs:0,0});
         \draw[dashed,green] ({rel axis cs:1,0}|-{0,18.385735235}) -- ({rel axis cs:0,0}|-{0,18.385735235});

         \filldraw [draw=red,pattern=north east lines] (1e5,20) rectangle (1e6,40);
         \draw [thick,blue] (1e6,40) rectangle (1e7,60);
  \end{semilogxaxis}
\end{tikzpicture}
\end{document}

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.