MATLAB Answers


Adding a marker to Data Marker to Heatmap (via imagesc)

Asked by Jason
on 21 Feb 2019
Latest activity Commented on by Jason
on 4 Mar 2019
Hello, i have a matrix Ib
2.29 2.29 2.29 2.29 1.89 1.89 1.89 1.89 1.77
2.29 2.29 2.29 2.29 1.89 1.89 1.89 1.89 1.77
1.77 1.77 1.77 1.77 1.89 1.89 1.89 1.89 0
1.77 1.77 1.77 1.77 1.89 1.89 1.89 1.89 0
2.81 2.81 2.81 2.81 1.89 1.89 1.89 1.89 0
2.81 2.81 2.81 2.81 1.89 1.89 1.89 1.89 0
0 0 0 0 2.50 2.50 2.50 2.50 0
I want to view as a heat map, so use imagesc to do this:
imagesc(Ib2); colorbar; title('Std Background el','FontSize',8)
axis off
This gives me:
I want to be able to highlight any zeros visually and so wanted to either colour that data with a green colour, or if thats too difficuly just plot a green cross superimposed.
I have tried this:
subplot(7,6,[22,23,24]); hold on;
But this just creates a new plot over the top.
Any suggestions please.


Where are you defining the colourmap since hot is not the default colourmap? It looks, from the colourbar, to be a very coarse colourmap of maybe just 10 points?
You can edit a colourmap, such as hot, if you wish as e.g.
mycmap = hot( 10 );
mycmap( 1,: ) = [0 1 0];
colourmap( hAxes, mycmap );
where hAxes is the handle of the axes you want to apply it to.
Hi Adam. my values will range from zero to 4095. if i wanted to keep the course settings (10) but only wanted the value of zero to be green rather than the 1st range how could this be done. at the moment , the 1st color bin would be 0-40 ish right? i only want zero to be green.

Sign in to comment.

3 Answers

Answer by Adam
on 28 Feb 2019
Edited by Adam
on 28 Feb 2019
 Accepted Answer

I guess you could take the odd approach of making the axes green (when you plot an image the axes should be entirely hidden so their colour is irrelevant) and then use AlphaData on your image to make the 0s transparent.
hImage = imagesc( Ib2, 'AlphaData', Ib2 )
should work in this case, with e.g.
hAxes.Color = 'g';
where hAxes is your axes handle, which you can get as the returned argument from a call to subplot if you wish.
As an example:
figure; hAxes = gca;
imagesc( hAxes, magic( 25 ), 'AlphaData', magic( 25 ) > 30 );
hAxes.Color = 'g';
For you, as mentioned, it should be simpler though as your data should be its own mask if it is just 0s you want to make green. In my example I just used 'magic' as I often do as a quick way to create some image, but it does not include any 0s so I had to create a mask of all values > 30 instead.
Note though that it is important to set axes colour after the imagesc as imagesc is one of those functions that also resets various axes properties, including the colour.


Hi adam, I have attempted your suggestion:
%-------------Test colormap set to green-----------------------
Nse = load ('saveA.mat','Nse') %read in matrix (reads in as a struct)
imagesc(Nse, 'AlphaData', Nse< 1 ); colorbar; title('Noise el','FontSize',8)
hAxes.Color = 'g';
But I get the following
This is my data
84.30 104.47 108.86 113.34 99.12 88.71 71.69 55.57 61.42
103.37 79.61 58.74 48.40 64.62 32.99 38.50 33.06 21.38
95.01 72.26 66.59 71.89 74.87 51.98 55.13 44.79 41.83
72.35 5
5.28 49.00 48.29 53.62 35.37 36.35 30.01 0.50
You need the mask the other way round - i.e.
Nse > 1
because 1 in the mask is opaque data and 0 is transparent. In your case you made all data < 1 opaque.
Didn't you say you wanted only exactly 0s to be green though originally? If so than simply using Nse as your mask would work, though in the data you give here you have no 0s anyway.
Perfect, thankyou.
I may need to choose the level, for now I have chosem it to be 1.
Thnaks again

Sign in to comment.

Answer by Krishna Zanwar on 27 Feb 2019

Hey Jason,
The second subplot command is creating another plot over the top of your original plot. Just remove the second subplot command and your code should work fine.


Hi Krishna, thanks for your help. But I use hold on.
I also do some plotting on other subplots and then comeback to the on I need after some calculations so need to identify the actual subplot.
Hey Jason,
As a work around you can remove the colorbar from the previos image and it will work.

Sign in to comment.

Answer by Walter Roberson
on 28 Feb 2019

temp = hot(10);
cmap = repelem(temp, 409, 1);
cmap(1,:) = [0 1 0];
This is not exactly correct as it creates a 4090 entry colormap instead of 4096.


Sign in to comment.