Plot contour lines on top of imagesc?

Hi,
I'm trying to make a contour plot with one of the variables being discrete (integer). I try to combine somehow the 2 following plots:
y = [1:10]';
x = 1:0.01:10;
z = repmat(y, [1,901]) - repmat(x, [10,1]);
figure
subplot(2,1,1)
contourf(x,y,z)
xlabel('x (continuous)');
ylabel('y (discrete)')
subplot(2,1,2)
imagesc(flipud(z))
xlabel('x (continuous)');
ylabel('y (discrete)')
colormap(jet(256))
In the top panel I have a (filled) contour plot but the lines are diagonal (not step-like) so this is not good for integer y values. The bottom plot shows the discrete y values and (almost) continuous x values but I don't know how to draw the contour lines on top of this? Is there a quick way of doing this?
Thanks!

 Accepted Answer

Sean de Wolski
Sean de Wolski on 12 Dec 2012
Edited: Sean de Wolski on 12 Dec 2012
You can specify x/y for imagesc so that the image is not shown against its index. From there you can simply use hold on and contour:
y = [1:10]';
x = 1:0.01:10;
z = repmat(y, [1,901]) - repmat(x, [10,1]);
figure
subplot(2,1,1)
contourf(x,y,z)
xlabel('x (continuous)');
ylabel('y (discrete)')
subplot(2,1,2)
imagesc(x,y,flipud(z)) %specify x/y
xlabel('x (continuous)');
ylabel('y (discrete)')
colormap(jet(256))
hold on %hold it on
contour(x,y,flipud(z),'color','k') %contour over it
More per Comments
In that case I would grab the 'CData' from the image and bin it using histc (essentially replacing image values with indexes into contour levels). Then draw a contour plot of this.
y = [1:10]';
x = 1:0.01:10;
z = repmat(y, [1,901]) - repmat(x, [10,1]);
figure
subplot(2,1,1)
contourf(x,y,z)
xlabel('x (continuous)');
ylabel('y (discrete)')
subplot(2,1,2)
h = imagesc(x,y,flipud(z));
xlabel('x (continuous)');
ylabel('y (discrete)')
colormap(jet(256))
hold on
D = get(h,'CData'); %image data
[~,idx] = histc(D,linspace(m(min(D)),max(max(D)),10)); %bin it
contour(x,y,idx,'color','k') %contour of the bins

5 Comments

Thanks for your answer, but what I really want is to plot step-wise contour lines because one of the variables is integer. I want somehow the lines to follow the equal-color paths from the bottom panel (which are sets of vertical segments)
We're almost there... If I change the last line of your code to
contour(x,y-0.5,idx,'color','k') %contour of the bins
I get almost what I want, except that the vertical segments are not really vertical and y = 10 doesn't have the black lines... Could you please indicate how I can fix these last details?
Thanks!
LE: Actually just shifting y -> y - 0.5 doesn't work for all cases. For example here it gives strange results:
y = [1:10]';
x = 1:0.01:10;
z = (repmat(y, [1,901]) - 5) .^ 2 + (repmat(x, [10,1]) - 5) .^ 2;
figure
subplot(2,1,1)
contourf(x,y,z)
xlabel('x (continuous)');
ylabel('y (discrete)')
subplot(2,1,2)
h = imagesc(x,y,flipud(z));
xlabel('x (continuous)');
ylabel('y (discrete)')
colormap(jet(256))
hold on
D = get(h,'CData'); %image data
[~,idx] = histc(D,linspace(min(min(D)),max(max(D)),10)); %bin it
contour(x,y-0.5,idx,'color','k') %contour of the bins
Nevermind... I figured it out now... Thanks for your answer. That helped me.
You're welcome!
I would image it just came down to changing the edges in the call to histc()?

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!