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'd like to shade the regions labeled A, B, C, D in this graph.

\begin{tikzpicture}
 \begin{axis}[
   xmin = 0, xmax = 1000,
   ymin = 12, ymax = 16,
   xlabel=$Q$,
   ylabel=$P$,
  ]
  \addplot[name path global=supply, domain=0:1000, red] { 0.00325*x + 12.5125 };
  \addplot[name path global=demand, domain=0:1000, blue] { -0.013*x + 22.75 };
  \addplot[name path global=world, domain=0:1000, black] { 13 };
  \addplot[name path global=tariff, domain=0:1000, green] { 13.65 };


  \node at (axis cs:100, 13.3) {$A$};
  \node at (axis cs:300, 13.3) {$B$};
  \node at (axis cs:550, 13.3) {$C$};
  \node at (axis cs:720, 13.1) {$D$};

  \vasymptote[dashed]{350}
  \vasymptote[dashed]{700}

  \legend{$Supply$, $Demand$, $P_{world}$, $P_{tariff}$}

 \end{axis}
\end{tikzpicture}

Here's the output: enter image description here

What I'd like is to shade the regions A, B, C, D -- for example, fill the trapezoid A with green, the triangle B with blue, the rectangle C with orange, and the triangle D with purple.

\vasymptote is defined in my preamble:

\usepackage{pgfplots}
\usepackage{tkz-fct}
\usetikzlibrary{intersections}


\newcommand{\vasymptote}[2][]{
    \draw [densely dashed,#1] ({rel axis cs:0,0} -| {axis cs:#2,0}) -- ({rel axis cs:0,1} -| {axis cs:#2,0});
}

\newcommand{\gettikzxy}[3]{%
  \tikz@scan@one@point\pgfutil@firstofone#1\relax
  \edef#2{\the\pgf@x}%
  \edef#3{\the\pgf@y}%
}

\pgfplotsset{
    every axis/.append style={
        scale only axis,
        width=1.0\columnwidth,
    },
    /tikz/every picture/.append style={
        trim axis left,
        trim axis right,
    }
}

\def\Dimline[#1][#2][#3][#4]{
\begin{scope}[thin, >=stealth'] % redefine as flechas
\draw let \p1=#1, \p2=#2, \n0={veclen(\x2-\x1,\y2-\y1)} in [|<->|,
decoration={markings,mark=at position .5 with {\node[#3] at (0,0)
{#4};},
},
postaction=decorate] #1 -- #2 ;
\end{scope}
}
share|improve this question
1  
Please include your preamble and be more clear with your question. Do you want to shade the region defined by the points A,B,C,D? Do you want to shade the regions indicated by the label? I don't understand.. –  Red Oct 7 '13 at 15:08
    
I've added my preamble and attached an image to clarify what I'm trying to accomplish -- basically shade the entire shapes where the labels reside. –  alcaras Oct 7 '13 at 22:46

1 Answer 1

up vote 5 down vote accepted

Here is a suggestion. I modified \vasymptote to have a second mandatory argument that is a name for the path. I then used name intersections to give node names to the various intersections between the lines, and finally used \fill to add the colour. I used the backgrounds library to add this fill on the background layer, avoiding partial covering of the plot lines.

In addition I suggest you not write the first two legend entries in math mode, and the subscripts in the two last legend entries in an upright font.

\documentclass[border=10mm]{standalone}
\usepackage{pgfplots}
\usepackage{tkz-fct}
\usetikzlibrary{intersections,backgrounds}


\newcommand{\vasymptote}[3][]{
    \draw [densely dashed,name path=#3,#1] ({rel axis cs:0,0} -| {axis cs:#2,0}) -- ({rel axis cs:0,1} -| {axis cs:#2,0});
}

\newcommand{\gettikzxy}[3]{%
  \tikz@scan@one@point\pgfutil@firstofone#1\relax
  \edef#2{\the\pgf@x}%
  \edef#3{\the\pgf@y}%
}

\pgfplotsset{
    every axis/.append style={
        scale only axis,
        width=1.0\columnwidth,
    },
    /tikz/every picture/.append style={
        trim axis left,
        trim axis right,
    }
}

\def\Dimline[#1][#2][#3][#4]{
\begin{scope}[thin, >=stealth'] % redefine as flechas
\draw let \p1=#1, \p2=#2, \n0={veclen(\x2-\x1,\y2-\y1)} in [|<->|,
decoration={markings,mark=at position .5 with {\node[#3] at (0,0)
{#4};},
},
postaction=decorate] #1 -- #2 ;
\end{scope}
}
\begin{document}

\begin{tikzpicture}
 \begin{axis}[
   xmin = 0, xmax = 1000,
   ymin = 12, ymax = 16,
   xlabel=$Q$,
   ylabel=$P$,
  ]
  \addplot[name path global=supply, domain=0:1000, red] { 0.00325*x + 12.5125 };
  \addplot[name path global=demand, domain=0:1000, blue] { -0.013*x + 22.75 };
  \addplot[name path global=world, domain=0:1000, black] { 13 };
  \addplot[name path global=tariff, domain=0:1000, green] { 13.65 };


  \node at (axis cs:100, 13.3) {$A$};
  \node at (axis cs:300, 13.3) {$B$};
  \node at (axis cs:550, 13.3) {$C$};
  \node at (axis cs:720, 13.1) {$D$};

  \vasymptote[dashed]{350}{first}
  \vasymptote[dashed]{700}{second}

  \legend{Supply, Demand, $P_\mathrm{world}$, $P_\mathrm{tariff}$}

  \path [name intersections={of=supply and world, by={a}},
         name intersections={of=supply and tariff, by={b}},
         name intersections={of=first and world, by={c}},
         name intersections={of=second and world, by={d}},
         name intersections={of=demand and tariff, by={e}},
         name intersections={of=demand and world, by={f}}];

 \begin{scope}[on background layer]
  \fill [blue,opacity=.3] (axis cs:0,13) -- (axis cs:0,13.65) -- (b) -- (a) -- cycle;
  \fill [red,opacity=.3] (a) -- (b) -- (c) -- cycle;
  \fill [green,opacity=.3] (b) -- (c) -- (d) -- (e) -- cycle;
  \fill [purple,opacity=.3] (e) -- (d) -- (f) -- cycle;
 \end{scope}
 \end{axis}

\end{tikzpicture}

\end{document}

enter image description here

share|improve this answer

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.