# How to fill an area bounded by grid lines and axes?

With pgfplots one can for axis use the options minor xtick and minor ytick together with grid=minor to manually produce grid lines. If one use such grid lines one may want to emphasize a certain region bounded by the grid lines and the axes by filling it with some color. Is this possible?

Here is an example to illustrate the problem. Say that I have the following

\documentclass{article}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
minor ytick={2},
minor xtick={2},
grid=minor
]
\end{axis}
\end{tikzpicture}

\end{document}


which produces

and say that I want to emphasize upper right region that is bounded by the grid lines and the axes. How can I do this? That is, to produce something like the following:

-

You can use a normal TikZ \fill command in combination with the axis cs: and rel axis cs: coordinate systems for this. Coordinates given in the form (axis cs:2,3) refer to the coordinate 2,3 in the data coordinate system, while rel axis cs:1,1 refers to the upper right-hand corner of the plot canvas. In your case, you could use

\fill [orange!25] (axis cs:2,2) rectangle (rel axis cs:1,1);


before your \addplot command to shade the rectangular area. Note that you will want to set axis on top, as the coloured area would otherwise overlay the tick marks and axis lines.

\documentclass{article}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
minor ytick={2},
minor xtick={2},
grid=minor,
axis on top
]
\fill [orange!25] (axis cs:2,2) rectangle (rel axis cs:1,1);