How can I draw contour lines in my problem ?

Hi all,
I have created cell array(which is attached here) in which the first and second columns representing latitude and longitude of each weather station exisitng in the Alaska state. The third column is the probability that freezing depth in that station exceeds 1 feet. What I want to do is plot contour lines represing the probability values. Can anyone help me with regard?

 Réponse acceptée

load Behi1
data = cell2mat(POFDE1);
I = scatteredInterpolant(data(:,[1 2]),data(:,3));
Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.
[lat,lon] = meshgrid(unique(data(:,1)),unique(data(:,2)));
contour(lon,lat,I(lat,lon),'ShowText','on')
colorbar

9 commentaires

Thank you for your answer, but I get error. Please see the attached jpg file.
Torsten
Torsten le 6 Fév 2023
Modifié(e) : Torsten le 6 Fév 2023
Rename your .m file to a name different from contour.m.
It's in conflict with MATLAB's contour.m.
github_repo contains a contour.m that is interfering with calling the Mathworks contour function.
You will need to done one of several possibilities:
  • rename it to something that does not clash; or
  • edit it to be a function that is able to detect whether it is being called in a MATLAB contour() fashion and if so implements MATLAB's contour() itself and otherwise does whather it is currently designed to do; or
  • make sure it is not in your current directory and not in your MATLAB path; or
  • edit the MATLAB contour() function to detect whether it is being called with no parameters, and if so then have it run() the github_repo/contour.m script
great! Thank you so much.
I faced a problem while I was plotting contour lines based on other "POFDE" cell array and I think there must be something wrong with the scatteredInterpolant function. The problem is that, values in column 3 to 7 of data(or POFDE) are all probabilities, which must not exceed 1 and be negative values. While I ploted countour lines using the following code, I realized that some contours' values are greater than 1 and even negative. Would you please guide me in this regard? I am pretty sure that the probabilities I calculated before have reasonable values.
load('POFDE.mat');
data = cell2mat(POFDE);
[lat,lon] = meshgrid(unique(data(:,1)),unique(data(:,2)));
for i = 1:(size(data, 2)-2)
figure
I = scatteredInterpolant(data(:,[1 2]), data(:,i+2));
contourf(lon, lat, I(lat,lon), 'ShowText', 'on');
colorbar
title(sprintf('the probability of frost depth exceedance of %d feet', i))
exportgraphics(gca, sprintf('FrostPlot_%d_feet.png', i))
end
Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.
Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.
Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.
Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.
Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.
I = scatteredInterpolant(data(:,[1 2]), data(:,i+2),'nearest');
instead of
I = scatteredInterpolant(data(:,[1 2]), data(:,i+2));
Yes, by default scatteredInterpolant will perform linear extrapolation, which can give you values outside whatever range.
A couple ways around this are:
1. Turn off linear extrapolation:
load Behi1
data = cell2mat(POFDE1);
I = scatteredInterpolant(data(:,[1 2]),data(:,3),'linear','none');
Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.
[lat,lon] = meshgrid(unique(data(:,1)),unique(data(:,2)));
contour(lon,lat,I(lat,lon),'ShowText','on')
colorbar
2. Replace probabilities < 0 with 0 and > 1 with 1:
load Behi1
data = cell2mat(POFDE1);
I = scatteredInterpolant(data(:,[1 2]),data(:,3));
Warning: Duplicate data points have been detected and removed - corresponding values have been averaged.
[lat,lon] = meshgrid(unique(data(:,1)),unique(data(:,2)));
contour(lon,lat,min(1,max(0,I(lat,lon))),'ShowText','on')
colorbar
Thank you so much. Your second solution worked for me.
Another question, in some point on the maps where contours overlape, the numbers on them can be rarely visible.(please see the attached map). Do you have any idea on How I can scale my maps?
Voss
Voss le 8 Fév 2023
Modifié(e) : Voss le 8 Fév 2023
I don't know of a way to guarantee that the labels will always be legible, but you can try changing the LabelSpacing and/or TextStep properties, or set ShowText to 'off' to turn off the text labels entirely.

Connectez-vous pour commenter.

Plus de réponses (2)

Divyank
Divyank le 6 Fév 2023
The contour plot requires z-coordinates to be specified as a matrix and this matrix must have at least two rows and two columns, and it must contain at least two different values. However, the z-coordinates in POFDE1 is 1x273 cell array, according to my understanding of your problem, you can consider using plot3(X, Y, Z) instead of contour after extracting X, Y and Z co-ordinates from POFDE1 cell array in the following way:
% The input needs to be a scalar, vector or a matrix, hence conversion from
% cell array to vector is needed in this case, you might want to construct
% a matrix instead of a cell array to avoid this conversion.
X = cell2mat(POFDE1(:, 1));
Y = cell2mat(POFDE1(:, 2));
Z = cell2mat(POFDE1(:, 3));
plot3(X, Y, Z, '.');

1 commentaire

The user specifically asked for contour lines. Voss shows one way of doing scattered contour.

Connectez-vous pour commenter.

Walter Roberson
Walter Roberson le 6 Fév 2023

0 votes

https://www.mathworks.com/matlabcentral/fileexchange/38858-contour-plot-for-scattered-data is a File Exchange contribution for contour lines for scattered data.
You would probably still need to use cell2mat() like @Voss showed.

Catégories

En savoir plus sur Contour Plots dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by