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

6 ビュー (過去 30 日間)
Jonathan Whiting
Jonathan Whiting 2016 年 10 月 14 日
コメント済み: Walter Roberson 2016 年 10 月 17 日
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 件のコメント
Star Strider
Star Strider 2016 年 10 月 14 日
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 2016 年 10 月 17 日
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.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2016 年 10 月 15 日
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 件のコメント
Jonathan Whiting
Jonathan Whiting 2016 年 10 月 17 日
編集済み: Jonathan Whiting 2016 年 10 月 17 日
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 2016 年 10 月 17 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeContour Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by