how to define global coastline shapefile (boundary line) for a pcolor plot of data having range from 0 to 360 instead of -180 to 180

Hello all,
I was juts doing some seasurface temperature plots which has the range longitude from 0 to 360, the sample plot and data set(a single year data file is shared since the entire data set is too large to share) is attached below.
I would like highlight the coastline for this plot, which i wasnt able to do.

1 Comment

Hi Muskula,

To accomplish your task,load the sea surface temperature data from the provided SST_1year.mat file.

load('SST_1year.mat');

Plot the sea surface temperature data with the longitude range from 0 to 360.

figure;

imagesc(lon, lat, sst_data);

colorbar;

xlabel('Longitude');

ylabel('Latitude');

title('Sea Surface Temperature Plot');

Then, highlight the coastline by overlaying a coastline map on your existing plot by using the coast function from the Mapping Toolbox if available.

% Check if Mapping Toolbox is available

if license('test', 'map_toolbox')

    hold on;
    coast = load('coast.mat');
    plot(coast.long, coast.lat, 'k'); % Plot coastline in black color
    hold off;

else

    disp('Mapping Toolbox is required to plot the coastline.');

end

To define a global coastline shapefile for a pcolor plot in Matlab with a data range from 0 to 360, you can adjust the longitude values of the coastline data. You need to shift the coastline data points that fall in the range of 180 to 360 to negative values by subtracting 360 from them. This adjustment aligns the coastline data with the desired longitude range. Here is a simple example to illustrate this adjustment:

% Load coastline data

load coast

% Adjust coastline data for range 0 to 360

coast(coast(:, 1) >= 180, 1) = coast(coast(:, 1) >= 180, 1) - 360;

% Plot pcolor with adjusted coastline

figure

pcolor(X, Y, C)

hold on

plot(coast(:, 1), coast(:, 2), 'k')

Hope this answers your questions. Good luck

Sign in to comment.

Answers (1)

Thank you Umar for your reply,
I have tried the way that you have mentioned but it displays the following error
Error using load
Unable to find file or directory 'coast.mat'
To add, the mapping tool box is already installed and I am using Matlab R2022b version does the version needed to be updated?

11 Comments

Hi Muskula,
Did you write your Matlab code for the coast function and save it as coast.m?
Hello Umar,
Thank you for the reply,
Yes, Umar I have written the code for it. However, the coast.mat is replaced with coastline.mat in the latest versions of matlab.
But still the problem remains the same.
Hi @Muskula,
Did you download the coastline data directly from MATLAB's Mapping Toolbox using the load coast command because this command will load the coastline data directly into the workspace without the need for an external file.
Yes Umar, I have done the same (used the load coast command), I havent used any external file.
Hi Muskula,
Try updating the version and use coastline.mat since you mentioned in your recent posted comments that the coast.mat is replaced with coastline.mat in the latest versions of matlab. Let me know if that fix your problem.
I have modified the code but it shows some random horizontal lines,
I am attaching the plot as well as the updated code for your reference
clc;
clear all;
% Load sea surface temperature data
load('SST_1year.mat');
sst_data = SST;
% Determine the size of the SST data
[lat_dim, lon_dim, time_dim] = size(sst_data);
% Generate latitude and longitude vectors
lat = linspace(-90, 90, lat_dim);
lon = linspace(0, 360, lon_dim);
% Plot the sea surface temperature data
figure;
imagesc(lon, lat, sst_data(:,:,1)); % Display the first time slice
colorbar;
xlabel('Longitude');
ylabel('Latitude');
title('Sea Surface Temperature Plot');
set(gca, 'YDir', 'normal'); % Correct the Y-axis direction
% Check if Mapping Toolbox is available
if license('test', 'map_toolbox')
hold on;
% Check if 'coastlines.mat' is available
if exist('coastlines.mat', 'file')
coast = load('coastlines.mat');
% Check the fields of the coast structure
disp('Fields in coast structure:');
disp(fieldnames(coast));
% Assuming the fields are 'coastlat' and 'coastlon'
coastlon = coast.coastlon;
coastlat = coast.coastlat;
% Adjust coastline data for longitude range 0 to 360
coastlon(coastlon < 0) = coastlon(coastlon < 0) + 360;
% Plot coastline
plot(coastlon, coastlat, 'k');
else
disp('coastlines.mat not found. Plotting using built-in coast data.');
% Use built-in coast data
load coastlines;
% coastlon = coastlon;
% coastlat = coastlat;
% Adjust coastline data for longitude range 0 to 360
coastlon(coastlon < 0) = coastlon(coastlon < 0) + 360;
% Plot coastline
plot(coastlon, coastlat, 'k');
end
hold off;
else
disp('Mapping Toolbox is required to plot the coastline.');
end
% Load global coastline shapefile if available
if exist('coastlines.mat', 'file')
load('coastlines.mat');
else
disp('coastlines.mat not found. Plotting using built-in coast data.');
% Use built-in coast data
load coastlines;
end
% Check the fields of the coast structure
disp('Fields in coast structure:');
disp(fieldnames(coast));
% Adjust coastline data for range 0 to 360
longitudes = coast.lon;
latitudes = coast.lat;
% Adjust longitudes for correct plotting
longitudes(longitudes < 0) = longitudes(longitudes < 0) + 360;
% Plot pcolor with adjusted coastline
figure;
pcolor(lon, lat, sst_data(:,:,1)); % Display the first time slice
shading flat;
colorbar;
hold on;
plot(longitudes, latitudes, 'k');
xlabel('Longitude');
ylabel('Latitude');
title('Sea Surface Temperature with Coastline');
hold off;
Hi @Muskula,
You mentioned that “the coast.mat is replaced with coastline.mat” then why are you using coastlines.mat in your code, so that means you are not having issue with “Error using load Unable to find file or directory 'coast.mat'” and you are struggling to fix random horizontal lines in your plot and you are seeking solution on how to fix this problem, please correct me if I am wrong.
Hello Umar,
Sorry, its coastlines.mat but not coastline.mat, there was a mistake from my side.
Yes, I am struggling to fix random horizontal lines in my plot and I am seeking solution on how to fix this problem.
Hi @Muskula,
Here are a few possible causes and corresponding solutions.Random horizontal lines can sometimes be caused by artifacts in the data itself. These artifacts may arise due to measurement errors or inconsistencies in the data collection process. To address this, you can try applying data smoothing techniques to reduce the impact of these artifacts on the plot.One common smoothing technique is the moving average filter. This filter replaces each data point with the average of its neighboring points within a specified window size. By smoothing the data, you can reduce the impact of outliers and artifacts, resulting in a cleaner plot.Here's an example of how to apply a moving average filter to the sea surface temperature data before plotting:
% Apply moving average filter to the sea surface temperature data
windowSize = 5; % Adjust the window size as needed
smoothed_data = movmean(sst_data(:,:,1), windowSize);
% Plot the smoothed sea surface temperature data
figure;
imagesc(lon, lat, smoothed_data);
colorbar;
xlabel('Longitude');
ylabel('Latitude');
title('Smoothed Sea Surface Temperature Plot');
set(gca, 'YDir', 'normal');
Another possible cause of random horizontal lines is artifacts introduced during the plotting process. These artifacts can occur due to issues with the rendering engine or the way the plot is displayed on the screen.To address this, you can try using a different plotting function or adjusting the plot settings to minimize these artifacts. In the provided code, the imagesc function is used to create the plot. You can experiment with alternative plotting functions, such as pcolor or contourf, to see if they produce a cleaner plot.Here's an example of how to use the pcolor function instead of imagesc:
% Plot the sea surface temperature data using pcolor
figure;
pcolor(lon, lat, sst_data(:,:,1));
shading flat;
colorbar;
xlabel('Longitude');
ylabel('Latitude');
title('Sea Surface Temperature Plot');
set(gca, 'YDir', 'normal');
If the above solutions do not resolve the issue, it is possible that the data itself requires additional preprocessing before plotting. This could involve handling missing or invalid data points, normalizing the data, or applying other data transformations.For example, if the sea surface temperature data contains missing values represented by NaN (Not-a-Number), you can replace these values with an appropriate fill value before plotting. This can be done using the isnan function and logical indexing.
% Replace NaN values with a fill value
fill_value = 0; % Adjust the fill value as needed
sst_data(isnan(sst_data)) = fill_value;
% Plot the sea surface temperature data with NaN values replaced
figure;
imagesc(lon, lat, sst_data(:,:,1));
colorbar;
xlabel('Longitude');
ylabel('Latitude');
title('Sea Surface Temperature Plot (NaN replaced)');
set(gca, 'YDir', 'normal');
By addressing these possible causes and implementing the corresponding solutions, you can fix the issue of random horizontal lines appearing in the plot of sea surface temperature data.
Hello Umar,
Thank you very much for your time and patience in addressing all my queries.
The issue is solved now, the problem was with my version of matlab, I updated to latest version and the issue is sorted now.
Thank you once again!
Hi Muskula,
No problem, glad to help out. It’s a good sign that issue is resolved. Please let me know if you have any further questions.

Sign in to comment.

Categories

Asked:

on 27 Jul 2024

Commented:

on 30 Jul 2024

Community Treasure Hunt

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

Start Hunting!