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.

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:

MATLAB generated 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}
]
\addplot[fill=white!60!orange,draw=white!60!orange] plot table[row sep=crcr]{0  125\\
2.5 125\\
}
\closedcycle;
\addplot[fill=mycolor1,draw=mycolor1] plot table[row sep=crcr]{2.5  125\\
5   125\\
}
\closedcycle;
\addplot [color=red,solid,mark=o,mark options={solid}]
  table[row sep=crcr]{0 0\\
1   1\\
2   4\\
3   9\\
4   16\\
5   25\\
};
\addlegendentry{line1};

\addplot [color=green,solid,mark=square,mark options={solid}]
  table[row sep=crcr]{0 0\\
1   1\\
2   8\\
3   27\\
4   64\\
5   125\\
};
\addlegendentry{line2};

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

\end{document}

Which results in this graph:

latex render of same plot

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.

share|improve this question

1 Answer 1

up vote 8 down vote accepted

The answers:

  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;
\addplot [color=red,solid,mark=o,mark options={solid}]
  table[row sep=crcr]{0 0\\
1   1\\
2   4\\
3   9\\
4   16\\
5   25\\
};
\addlegendentry{line1};

\addplot [color=green,solid,mark=square,mark options={solid}]
  table[row sep=crcr]{0 0\\
1   1\\
2   8\\
3   27\\
4   64\\
5   125\\
};
\addlegendentry{line2};

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

\end{document}

enter image description here

share|improve this answer
    
simply incredible. Thank you very much! –  Steve Hatcher May 13 at 2:56
1  
@SteveHatcher You're welcome. I just updated my answer with a shift to the legend that might be of interest for you. –  Gonzalo Medina May 13 at 3:03

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.