How can I create a correct contour plot with contourfm function?
Show older comments
Dear All, I have a lot of matrices what about I want to create colourfm maps. The values of the matrices varied between 1 and 31. I tried to create a map about one of them which values are: 1,3,4,5,6,11,12,17,18,19,20,21,22,28,29,30 with the following way:
worldmap('Europe');
load coast;
plotm(lat, long, 'k');
clear lat long;
caxis([1 31]);
colormap(cmap(1:31,:));
colorbar;
contourfm(latitude,longitude,IU2,31);
I received the Fig 1. It is wrong because it shows the color value 2. Moreover in Scandinavia the value is dominantly 30 instead of 31.
I tried also the
worldmap('Europe');
load coast;
plotm(lat, long, 'k');
clear lat long;
caxis([1 31]);
colormap(cmap(1:31,:));
colorbar;
contourfm(latitude,longitude,IU2,30);
but it also shows wrong colour values (Fig 2) moreover the colours does not change between some contour.
Can someone suggest me a correct solution for this issue? Thank you for your help in advance!
Answers (1)
Jacob Halbrooks
on 30 Nov 2026 at 15:27
In this example, contourfm is not well suited for displaying nominal or categorical data. The contourfm function treats the input data as continuous numeric values and creates evenly spaced intervals between them. This results in categories that will not directly match the nominal or categorical values in the input data.
The data in this question is likely from a geolocated data grid. There are two ways to display the raster data as categorical/nominal:
- If the latitude and longitude values are evenly spaced, you can convert to a raster reference object and use geopcolor.
- For any geolocated data grid, regularly spaced or not, you can use geoshow with DisplayType = “texturemap”.
A workflow example using geopcolor for evenly spaced raster data with a raster reference object is shown below:
- Define rectangular grid based on lat/lon data
- Create raster reference object based on input data
- Call geopcolor with IU2 data and newly created raster reference object
[latlim, lonlim] = bounds(aoiquad(lat, lon));
R = georefpostings(latlim, lonlim, size(IU2));
cmap = turbo;
geobasemap("none")
geopcolor(IU2, R)
hold on
load coastlines
geoplot(coastlat, coastlon)
colormap(cmap(1:31,:))
clim([0.5 31.5])
colorbar(Ticks=1:31)
A workflow example using geoshow for a geolocated grid, regularly spaced or not, is shown below. For this example, you can replace the call to contourfm with something like:
geoshow(latitude,longitude,IU2,DisplayType="texturemap")
A more complete code snippet may look like:
worldmap("Europe")
geoshow(latitude,longitude,IU2,DisplayType="texturemap")
load coastlines
plotm(coastlat,coastlon,"k")
colormap(cmap(1:31,:))
clim([0.5 31.5])
colorbar(Ticks=1:31)
Categories
Find more on Mapping Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!