Colorbar shifts upwards and associates colors with incorrect values when printing

8 views (last 30 days)
Dear all,
I have been having an issue for some time with the colorbar on some geophysical maps that I have been producing in Matlab. I will try to explain my problem qualitatively first, and go through a worked example below.
I have some gridded data (for example, the depth of a density surface in the global ocean) that I wish to plot as a surface, with the colours signalling the data value at each point. There are certain points on the surface that I wish to mask out. In this example they might correspond to land points, or points where the density surface is not present in the water column. Since there are multiple masks that I wish to apply, I have taken the approach to assign new values, just outside the ‘normal' data range, to the points I wish to mask. I set up a colormap that sets these outlying values to a specific color (usually white or grey or black). I then set the color axis limits to include all of the data values (including those that I have newly assigned), while setting the color bar limits to include ONLY those values within the valid data range. The result is a figure that includes both coloured data and different colours of masks, for which the associated colorbar displays only the coloured data range. I have spent some time refining this, and it works well. The problem comes when I print the figure to a file. When I do this, the colorbar is shifted upwards, so that the colour assigned to one of the masks now corresponds to the lowest value in the data range, while the upper value in the data range is missed off the top. The colours on the plot itself, however, are unaffected.
A simple worked example:
I have data gridded in a 5x5 matrix, the values of which range from 0 to 9 and there are some NaNs present.
x = [0 0 4 4 5; 0 3 4 5 6; 2 3 3 NaN NaN; 4 6 6 7 NaN; 4 5 6 8 9];
I wish to apply different masks to points at which the value is 3 (perhaps I know this is an invalid result) and NaN. I assign these points values just outside the valid data range (0 to 9).
valuethrees = -1;
valuenans = -2;
x(x==3) = valuethrees;
x(isnan(x))=valuenans;
I set up a colormap (using the RGB formulation), which has a certain colour for each of the masks (white [1,1,1] for the 3’s, and black [0,0,0] for the NaNs), and a certain color for each of the values in the data range (0 to 9).
CM = [0 0 0; 1 1 1; 1 0 1/10; 1 0 2/10; 1 0 3/10; 1 0 4/10; 1 0 5/10; 1 0 6/10; 1 0 7/10; 1 0 8/10; 1 0 9/10; 1 0 1];
I set the colour axis limits to include all of the values in the matrix, and the colorbar limits to include only those in the range 0 to 9. In doing this I set the limits to +-0.5 at either side so that the values fall nicely within each colour band defined in CM.
caxlims = [-2.5 9.5];
cbarlims = [-0.5 9.5];
I plot the figure using imagesc , and set up the colorbar appropriately.
figure;
imagesc(x);
colormap(CM);
caxis(caxlims);
t = colorbar;
set(t,ylim,cbarlims);
The figures looks just as I had intended it to. I save the figure as a PDF using export_fig .
export_fig(image,-pdf);
The resulting figure (attached) has the colorbar shifted upwards, so that the colour assigned to the 3’s (white) is now assigned to the lowest value of the valid data range (0). Note that the colours in the figure themselves remain correct. This problem is present when I print to any vector drawing (PDF or EPS) but is not the case when I print to JPEG or PNG format, but these latter formats have their own issues and are far from ideal for my purposes. The problem is also independent of whether I use export_fig, or Matlab’s own print.
I hope that this rather long explanation makes sense. I would appreciate anyone’s input on why this might be happening, and if there is a known way to fix it. I presume that it is something to do with the specific way in which my computer renders the figure, but I am far from an expert on that sort of thing so would appreciate someone’s guidance. What information I can give ahead of more specific guidance is that I am using Matlab R2013b on a linux machine running Mandriva 2010.2.
There is a work around for the problem that involves making the colorbar manually, but this is both tricky to implement in my codes and unsatisfying. It would be nice to know if there is an easier and more elegant solution.
Many thanks in advance for your help,
Graeme

Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!