Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I need to reproduce the following plot: CAP Curve

I tried the following code using pgfplots:

\documentclass[8pt]{beamer}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usepackage{pgfplots}
\begin{document}
  \begin{frame}
  \begin{tikzpicture}
  \begin{axis}[domain=0:1,
   width=10cm,
  minor tick num=3,
   height=8cm,
  ymin=0,
  ymax=1,
  xmin=0,
  xmax=1,
  xticklabel=\empty,
  yticklabel=\empty,
  samples=50,
  smooth,
  xlabel=$r_{k}$,
  ylabel=$s_{k}$,
  extra x ticks={0,1},
  extra y ticks={1},
  extra x tick style={grid=major,thick},
  extra y tick style={grid=major,thick},
  extra x tick labels={$0$,$1$},
  extra y tick labels={$1$},
  legend pos=north east]


\addplot[fill=green,draw=none,domain=0:1,pattern=north east lines,pattern      color=yellow,stack plots=y]{x^0.25-x} \closedcycle;
\addplot[fill=green,draw=none,domain=0:1,stack plots=y]{1-x}\closedcycle;

\addplot[id=naive,color=black,domain=0:1,samples=50] {x};
\addplot[id=mod,color=black,domain=0:1,samples=50]{x^0.25};
\addplot[id=perf,color=black,domain=0:1,samples=50]{1};


\node (a) at (axis cs:0.4,0.6)  {$a_{R}$};
\node (b) at (axis cs:0.1,0.8)  {$a_{P}$};
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}

I don't get the shapes right. The filled areas are inverted and do not apply on the designated region. Followed many similar questions and tried different things and none worked for me so far.

share|improve this question
    
And which data are you using to draw the paths enclosing the regions? –  Gonzalo Medina Feb 22 at 23:28
    
As I said in the comment below, I used the curve x^0.25 to draw the arc inside the triangle. –  Chedly Feb 23 at 12:53

3 Answers 3

up vote 19 down vote accepted

The question didn't contain the functions or coordinates needed to produce the graphs showed in the image, so I used some dummy coordinates, but the idea is the same. Two customizable filling patterns were also defined:

\documentclass[8pt]{beamer}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usepackage{pgfplots}

% New customizable pattern
\tikzset{
  hatch distance/.store in=\hatchdistance,
  hatch distance=8pt,
  hatch thickness/.store in=\hatchthickness,
  hatch thickness=5pt,
  hatch color/.store in=\hatchcolor,
  hatch color=gray!20
}

\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{thick vlines}
  {\pgfpointorigin}{\pgfqpoint{\hatchthickness}{100pt}}{\pgfqpoint{\hatchdistance}{100pt}}%
  {
  \pgfsetlinewidth{\hatchthickness}
  \pgfpathmoveto{\pgfqpoint{0.5pt}{0pt}}
  \pgfpathlineto{\pgfqpoint{0.5pt}{100pt}}
  \pgfusepath{stroke}
  }
\pgfdeclarepatterninherentlycolored[\hatchcolor]{crosshatch dots color}
{\pgfpointorigin}{\pgfpoint{8pt}{8pt}}
{\pgfpoint{8pt}{8pt}}
{
  \pgfsetfillcolor{\hatchcolor}
  \pgfpathrectangle{\pgfpointorigin}{\pgfpoint{8pt}{8pt}}
  \pgfusepath{fill}
  \pgfsetfillcolor{\hatchcolor}
  \pgfpathcircle{\pgfpoint{2pt}{1.75pt}}{20pt}
  \pgfpathcircle{\pgfpoint{6pt}{5.75pt}}{20pt}
  \pgfusepath{fill}
  \pgfsetfillcolor{pgf@darklightsteelblue!70}
  \pgfpathcircle{\pgfpoint{2pt}{2.25pt}}{0.4pt}
  \pgfpathcircle{\pgfpoint{6pt}{6.25pt}}{0.4pt}
  \pgfusepath{fill}
}

\begin{document}

\begin{frame}
  \begin{tikzpicture}
  \begin{axis}[domain=0:1,
   width=10cm,
  minor tick num=3,
   height=8cm,
  ymin=0,
  ymax=1.2,
  xmin=0,
  xmax=1,
  xticklabel=\empty,
  yticklabel=\empty,
  samples=50,
  smooth,
  xlabel=$r_{k}$,
  ylabel=$s_{k}$,
  extra x ticks={0,1},
  extra y ticks={1},
  extra x tick style={grid=major,thick},
  extra y tick style={grid=major,thick},
  extra x tick labels={$0$,$1$},
  extra y tick labels={$1$},
  legend pos=north east]

\draw[pattern=crosshatch dots color]
  (axis cs:0,0) -- (axis cs:0.12,1) -- (axis cs:1,1) -- (axis cs:0,0);
\addplot+[
  id=mod,
  color=black,
  domain=0:1,
  samples=50,
  pattern=thick vlines,
  hatch thickness=3pt
] coordinates {(0,0) (0.2,0.45) (0.5,0.8) (0.75,0.92) (1,1)};
\addplot[id=naive,color=black,domain=0:1,samples=50] {x} ;

\node (a) at (axis cs:0.2,0.8)  {$a_{P}$};
\node[draw,fill=gray!30] (b) at (axis cs:0.45,0.6)  {$a_{R}$};

\begin{scope}[
  every pin edge/.style={<-,>=latex},
  pin distance=1.5cm
]
\node[pin=-30:{Rating model},] at (axis cs:0.5,0.8) {};
\node[pin=-30:{Random model},] at (axis cs:0.3,0.3) {};
\node[pin=15:{Perfect model},] at (axis cs:0.4,1.01) {};
\end{scope}
\end{axis}
\end{tikzpicture}
\end{frame}

\end{document}

enter image description here

And using x^0.45 to produce the curved path:

\documentclass[8pt]{beamer}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usepackage{pgfplots}

\pgfplotsset{compat=newest}

% New customizable pattern
\tikzset{
  hatch distance/.store in=\hatchdistance,
  hatch distance=8pt,
  hatch thickness/.store in=\hatchthickness,
  hatch thickness=5pt,
  hatch color/.store in=\hatchcolor,
  hatch color=gray!20
}

\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{thick vlines}
  {\pgfpointorigin}{\pgfqpoint{\hatchthickness}{100pt}}{\pgfqpoint{\hatchdistance}{100pt}}%
  {
  \pgfsetlinewidth{\hatchthickness}
  \pgfpathmoveto{\pgfqpoint{0.5pt}{0pt}}
  \pgfpathlineto{\pgfqpoint{0.5pt}{100pt}}
  \pgfusepath{stroke}
  }
\pgfdeclarepatterninherentlycolored[\hatchcolor]{crosshatch dots color}
{\pgfpointorigin}{\pgfpoint{8pt}{8pt}}
{\pgfpoint{8pt}{8pt}}
{
  \pgfsetfillcolor{\hatchcolor}
  \pgfpathrectangle{\pgfpointorigin}{\pgfpoint{8pt}{8pt}}
  \pgfusepath{fill}
  \pgfsetfillcolor{\hatchcolor}
  \pgfpathcircle{\pgfpoint{2pt}{1.75pt}}{20pt}
  \pgfpathcircle{\pgfpoint{6pt}{5.75pt}}{20pt}
  \pgfusepath{fill}
  \pgfsetfillcolor{pgf@darklightsteelblue!70}
  \pgfpathcircle{\pgfpoint{2pt}{2.25pt}}{0.4pt}
  \pgfpathcircle{\pgfpoint{6pt}{6.25pt}}{0.4pt}
  \pgfusepath{fill}
}

\begin{document}

\begin{frame}
  \begin{tikzpicture}
  \begin{axis}[domain=0:1,
   width=10cm,
  minor tick num=3,
   height=8cm,
  ymin=0,
  ymax=1.2,
  xmin=0,
  xmax=1,
  xticklabel=\empty,
  yticklabel=\empty,
  samples=50,
  smooth,
  xlabel=$r_{k}$,
  ylabel=$s_{k}$,
  extra x ticks={0,1},
  extra y ticks={1},
  extra x tick style={grid=major,thick},
  extra y tick style={grid=major,thick},
  extra x tick labels={$0$,$1$},
  extra y tick labels={$1$},
  legend pos=north east]

\draw[pattern=crosshatch dots color]
  (axis cs:0,0) -- (axis cs:0.05,1) -- (axis cs:1,1) -- (axis cs:0,0);
\addplot[
  id=mod,
  color=black,
  domain=0:1,
  samples=100,
  pattern=thick vlines,
  hatch thickness=3pt
] {(x^(0.45))};
\addplot[id=naive,color=black,domain=0:1,samples=50] {x} ;

\node (a) at (axis cs:0.2,0.8)  {$a_{P}$};
\node[draw,fill=gray!30] (b) at (axis cs:0.45,0.6)  {$a_{R}$};

\begin{scope}[
  every pin edge/.style={<-,>=latex},
  pin distance=1.5cm
]
\node[pin=-30:{Rating model},] at (axis cs:0.5,0.8) {};
\node[pin=-30:{Random model},] at (axis cs:0.3,0.3) {};
\node[pin=15:{Perfect model},] at (axis cs:0.4,1.01) {};
\end{scope}
\end{axis}
\end{tikzpicture}
\end{frame}

\end{document}

enter image description here

In general, the following code can be used to fill the area between the graph of two functions f and g:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}

\pgfplotsset{compat=newest}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
  domain=-2:9,
  samples=60,
  stack plots=y
]
% draw graph for the first function f
\addplot+[mark=none] {(x-3)^2};
% draw graph of max(g-f, 0) and stack
\addplot+[mark=none,fill=green!40,draw=red] {max(x+2-((x-3)^2),0)} \closedcycle;
% draw graph of min(g-f, 0) and stack
\addplot+[mark=none,fill=none,draw=red] {min(x+2-((x-3)^2),0)};
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

share|improve this answer
    
My concern wasn't the coordinates. I intended to reproduce the curve using x^0.25. What I am still missing is why my code fails when it succeeded with other examples? It's puzzling –  Chedly Feb 23 at 12:51
1  
@Chedly please see my updated answer. I used x^0.45 instead of x^0.25, since near zero the behaviour of the later was not satisfying. –  Gonzalo Medina Feb 23 at 15:55
    
Thank you Gonzalo. Your contribution was very valuable to me. Just we can't stop learning Latex even after many years of experience :) –  Chedly Feb 23 at 16:39
1  
@Chedly You're welcome :) Don't forget (now that your question has several answers) that you can accept the one you consider helped you the most by clicking the checkmark to the left of the answer (you've up-voted answers, but accepting and up-voting are two different actions). In case of doubt, please see How do you accept an answer? –  Gonzalo Medina Feb 23 at 16:53
    
Done Gonzalo ;) –  Chedly Feb 23 at 18:57

Just for completion. Next code (borrowed from Gonzalo's answer) shows how to use fillbetween pgfplots' library (needs v1.10) to fill areas between curves. It's not necessary to draw closed paths, just name them and pgfplots will compute intersections and fill the areas in between.

\documentclass[8pt]{beamer}
\usepackage{tikz}
\usetikzlibrary{patterns}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=newest}

% New customizable pattern
\tikzset{
  hatch distance/.store in=\hatchdistance,
  hatch distance=8pt,
  hatch thickness/.store in=\hatchthickness,
  hatch thickness=5pt,
  hatch color/.store in=\hatchcolor,
  hatch color=gray!20
}

\pgfdeclarepatternformonly[\hatchdistance,\hatchthickness]{thick vlines}
  {\pgfpointorigin}{\pgfqpoint{\hatchthickness}{100pt}}{\pgfqpoint{\hatchdistance}{100pt}}%
  {
  \pgfsetlinewidth{\hatchthickness}
  \pgfpathmoveto{\pgfqpoint{0.5pt}{0pt}}
  \pgfpathlineto{\pgfqpoint{0.5pt}{100pt}}
  \pgfusepath{stroke}
  }
\pgfdeclarepatterninherentlycolored[\hatchcolor]{crosshatch dots color}
{\pgfpointorigin}{\pgfpoint{8pt}{8pt}}
{\pgfpoint{8pt}{8pt}}
{
  \pgfsetfillcolor{\hatchcolor}
  \pgfpathrectangle{\pgfpointorigin}{\pgfpoint{8pt}{8pt}}
  \pgfusepath{fill}
  \pgfsetfillcolor{\hatchcolor}
  \pgfpathcircle{\pgfpoint{2pt}{1.75pt}}{20pt}
  \pgfpathcircle{\pgfpoint{6pt}{5.75pt}}{20pt}
  \pgfusepath{fill}
  \pgfsetfillcolor{pgf@darklightsteelblue!70}
  \pgfpathcircle{\pgfpoint{2pt}{2.25pt}}{0.4pt}
  \pgfpathcircle{\pgfpoint{6pt}{6.25pt}}{0.4pt}
  \pgfusepath{fill}
}

\begin{document}

\begin{frame}
  \begin{tikzpicture}
  \begin{axis}[domain=0:1,
   width=10cm,
   minor tick num=3,
   height=8cm,
   ymin=0,
   ymax=1.2,
   xmin=0,
   xmax=1,
   xticklabel=\empty,
   yticklabel=\empty,
   samples=50,
%  smooth,
   xlabel=$r_{k}$,
   ylabel=$s_{k}$,
   extra x ticks={0,1},
   extra y ticks={1},
   extra x tick style={grid=major,thick},
   extra y tick style={grid=major,thick},
   extra x tick labels={$0$,$1$},
   extra y tick labels={$1$},
   legend pos=north east]

% Add a name to any path, even not closed
\addplot [name path=perfect]  coordinates {(0,0) (0.05,1) (1,1)};
\addplot [name path=naive] {x};

% and use both names to set filled area
% pattern option is added to `addplot` options not to `fill between`.
\addplot [pattern=crosshatch dots color] fill between[of=perfect and naive];

\addplot [name path=random,smooth] {(x^(0.45))};
\addplot [pattern=thick vlines, hatch thickness=3pt] fill between[of=random and naive];

\node (a) at (axis cs:0.2,0.8)  {$a_{P}$};
\node[draw,fill=gray!30] (b) at (axis cs:0.45,0.6)  {$a_{R}$};

\begin{scope}[
  every pin edge/.style={<-,>=latex},
  pin distance=1.5cm
]
\node[pin=-30:{Rating model},] at (axis cs:0.5,0.75) {};
\node[pin=-30:{Random model},] at (axis cs:0.3,0.3) {};
\node[pin=15:{Perfect model},] at (axis cs:0.4,1.01) {};
\end{scope}
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}

enter image description here

share|improve this answer

Just for fun with PSTricks.

\documentclass[pstricks]{standalone}
\usepackage{pst-plot,pst-eucl}
\psset{xunit=7,yunit=5,runit=\psxunit,linejoin=1,PointName=none,fillstyle=solid,fillcolor=lightgray}
\begin{document}
\begin{pspicture}(-.3,-.3)(1.3,1.3)
    \psline[fillstyle=none]{<->}(0,1.2)(0,0)(1.2,0)
    \psxTick(1){1}\psyTick(1){1}
    \rput[tr](1.2,-20pt){\scriptsize proportion of all debtors}
    \rput[br]{90}(-20pt,1.2){\scriptsize proportion of defaulted debtors}
    \pstGeonode[PointSymbol={none,default}](.1,1){C}(0,0){A}(1,1){B}
    \pspolygon(A)(B)(C) 
    \pstGeonode(.1,.4){D}(.3,.7){E}(.5,.8){F}(.7,.9){G}
    \pspolygon[fillstyle=vlines,hatchangle=0,hatchwidth=2pt](A)(D)(E)(F)(G)(B)
    \rput(.2,.8){$a_P$}
    \psTextFrame(.375,.55)(.475,.65){$a_R$}
    \rput[bl](.5,1.1){\rnode[l]{permodelA}{perfect model}}
    \rput[tl](.4,.2){\rnode[l]{ranmodelA}{random model}}
    \rput[tl](.8,.6){\rnode[l]{ratmodelA}{rating model}}
    \pnodes(.3,1){permodelB}([nodesep=.3]{B}A){ranmodelB}
    \psset{arrows=->,nodesep=3pt}
    \ncline{permodelA}{permodelB}
    \ncline{ranmodelA}{ranmodelB}
    \ncline{ratmodelA}{F}
\end{pspicture}
\end{document}

enter image description here

enter image description here

share|improve this answer
1  
Thank you The Last Error. Although never touched pstricks but I'll give a try to see if it fits into my slides ;) –  Chedly Feb 23 at 16:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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