How do I replace NaN values in contourf with another plot?

21 views (last 30 days)
I am using contourf to plot salinity over a large region. Data only shows over ocean, so I was hoping to show an outline of the shoreline where NaN values appear on the contour. I've created a representative example in the following code:
X=rand(30,60);
X(10:25,10:30) = NaN;
figure();
subplot(2,1,1)
load coast;
lat2=[nan;lat;nan; lat];
long2=[nan;long; nan; long+360];
plot(long2,lat2,'k','linewidth',1)
axis([260 320 0 30]);
subplot(2,1,2)
contourf(X)
This example creates two subplots: the first shows the shorelines, the second shows contours with a hole (due to NaN values). I want to create one plot that shows the filled contours on top with the shorelines peaking through the hole.
I have tried making the NaN values transparent, but wasn't getting anywhere. Any help would be greatly appreciated!
  2 Comments
Star Strider
Star Strider on 14 Oct 2016
Are you going to share ‘coast’, ‘lat’ and ‘lon’ with us?
Does this require the Mapping Toolbox? If so, please add that in the Product Tags.
Also, your axis call is perplexing, because ‘0’ and ‘30’ as integers can be represented exactly in binary form. No rounding is necessary.
Jonathan Whiting
Jonathan Whiting on 17 Oct 2016
Sorry about that: 'coast', 'lat' and 'long' are all from the Mapping Toolbox, which has been added to the Product Tags. And sorry about the rounding, it is an artifact that I missed when simplifying the code for this example.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 15 Oct 2016
You need to reference your data to lat and long. The below code does not assume that the data is spaced in degree increments.
Z = rand(30,60); %numeric arrays use Y for rows and X for columns
long_start = 260; long_end = 320; num_long = size(Z,2);
lat_start = 0; lat_end = 30; num_lat = size(Z,1);
[X, Y] = meshgrid(linspace(long_start,long_end,num_long), linspace(lat_start, lat_end, num_lat));
Z(10:25,10:30) = NaN;
contourf(X, Y, Z);
hold on
load coast;
lat2=[nan;lat;nan; lat];
long2=[nan;long; nan; long+360];
plot(long2,lat2,'k','linewidth',1)
axis([long_start long_end lat_start lat_end])
hold off
The methods would be a little different if you were using contourfm() and plotm() to do the drawing.
  2 Comments
Jonathan Whiting
Jonathan Whiting on 17 Oct 2016
Edited: Jonathan Whiting on 17 Oct 2016
Thank you, this is exactly what I was looking for! Apparently my understanding of the hold function isn't great... Is there any good documentation to understand hold?
And setting up the meshgrid helped greatly.
Walter Roberson
Walter Roberson on 17 Oct 2016
All of the "high level" functions such as plot() usually clear the axes before drawing, whereas the "low level" functions such as "line" and "text" always add to existing plots. "hold on" mostly says "do not erase what has already been drawn"
The difficulty you were having was that your coordinates were wrong for your contour relative to your plot(). The default coordinates for contour() are the small integers, 1 to the number of rows or columns, whereas you were plotting near 260 . You needed to bring the two into alignment.

Sign in to comment.

More Answers (0)

Categories

Find more on Contour Plots in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!