# Axis background shading overriding legend

This code in MATLAB:

hold on
h(1) = area([0 2.5], [125 125]);
h(2) = area([2.5 5], [125 125]);
set(h(1),'FaceColor',[1.0 0.8 0.6],'EdgeColor',[1.0 0.8 0.6]);
set(h(2),'FaceColor',[1.0 0.8 0.4],'EdgeColor',[1.0 0.8 0.4]);
x = [0:1:5];
y1 = x.^2;
y2 = x.^3;
hLine1 = plot(x,y1,'r-o');
hLine2 = plot(x,y2,'g-s');
set(gca,'ylim',[0 125]); %left yaxis range
hleg1 = legend([hLine1 hLine2],{'line1','line2'});


Creates this plot:

And I export it to tikz using matlab2tikz and compile it in this .tex file (Note: You need to comment out stack plots=y, or pgfplots gives an error, not sure why):

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{plotmarks}
\usepackage{tikz}
\begin{document}

\definecolor{mycolor1}{rgb}{1.00000,0.80000,0.40000}%

\begin{figure}
\begin{tikzpicture}

\begin{axis}[%
width=4.52083333333333in,
height=3.565625in,
area style,
%stack plots=y,
scale only axis,
xmin=0,
xmax=5,
ymin=0,
ymax=125,
axis x line*=bottom,
axis y line*=left,
legend style={draw=black,fill=white,legend cell align=left}
]
2.5 125\\
}
\closedcycle;
5   125\\
}
\closedcycle;
table[row sep=crcr]{0 0\\
1   1\\
2   4\\
3   9\\
4   16\\
5   25\\
};

table[row sep=crcr]{0 0\\
1   1\\
2   8\\
3   27\\
4   64\\
5   125\\
};

\end{axis}
\end{tikzpicture}%
\end{figure}

\end{document}


Which results in this graph:

Questions:

1. As you can see the two background shadings have become the legend entrys... why?

2. Is there a better or proper way to shade the background of a plot?

3. How can I make pgfplots keep the black border around the whole plot? (not just the x and y axis)

Thank you all.

1. You need to pass the forget plot option to \addplot to exclude the background plots from the legend; I also commented out the option area style and shifted the legend a little (to prevent it from overlapping the plots) using at={(0.9,0.95) in the legend style key.

2. There might be another options using some coordinate systems, axis cs, or axis description cs, but your options is good.

3. Add the options axis on top to the axis environment; I also commented out the options axis x line*=bottom and axis y line*=left.

The code:

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{plotmarks}
\usepackage{tikz}
\begin{document}

\definecolor{mycolor1}{rgb}{1.00000,0.80000,0.40000}%

\begin{figure}
\begin{tikzpicture}

\begin{axis}[%
width=4.52083333333333in,
height=3.565625in,
%area style,
axis on top,
%stack plots=y,
scale only axis,
xmin=0,
xmax=5,
ymin=0,
ymax=125,
%axis x line*=bottom,
%axis y line*=left,
legend style={draw=black,fill=white,legend cell align=left,at={(0.9,0.95)}}
]
\addplot[fill=white!60!orange,draw=white!60!orange,forget plot] plot table[row sep=crcr]{0  125\\
2.5 125\\
}
\closedcycle;
\addplot[fill=mycolor1,draw=mycolor1,forget plot] plot table[row sep=crcr]{2.5  125\\
5   125\\
}
\closedcycle;
table[row sep=crcr]{0 0\\
1   1\\
2   4\\
3   9\\
4   16\\
5   25\\
};

table[row sep=crcr]{0 0\\
1   1\\
2   8\\
3   27\\
4   64\\
5   125\\
};

\end{axis}
\end{tikzpicture}%
\end{figure}

\end{document}


• @SteveHatcher You're welcome. I just updated my answer with a shift to the legend that might be of interest for you. May 13, 2014 at 3:03