Question

I am trying to plot contour lines of one data set on top of filled contours of another data set. Plotted individually they both look correct but when I combine them in the usual way the plots don't look right:

clc; clear all; close all;

x = linspace(-2*pi,2*pi);
y = linspace(0,4*pi);
[X1,Y1] = meshgrid(x,y);
Z1 = sin(X1)+cos(Y1);

[X2,Y2] = meshgrid(x,y);
Z2 = 1000*(sin(1.2*X2)+2*cos(Y2));

figure;
contourf(X1,Y1,Z1);
shading flat;

figure;
contour(X2,Y2,Z2,'k');

figure;
contourf(X1,Y1,Z1);
shading flat;
hold on;
contour(X2,Y2,Z2,'k');

enter image description here enter image description here enter image description here

Was it helpful?

Solution

To fix this you have to use caxis to set the limits for the contourf plot:

clc; clear all; close all;

x = linspace(-2*pi,2*pi);
y = linspace(0,4*pi);
[X1,Y1] = meshgrid(x,y);
Z1 = sin(X1)+cos(Y1);

[X2,Y2] = meshgrid(x,y);
Z2 = 1000*(sin(1.2*X2)+2*cos(Y2));

figure;
contourf(X1,Y1,Z1);
shading flat;
caxis([min(min(Z1)) max(max(Z1))]);
hold on;
contour(X2,Y2,Z2,'k');

You can replace min(min(Z1)) and max(max(Z1)) with the upper and lower limits that you want. This results in this plot:

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top