Make contour labels smaller?

1 view (last 30 days)
Abigail Waring
Abigail Waring on 12 May 2021
Commented: Abigail Waring on 4 Jun 2021
I have this contour map and the white labels are huge and I'm not sure how to reduce them and make them smaller.
Any ideas?
Heres the code
% ERA5_contourf_1
% Choose netcdf file
nc_file = '../data/ERA/ERA5_1979_1984.nc'; % put the full path to the netcdf file between the inverted commas
% Choose what time to plot (the nearest available data will be plotted)
chosen_time_datestr = '2008-06-29-10.00'; % edit this to plot different time-slices
chosen_time_datenum = datenum(chosen_time_datestr, 'yyyy-mm-dd-HH.MM');
%% Look at netcdf file metadata
% ncdisp(nc_file);
%
% Source:
% G:\sheffield_fellowship\teaching\pac_dissertations\2020_2021\abigail_waring\data\ERA\test 1979-2009.nc
% Format:
% 64bit
% Global Attributes:
% Conventions = 'CF-1.6'
% history = '2021-02-10 19:19:08 GMT by grib_to_netcdf-2.16.0: /opt/ecmwf/eccodes/bin/grib_to_netcdf -S param -o /cache/data3/adaptor.mars.internal-1612984616.206418-32671-22-77884e71-90b2-4259-9917-b6355929a1cd.nc /cache/tmp/77884e71-90b2-4259-9917-b6355929a1cd-adaptor.mars.internal-1612984616.2070854-32671-10-tmp.grib'
% Dimensions:
% longitude = 240
% latitude = 323
% time = 1575
% Variables:
% longitude
% Size: 240x1
% Dimensions: longitude
% Datatype: single
% Attributes:
% units = 'degrees_east'
% long_name = 'longitude'
% latitude
% Size: 323x1
% Dimensions: latitude
% Datatype: single
% Attributes:
% units = 'degrees_north'
% long_name = 'latitude'
% time
% Size: 1575x1
% Dimensions: time
% Datatype: int32
% Attributes:
% units = 'hours since 1900-01-01 00:00:00.0'
% long_name = 'time'
% calendar = 'gregorian'
% t2m
% Size: 240x323x1575
% Dimensions: longitude,latitude,time
% Datatype: int16
% Attributes:
% scale_factor = 0.001321
% add_offset = 278.4375
% _FillValue = -32767
% missing_value = -32767
% units = 'K'
% long_name = '2 metre temperature'
%% Open netcdf file
disp(['Loading ' nc_file]);
% Get variables from netcdf file
% Extract required variables
longitude = double(ncread(nc_file,'longitude')); % 'degree_east'
latitude = double(ncread(nc_file,'latitude')); % 'degree_north'
time = ncread(nc_file,'time'); % 'hours since 1900-01-01'
t2m = ncread(nc_file,'t2m')-273.15; % 'Centigrade'
t2m = permute(t2m,[2,1,3]);
% Prepare example data to plot
[lon_grid, lat_grid] = meshgrid(longitude, latitude);
t2m_eg = t2m(:,:,1);
% Convert to MATLAB time format
matlab_datetime = datetime(1900,1,1) + hours(time);
matlab_datetime = datenum(matlab_datetime);
% Get the closest data to the time indicated
[min_time_diff, min_time_diff_idx] = min(abs(matlab_datetime - chosen_time_datenum));
%% Load coastline
S = shaperead('../data/gshhs/GSHHS_shp/l/GSHHS_l_L1.shp', 'UseGeoCoords', true);
%% Plot example time-slice from netcdf file
disp('Plotting and saving a figure...');
% Make a figure
figure('units','normalized','outerposition',[0 0 1 1],'visible','on');
set(gcf,'color','w');
% Set axis font size
font_size = 12;
set(0, 'DefaultAxesFontSize', font_size);
% Make a map axis to encompass the data
ax = worldmap([nanmin(lat_grid(:)) nanmax(lat_grid(:))], ...
[nanmin(lon_grid(:)) nanmax(lon_grid(:))]);
% Allow overplotting
hold on
% Plot a pseudocolour plot of the chosen time-slice of the data
contourm(lat_grid, lon_grid, t2m(:,:,min_time_diff_idx), 'LevelStep', 5, ...
'LabelSpacing', 5, 'ShowText', 'on', 'fill', 'on', 'LineColor', [0.4 0.4 0.4]);
% Plot the gsshs coastlines using geoshow
geoshow(S, 'DisplayType', 'polygon',...
'FaceColor', 'none', 'EdgeColor', [0.1 0.1 0.1]);
% Make a colourbar
cbar = colorbar('eastoutside','fontsize',font_size);
% Add a colourbar label
set(get(cbar,'Xlabel'),'String','Temperature (^oC)');
% Add a scale bar
scb = scalebar('color',[0.3 0.3 0.3],'FontSize',font_size,'location','southeast');
% Add a title
title(datestr(matlab_datetime(min_time_diff_idx), 'HH.MM-dd-mm-yyyy'));
  2 Comments
Voss
Voss on 12 May 2021
To me, it looks like the problem is not that the contour labels are too big, but that there are too many of them. To change this, you might try changing the LabelSpacing from 5 to something like 144.
Abigail Waring
Abigail Waring on 4 Jun 2021
Thank you @Benjamin worked perfectly!

Sign in to comment.

Answers (1)

DGM
DGM on 12 May 2021
Edited: DGM on 12 May 2021
It can be done, but it's a little awkward:
% this is just some test data
lat = 27.988056+(1:50);
lon = 86.925278+(1:50);
[lat lon] = ndgrid(lat,lon);
h = egm96geoid(lat,lon);
[c,hc] = contourm(lat,lon,h,'LevelStep',20,'ShowText','on');
axis equal
hch = get(hc,'children'); % labels and lines
hch = hch(isgraphics(hch,'Text')); % select only labels
set(hch,'fontsize',8) % set font size of all labels
Forgive the ugly example plot, but you get the idea.

Community Treasure Hunt

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

Start Hunting!