7

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
    ]
    \addplot{x};
  \end{axis}
\end{tikzpicture}

\end{document}

which produces

Plot produced by example code

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:

Plot with filling in region bounded by the grid lines and the axes

1 Answer 1

13

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);
    \addplot{x};
  \end{axis}
\end{tikzpicture}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .