9

How can I set two different backgrounds in a graph using pgfplots? In my case, I need a colored background for x<5 and a different background for x>5.

Urko
  • 969

3 Answers3

10

You can simply draw a rectangle using relative axis cs. Change the coordinates as you wish. Note that lower (south west) end is {rel axis cs:0,0)} and the upper (north east) end is {rel axis cs:1,1}.

\documentclass{article}
\usepackage{pgfplots}
\usepackage[graphics,tightpage,active]{preview}

\PreviewEnvironment{tikzpicture}
\pgfplotsset{compat=1.9}
\usetikzlibrary{backgrounds}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[%
        ymin=-1,%
        ymax=1,%
        grid=both,%
        %axis on top
        ]
    \begin{scope}[on background layer]
    \fill[green,opacity=1] ({rel axis cs:0,0}) rectangle ({rel axis cs:0.5,1});
    \fill[red,opacity=1] ({rel axis cs:0.5,0}) rectangle ({rel axis cs:1,1});
    \end{scope}
    \addplot[domain=-360:360, blue , very thick, smooth]{sin(x)};
    \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

With background layers, opacity need not be used.

  • When I tried to fill the area in my own I did not use the opacity option and the result was quite bad. Now my problem is solved. Thank you Harish! – Urko Jan 29 '14 at 07:27
  • 1
    For people who might wonder: place the \addplot after the \fill's if you only want the background, and not the blue line, to change color. – pkofod Jan 29 '14 at 08:21
  • @pkofod Nice suggestion. I have edited accordingly. Thanks :) –  Jan 29 '14 at 09:29
  • You might also add axis on top then the axis will not be overlapped by the highlighting rectangle. – quinmars Feb 27 '14 at 18:19
  • @quinmars That is a good suggestion. But it will bring minor grid on top of the curve. I used background layer instead. thanks :) –  Feb 27 '14 at 22:19
  • Oh, that solution is even better :) – quinmars Feb 27 '14 at 22:29
6

In my PhD-Thesis I simply drew a slab behind the plot for the region I wanted to highlight. There might be an easier way to do it, but I found this quite simple in the end. In essence it boils down to drawing a rectangle a bit bigger than the shown axes, as seen in the minimal example below:

\documentclass{article}
\usepackage{pgfplots}
\usepackage[graphics,tightpage,active]{preview}

\PreviewEnvironment{tikzpicture}

\begin{document}

\begin{tikzpicture}
    \begin{axis}[%
        ymin=-1,%
        ymax=1,%
        grid=both,%
        ]
    \addplot [draw=red,fill=red, semitransparent]
        coordinates {(55,-1.1) (55,1.1) (333,1.1) (333,-1.1)};
    \addplot [draw=green,fill=green, semitransparent]
        coordinates {(-360,-1.1) (-360,1.1) (55,1.1) (55,-1.1)};
    \addplot[domain=-360:360, blue , very thick, smooth]{sin(x)};
    \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

I'm sure you can figure out what to change to have the colored region to the point you want.

Habi
  • 7,694
3

With PSTrick just for fun!

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-plot}
\psset{algebraic,plotpoints=200}
\def\f{x*sin(3*x)}
\begin{document}
\begin{pspicture}(-4,-4)(4.5,4.5)
    \psframe*[linecolor=red](-4,-4)(-2,4)%
    \psframe*[linecolor=green](-2,-4)(4,4)%
    \psaxes{->}(0,0)(-4,-4)(4.25,4.25)[$x$,0][$y$,90]%
    \psplot[linecolor=blue,linewidth=2pt]{-4}{4}{\f}%
\end{pspicture}
\end{document}

enter image description here