Need to change colormap on scatter after a hold on
41 views (last 30 days)
Show older comments
I am using imagesc to plot a gray scale image. I then want to overplot points with a jet colormap. To do this I am using scatter. The code below shows the basics.
tiledlayout(3,3,'TileSpacing','compact','Padding','compact');
nexttile([3,2])
%
imagesc(xax(col_in:col_out)/1000,altsfud(row_in:row_out),flipud(A_cut),clims); % this is the final image
xlabel('km')
ylabel('Altitude (km)')
axis xy
axis image
colormap(gray);
hold on
% now I use scatter to overplot the points and try to change the colormap to jet
scatter(xax(a_col)/1000,altsfud(a_rowfud),sz,a_time);
colormap(jet);
colorbar
hold off
When I run this code I get the following image. I clearly have not managed to set the colormap correctly. Do I need a second axes somehow? I thought hold on would let me change the colormap, but apparently not.
thanks
3 Comments
dpb
on 2 Aug 2023
Oh, pooh! It looks like the image was lost when fixed the code formatting somehow...unintended side consequence, indeed.
@Matthew McHarg, my bad; please reload the image (but next time, also format your code, and I won't be tempted to fix it :) !!)
dpb
on 2 Aug 2023
However, as @Image Analyst notes and you've observed, the colormap doesn't change the colors of the scatter object; to do that as noted, you would need to map the colors vector to scatter to match the desired colormap rgb values as function of the desired scale (your a_time in above code).
Again, sorry about the image...
Answers (2)
dpb
on 2 Aug 2023
Edited: dpb
on 2 Aug 2023
Despite my gaffe in deleting the sample image, I think I can illustrate how to do what you're wanting...
Z = 10 + peaks; % stolen from an imagsc() example
imagesc(Z)
G=colormap('gray'); % set, save the desired colormap
hold on
c=G(fix(linspace(2,size(G,1)-1,10)),:); % scale the scatter points across G, this is just linear
hSc=scatter(randi(round(xlim),10,1),randi(round(ylim),10,1),50,c,'filled'); % and draw 'em
If the scaling is proportional to your range by whatever is the emphasis variable, then the two will map together using same colormap scheme. However, as noted, colormap() itself is not used by the line/scatter plot colors; they use the given rgb color.
Hope that helps...
2 Comments
dpb
on 2 Aug 2023
Actually, there is a way to do it that way -- but the a_time variable will need to be scaled to the size of the colormap (or the portion it's to map to), not the actual times.
The example for scatter does it that way...
hAx(1)=subplot(2,1,1);
x = linspace(0,3*pi,200);
y = cos(x) + rand(1,200);
c = linspace(1,10,length(x));
scatter(x,y,[],c)
colormap(hAx(1),gray)
hAx(2)=subplot(2,1,2);
scatter(x,y,[],c)
colormap(hAx(2),jet)
Now the colormap does affect the scatter object.
Walter Roberson
on 3 Aug 2023
Any given axes can only have one colormap. (Originally it was only one colormap per figure!)
You have several possibilities:
- create multiple axes, possibly in exactly the same place, possibly using yyaxis -- since you can have one colormap for each axes
- after doing the imagesc(), use the File Exchange contribution freezecolors
- when you scatter, instead of specifying numeric values for colors and counting on caxis and colormap, use rescale to [0 255], uint8 the result, and ind2rgb to create specific RGB colors for each of the markers, and specify those as the colors in the scatter() call
- take the data you were going to imagesc(), rescale to [0 255] and uint8, then replicate it along the third dimension to end up with an RGB array that you image() instead of imagesc() . That will not need a colormap, so you can then scatter the color markers over it
0 Comments
See Also
Categories
Find more on Colormaps 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!