12

enter image description here

I want to draw this graph and this is the code that I wrote so far but the shape looks quite different... and I couldn't solve how to fill the area with dots... plz help me.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\begin{document}
\begin{figure}
\begin{tikzpicture}[scale=1]

% Axis

\draw (0,7) -- (0,0) node[below]{0} -- (7,0) node[below] {1};
\draw (0,7) -- (7,7)  -- (7,0);


% curve

\draw (0,0) to (7,7);
\draw [very thick] (0,0) to [out=90, in=180] (2,2);
\draw [very thick] (2,2) to [out=0, in=270] (4,4);
\draw [very thick] (4,4) to [out=90, in=180] (7,6);

\draw (0.3,0.3) to [out=0, in=-270] (0.5,0) ;
\node[right] at (0.6,0.25) {$45^{o}$};

\draw [dashed] (2,0) -- (2,2);
\draw [dashed] (4,0) -- (4,4);
\draw [dashed] (5.9,0) -- (5.9,5.9);


\node[below] at (2,0) {\large $\lambda_1$};
\node[below] at (4,0) {\large $\lambda_2$};
\node[below] at (5.9,0) {\large $\lambda_3$};


\draw[<-,>=latex] (2,6) node[above] {\LARGE \begin{tabular}{c}$D(e(\lambda)$)\end{tabular}} to[out=-90,in=-100] (4.05,4.4);
\node[above] at (1.9,6.5) {-};

\end{tikzpicture}

\end{figure}

\end{document}
2
  • Welcome to TeX.SX!
    – Bobyandbob
    May 5, 2018 at 8:38
  • Excuse me, but is there a good reason to add all those dots? In my opinion they are not adding any value to the figure, but rather make it less clear (the angle label for example). Sorry if this is not the correct place to place comments like this.
    – mickep
    May 5, 2018 at 14:54

6 Answers 6

16

As Henri mentioned it is possible to define your own pattern. In the code below one such definition is shown. You can change the size of the dots with the dots size key, e.g. \fill [pattern=mydots, dots size=3pt], and the spacing with dots spread, e.g. \fill [pattern=mydots, dots spread=15pt].

As Jasper, I used \bar{e} instead of drawing that bar manually, and I used an arc to draw the angle mark. I also used a loop to draw the three dashed lines with lambdas.

enter image description here

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{patterns}

\tikzset{
    dots size/.store in=\dotssize,
    dots size=1pt,
    dots spread/.store in=\dotsspread,
    dots spread=10pt
    }

\makeatletter
\pgfdeclarepatternformonly[\dotssize,\dotsspread]{mydots}
{\pgfpointorigin}
{\pgfpoint{\dotsspread}{\dotsspread}}
{\pgfpoint{\dotsspread}{\dotsspread}}
{
    \pgfsetcolor{\tikz@pattern@color}
    \pgfpathcircle{\pgfpoint{\dotsspread/2}{\dotsspread/2}}{\dotssize}
    \pgfusepath{fill}
}
\makeatother

\begin{document}
\begin{tikzpicture}

% Axis
\draw (7,7) -| (0,0) node[below]{0} -| node[below] {1} cycle;

% curve    
\draw (0,0) to (7,7);

\fill [pattern=mydots, opacity=0.7] (0,0) to [out=80,in=200] (2,2)
                           to [out=20, in=250] (4,4) 
                           to [out=70, in=200] (7,6.4)
                           |- cycle;
\draw [very thick] (0,0) to [out=80,in=200] (2,2)
                         to [out=20, in=250] (4,4) 
                         to [out=70, in=200] coordinate[pos=0.2] (m) (7,6.4);


\draw (0.5,0) arc[start angle=0,end angle=45,radius=0.5] node[midway,right] {$45^{\circ}$} ;

\foreach \i in {1,2,3}
   \draw [dashed,thick] (2*\i,2*\i) -- (2*\i, 0) node[below,font=\large] {$\lambda_{\i}$};

\draw[<-,>=latex] (2,6) node[above] {\LARGE $D(\bar{e}(\lambda)$)} to[out=270,in=200] (m);

\end{tikzpicture}
\end{document}

Calculate intersections

Not sure there is much point for this case, but it's possible to calculate the intersections between the D-curve and the straight line with the intersections library. It takes a couple of seconds though.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{patterns, intersections}

\tikzset{
    dots size/.store in=\dotssize,
    dots size=1pt,
    dots spread/.store in=\dotsspread,
    dots spread=10pt
    }

\makeatletter
\pgfdeclarepatternformonly[\dotssize,\dotsspread]{mydots}
{\pgfpointorigin}
{\pgfpoint{\dotsspread}{\dotsspread}}
{\pgfpoint{\dotsspread}{\dotsspread}}
{
    \pgfsetcolor{\tikz@pattern@color}
    \pgfpathcircle{\pgfpoint{\dotsspread/2}{\dotsspread/2}}{\dotssize}
    \pgfusepath{fill}
}
\makeatother

\begin{document}
\begin{tikzpicture}

% Axis
\draw (7,7) -| (0,0) node[below]{0} -| node[below] {1} cycle;

% curve
% add name path=x
\draw [name path=x] (0,0) to (7,7);

\fill [pattern=mydots, opacity=0.7] (0,0) to [out=80,in=200] (2,2)
                           to [out=20, in=250] (4,4) 
                           to [out=70, in=200] (7,6.4)
                           |- cycle;

% add name path=D
\draw [very thick, name path=D] (0,0) to [out=80,in=200] (2,2)
                         to [out=20, in=250] (4,4) 
                         to [out=70, in=200] coordinate[pos=0.2] (m) (7,6.4);


\draw (0.5,0) arc[start angle=0,end angle=45,radius=0.5] node[midway,right] {$45^{\circ}$} ;

% find intersections
\path [name intersections={of=x and D, name=lambda}];
%first intersection is at x=0, so use intersections 2-4
\foreach [count=\i] \j in {2,3,4}
   \draw [dashed,thick] (lambda-\j) -- (lambda-\j |- 0,0) node[below,font=\large] {$\lambda_{\i}$};

\draw[<-,>=latex] (2,6) node[above] {\LARGE $D(\bar{e}(\lambda)$)} to[out=270,in=200] (m);

\end{tikzpicture}
\end{document}
0
15

Very similar to Henri’s answer but with several minor improvements:

  • lighter shading of the dot pattern
  • 45° mark using arc
  • labelling using \bar{}
  • curve and pattern behind axis and labels

-

\documentclass[tikz]{standalone}
\usetikzlibrary{patterns}

\begin{document}
\begin{tikzpicture}

% curve

\fill[pattern=dots,pattern color=black!25] (0,0) to[out=80, in=200] (2,2) 
       to[out=20, in=260] (4,4)
       to[out=80, in=180, distance=45] (7,6)
       -- (7,0) -- cycle;

\draw[very thick] (0,0) to[out=80, in=200] (2,2) 
       to[out=20, in=260] (4,4)
       to[out=80, in=180, distance=45] (7,6);

\draw[<-,>=latex] (2,6) node[above] {\LARGE $D(\bar{e}(\lambda))$} to[out=-90,in=180] (4.05,4.4);

% axis

\draw (0,7) -- (0,0) node[below]{0} -- (7,0) node[below] {1} 
            -- (7,7)  -- cycle;

\draw (0,0) to (7,7);
\draw (0:.5) arc (0:45:.5) node[above right,midway] {$45^{\circ}$};

\draw[dashed] (2,0) node[below] {\large $\lambda_1$} -- (2,2);
\draw[dashed] (4,0) node[below] {\large $\lambda_2$} -- (4,4);
\draw[dashed] (5.9,0) node[below] {\large $\lambda_3$} -- (5.9,5.9);

\end{tikzpicture}
\end{document}

enter image description here

12

You're almost there. I would have used a little different angles. To get the stippling I use the dots pattern from the patterns libary.

Unforunately patterns in TikZ do not have any parameters, so you have to live with the very dense filling. Otherwise you'd have to define you own dot pattern.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{patterns}
\begin{document}
\begin{figure}
\begin{tikzpicture}[scale=1]

% Axis

\draw (0,7) -- (0,0) node[below]{0} -- (7,0) node[below] {1};
\draw (0,7) -- (7,7)  -- (7,0);


% curve

\draw (0,0) to (7,7);
\draw [very thick] (0,0) to [out=90, in=200] (2,2)
to [out=20, in=250] (4,4)
to [out=70, in=180] (7,6);

\fill [pattern=dots] (0,0) to [out=90, in=200] (2,2)
to [out=20, in=250] (4,4)
to [out=70, in=180] (7,6)
-- (7,0) -- cycle;


\draw (0.3,0.3) to [out=0, in=-270] (0.5,0) ;
\node[right] at (0.6,0.25) {$45^{o}$};

\draw [dashed] (2,0) -- (2,2);
\draw [dashed] (4,0) -- (4,4);
\draw [dashed] (5.9,0) -- (5.9,5.9);


\node[below] at (2,0) {\large $\lambda_1$};
\node[below] at (4,0) {\large $\lambda_2$};
\node[below] at (5.9,0) {\large $\lambda_3$};


\draw[<-,>=latex] (2,6) node[above] {\LARGE \begin{tabular}{c}$D(e(\lambda)$)\end{tabular}} to[out=-90,in=-100] (4.05,4.4);
\node[above] at (1.9,6.5) {-};

\end{tikzpicture}

\end{figure}

\end{document}

enter image description here

3
  • 2
    Please explain your downvote. May 5, 2018 at 10:03
  • I didn't downvote, but your dotted line above $\lambda_3$ seems a little off, it doesn't quite hit the intersection of the 45 degree line and the curve.
    – corporal
    May 6, 2018 at 4:46
  • @corporal That is copied from the original post. I only provided the adjusted angles and the dot filling. Later I wondered whether I should improve some of the copied shortcomings but then it would be too similar to the other answers. May 6, 2018 at 5:35
12

A slight variant of the other answers. This one changes the font to XITS and the fill pattern to crosshatch dots gray. (There are some artifacts created by the rasterizer I used; sorry.)

\documentclass{article}
\usepackage{unicode-math}
\usepackage{tikz}
\usetikzlibrary{patterns}

\setmainfont{XITS}
\setmathfont{XITS Math}

\begin{document}
\begin{tikzpicture}

% Axis
\draw (7,7) -| (0,0) node[below]{0} -| node[below] {1} cycle;

% curve    

\fill [pattern=crosshatch dots gray, opacity=0.7] (0,0) to [out=80,in=200] (2,2)
                           to [out=20, in=250] (4,4) 
                           to [out=70, in=200] (7,6.4)
                           |- cycle;
\draw [very thick] (0,0) to [out=80,in=200] (2,2)
                         to [out=20, in=250] (4,4) 
                         to [out=70, in=200] coordinate[pos=0.2] (m) (7,6.4);

\draw (0,0) to (7,7);

\draw (0.5,0) arc[start angle=0,end angle=45,radius=0.5] node[midway,right] {$45^{\circ}$} ;

\foreach \i in {1,2,3}
   \draw [dashed,thick] (2*\i,2*\i) -- (2*\i, 0) node[below,font=\large] {$\lambda_{\i}$};

\draw[<-,>=latex] (2,6) node[above] {\LARGE $D(\bar{e}(\lambda)$)} to[out=270,in=200] (m);

\end{tikzpicture}
\end{document}

Graph

Some variants you might try: crosshatch dots is another built-in fill pattern that’s pretty close, and you could \usepackage{xits} instead of unicode-math to use legacy fonts rather than OpenType. See Torbjørn T.’s answer if you want to customize the dot pattern.

2
  • This is good, but the line at 45 degrees is a bit "grayed out" from the fill. Better to have \draw (0,0) -- (7,7); just before \end{tikzpicture}.
    – corporal
    May 6, 2018 at 4:41
  • @corporal Great idea! Implemented.
    – Davislor
    May 6, 2018 at 5:27
6

Just for fun with MetaFun (pun intended).

\startMPpage
  input hatching ;

  numeric u, t, l[] ;
  u := 6cm ;
  l1 := .22u ;
  l2 := .55u ;
  l3 := .9u ;
  t := .8 ;

  draw unitsquare scaled (1u) ;
  for i = 0, 1:
      label.bot("$" & decimal i & "$", (i*u,0)) ;
  endfor ;
  draw (0,0) -- (1u,1u) ;

  path p ; p := (0,0){up}
      .. tension t .. (l1,l1)
      .. tension t .. (l2,l2)
      .. tension t .. (l3,l3)
      .. tension t .. (1u,.92u);
  draw image (
      interim linecap := butt ;
      draw p withpen pencircle scaled (2pt) ;
  ) ;

  hatchoptions (dashed withdots);
  hatchfill (p -- (1u,0) -- cycle) withcolor (
      -45, % hatching angle
      5pt, % distance between lines
      -1pt % thickness of lines
  );

  for i = 1 upto 3:
      draw (l[i],0) -- (l[i],l[i]) dashed evenly ;
      label.bot("$\lambda_{" & decimal i & "}$", (l[i],0)) ;
  endfor;

  draw anglebetween(origin -- (1u,0),origin -- (l1,l1),"$45^\circ$") ;

  label.top("$D(\bar{e}(\lambda))$", (.3u,.85u));
  drawarrow (point 2.1 of p){left} .. {up}(.3u,.85u) ;
\stopMPpage

enter image description here

5

Personally I do not understand how a question like this can bring so mush interest. How a question titled "How do I draw this graph using TikZ?" could be useful for future googling?

Any way, here are 17 lines of code with 5 drawing commands that draw (approximately) this image.

\documentclass[tikz,border=7pt]{standalone}
\usetikzlibrary{patterns,svg.path}
\begin{document}
  \begin{tikzpicture}
    \begin{scope}
      \clip[preaction=draw]rectangle(7,7)--(0,0);
      \begin{scope}[scale=1cm/1pt]
        \draw[thick,pattern=dots,fill opacity=.21]svg{M0 0Q 0 2 2 2T4 4 7.1 6V0};
        \draw[-latex]svg{M4 4.5Q2 4.5 2 5.9}
          node[above,scale=1.4]{$D\big(\bar e(\lambda)\big)$};
      \end{scope}
      \draw(0:.5)arc(0:45:.5)(22.5:1)node{$45^{\circ}$};
    \end{scope}
    \draw[dashed,below]node{$0$}(7,0)node{$1$}
      foreach[count=\i]~in{2,4,5.9}{(~,~)--(~,0)node{$\lambda_\i$}};
  \end{tikzpicture}
\end{document}

enter image description here

2

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.