Why doesn’t contour plot run when I add ‘ShowText’? Is my data set too big?

Hi y’all. I am trying to create an oceanographic t-s contour plot (salinity as x-axis, temperature as y-axis, and contour defined by density). When I run the code without ‘ShowText’, it takes a few seconds, but it runs. But MATLAB can’t produce a plot when I want to add labels on the contour lines. Is it because my data set it too long (6522 rows)? Is the function slowing everything down? My code is shown in comments below.

2 Comments

function [rho] = density(S,T)
RHO0 = R0+T.*(R1+T.*(R2+T.*(R3+T.*(R4+T.*R5))));
A = A0+T.*(A1+T.*(A2+T.*(A3+T.*A4)));
B = B0+T.*(B1+T.*B2);
RHO = RHO0+S.*(A+B.*sqrt(S)+C.*S);
rho = RHO ./ 1000;
end
figure(1)
T = stations.Temperature;
S = stations.Salinity;
[X,Y] = meshgrid(S,T);
Z = density(X,Y);
contour(X, Y, Z, 'color', 'k', 'ShowText','on', "LabelFormat","%0.1f");
I actually ended up leaving the code to run and it took maybe a half hour and produced this graph. I'm not sure how to prevent the repetition on the contour text. I'm not sure why it shows so many.

Sign in to comment.

 Accepted Answer

We don't have the ranges for T,S but they appear to be pretty closely spaced from the plot. Either only plot every Nth point or use the 'LabelSpacing' named parameter to reduce the number of labels shown.
The idea with data reduction---
T = stations.Temperature;
S = stations.Salinity;
nSpacing=10;
[X,Y] = meshgrid(S(1:nSpacing:end),T(1:nSpacing:end));
Z = density(X,Y);
contour(X, Y, Z, 'color', 'k', 'ShowText','on', "LabelFormat","%0.1f");
Or, with all data plotted but reducing the number of labels shown...
T = stations.Temperature;
S = stations.Salinity;
nSpacing=10;
[X,Y] = meshgrid(S,T);
Z = density(X,Y);
contour(X, Y, Z, 'color', 'k', 'ShowText','on', "LabelFormat","%0.1f",'LabelSpacing',144*nSpacing);
The default for 'LabelSpacing' is 144 points, the above sets a multiplier on that value. This is a single vaue for the whole plot, given the appearance that the point density is higher at larger X, you may need to use a variable selection of point spacing instead to get a more uniform spread of written labels.

5 Comments

The first code kind of works. The second takes an unknown amount of time to run.
Maybe I just need to adjust my souce data.
T = stationALL.PotentialTemperatureITS90DegC;
S = stationALL.SalinityPracticalPSU;
nSpacing = 20;
[X,Y] = meshgrid(S(1:nSpacing:end),T(1:nSpacing:end));
Z = density(X,Y);
contour (X, Y, Z, 'color', 'k', 'ShowText','on');
You clearly still have a huge number of points in the last trace...
Attaching the data files would help others to at least poke at it rather than guessing at trying to duplicate.
It's likely the second is time-consuming simply because of the number of points and the amount of computation required to try to keep labels from being within X points of each other.
Have you played with changing the decimation factors and/or making different factors for S,T and or adjusting them by spacing?
Without knowing what the actual data spacing is, it's pretty much beyond our being able to tell you anything more specific other than there are too many points too close to each other and that is dependent upon where in the T,S plane you look.
Try combinng both forms at once...
T = stationALL.PotentialTemperatureITS90DegC;
S = stationALL.SalinityPracticalPSU;
nSpacing = 20;
[X,Y] = meshgrid(S(1:nSpacing:end),T(1:nSpacing:end));
Z = density(X,Y);
contour(X,Y,Z,'color','k','ShowText','on', "LabelFormat","%0.1f",'LabelSpacing',144*nSpacing);
Or, alternatively, limit the number of points overall...
NT=50; % cut it down to 2500 points overall, work from there
NS=50;
T=stationALL.PotentialTemperatureITS90DegC; % get the original
S=stationALL.SalinityPracticalPSU;
T1=linspace(min(T),max(T),NT); % vectors within grid ranges
S1=linspace(min(S),max(S),NS);
[X,Y]= meshgrid(S1,T1); % subsequent grid
Z= density(X,Y);
contour(X,Y,Z,'color','k','ShowText','on');

Sign in to comment.

More Answers (0)

Categories

Find more on Oceanography and Hydrology in Help Center and File Exchange

Products

Release

R2024b

Asked:

on 24 Feb 2025

Commented:

on 27 Feb 2025

Community Treasure Hunt

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

Start Hunting!