How to change image axis labels

Prayudha Hartanto picture Prayudha Hartanto · Feb 25, 2013 · Viewed 30.6k times · Source

I'm trying to change the image axis labels with some latitude/longitude but I can't find how to do it. I tried some basic commands like:

imagesc(data)
axis(meshgrid([-180:20:180],[-90:20:90]))
colorbar

but these expression appeared:

imagesc(data),axis(meshgrid([-180:20:180],[-90:20:90])), colorbar Operands to the || and && operators must be convertible to logical scalar values.

Error in axis>allAxes (line 448)
result = all(ishghandle(h)) && ...

Error in axis (line 57)
if ~isempty(varargin) && allAxes(varargin{1}). 

Can anybody help me? FYI, my image axis labels are the data order (from 0 to N).

My desired results is an image looks like a world map, with graticule/grid lines as the axes. It should be looked like this:

enter image description here

Answer

Eitan T picture Eitan T · Feb 25, 2013

From your question I infer that you want to set the x-axis labels from -180 to 180, and the y-axis labels from -90 to 90. To do this, you should change the XTickLabel and YTickLabel properties of the axis object (note that you'll also need to adjust the number of ticks in each axis by modifying the XTick and YTick properties accordingly).

So, assuming that your image is stored in the matrix data and you display it with imagesc(data), here's how to change the tick labels in the x-axis to be from -180 to 180:

xticklabels = -180:20:180;
xticks = linspace(1, size(data, 2), numel(xticklabels));
set(gca, 'XTick', xticks, 'XTickLabel', xticklabels)

Similarly, here's how to change the tick labels in the y-axis to be from -90 to 90:

yticklabels = -90:20:90;
yticks = linspace(1, size(data, 1), numel(yticklabels));
set(gca, 'YTick', yticks, 'YTickLabel', flipud(yticklabels(:)))

This is what it should look like:

enter image description here