MATLAB: imagesc() and image() display the same colormap differently - matlab

I have some data I would like to display in a picture form. In one case I want to rescale the x and y axis, which leads me to using imagesc. The issue is that the same colormap (jet) looks different in imagesc as compared to image.
Is there a way to make them the same?
I am using MATLAB R2014a.
Demonstration:
Here is how I show them:
figure; image(cancelledmap); colormap(jet); %image
figure; imagesc(y,x,cancelledmap); colormap(jet); %imagesc
And the output:
The colormap settings for both figures are somehow the same, however:

imagesc scales the color axis, whereas image does not. That's why the colors look different. If you click the colorbar button you'll see that they are on different color scales.
You can change the colorscale with caxis.
By the way, if you only want to scale the X- and Y-axes, you can use either function. Just supply your own scaled x and y vectors.

Related

Matlab: How can I control the color of a streamtube plot?

I am currently trying to plot 3D streamtubes. I want the tubes to be colored corresponding to their respective velocity (e.g. slow = blue, fast = red).
To be more exact, I have three 3D-matrices containing the velocity in x, y and z direction. The color of the streamtubes should be sqrt(vx^2+vy^2+vz^2). When using streamtube(x,y,z,vx,vy,vz,sx,sy,sz) the tubes are colored according to their z-coordinate which is useless because it's a 3D plot anyway.
Well this wasn't easy (it ought to be a builtin option), but by modifying the CData of each tube (they are each their own graphics object), you can achieve the desired result. Here's an example
load wind
[sx,sy,sz] = meshgrid(80,20:10:50,0:5:15);
h=streamtube(x,y,z,u,v,w,sx,sy,sz);
drawnow
view(3)
axis tight
shading interp;
This gives this picture:
but then doing this:
vel=sqrt(u.^2+v.^2+w.^2); %// calculate velocities
for i=1:length(h)
%// Modify the colour data of each tube
set(h(i),'CData',interp3(x,y,z,vel,get(h(i),'XData')...
,get(h(i),'YData'),get(h(i),'ZData'),'spline'))
end
drawnow
view(3)
axis tight
shading interp;
gives this result
NOTES:
1) I don't know if this is fully correct, I don't know how to test it
2) You have to interpolate the velocity data from the points where it's known onto the vertices of the streamtubes
3) I found the spline interpolation option to work best, but the other options might work better in other cases

Surface plot on top of colour image (x-y plane)

I guess, I've got an colormap issue with Matlab2013. I plot a 3d surface plot with colormap hot and I would like to have in the same plot an bitmap (8bit colour image) on the x-y plane. Plotting both separately from each other works fine but as soon as I plot them in one figure the first surface plot is only black. I guess it is because the RGB image on the x-y plane uses a different colour map. Is there an option in Matlab to plot two different type of images in the same plot?
surf(X,Y,density,'FaceColor','texturemap','Edgecolor','none')
colormap hot
...
%// define the location of the bitmap
xImage = [miX maX; miX maX]; %// The y data for the image corners
yImage = [miY miY; maY maY]; %// The x data for the image corners
zImage = [zDist zDist; zDist zDist]; %// The z data for the image corners
surf(xImage,yImage,zImage,... %// Plot the surface
'CData',RGBImage,...
'FaceColor','texturemap');
Thanks!
Durin
I think this is an issue with the relative scaling of density and zImage. I can replicate this by doing the following:
1) Plot a surf where the third input is n x m which is scaled like some real data (-0.2 to +0.2, for example). This responds to changes in colormap as you'd expect.
2) After hold on, plot another surf where the third input is n x m x 3, like a RGB image, double values scaled between 0 and 1.
This causes the first image to go "dark" (or whatever the lowest color in that particular colormap is). The issue is that they share their CLim, being in the same axis, although the RGBImage doesn't, in fact, reference the colormap.
This is "fixable" by scaling/normalising the first plot (your density values) to be between 0 and 1 (in this case) - although this quick fix is going to give you issues if you then want to add a colorbar. Alternatively, first grab the "CLim" from your axis after plotting the first surf:
trueC = get(gca,'CLim');
Then set it back after you plot the image:
set(gca,'CLim',trueC)

How can we fill different levels of contour with a color in matlab

I have a figure which consists of different levels of a contour plotted using the hold on function. I want to fill the space between the levels of contour with a color. Could you please assist me how can I do that. I have already tried the contourf function. The figure consists of different red colored levels of a contour, what I want is a solid color filled between these contour levels.
I am not sure if I got exactly what you wanted but here goes a possible solution to your problem.
If you plotted using the hold on function I would therefore assume you have each contour in a different variable. If so you can use logic to check whether a contour is higher or lower relatively to another. If your problem is that you do not have the same x axis for each so that you can use logic just interpolate for each contour (for example between -0.8 and 0.8)
I will give an example of what I am saying. See if this helps.
%simulated contours
x=linspace(0,pi,100);
y = sin(x);
y2 = sin(x)*2;
y3 = sin(x)*3;
figure, hold on,
plot(x,y),
plot(x,y2),
plot(x,y3),
%fill contours
[X,Y]=meshgrid(x,0:3/100:3);
zzz=(Y<repmat(y,size(Y,1),1))+(Y<repmat(y2,size(Y,1),1))+(Y<repmat(y3,size(Y,1),1));
figure,imagesc(zzz)
set(gca,'YDir','normal')

Colorbar is not showing the colors I want

I have a similar question than the one in this post.
I have a grayscale image and I plot points on it. Fro plotting the points I use colormap('jet') but as I want the image to be grayscale, after plotting the points I reset the colormap, colormap('gray').
But I want to show a colorbar! And the colorbar is plotted in grayscale, not 'jet'.
How can I do that?
EDIT:
I want a Colorbar showing the color of the points!
You should convert your image to RGB by putting the same data into R-, G-, and B-channels (this will be grayscale RGB image). Colormap in MatLab is not applied to RGB images, only to indexed ones. Then plot your points over the image with colormap you like.
As discussed here, there's a few ways:
If you have the image processing toolbox, use subimage to create an independent image with a separate colormap. Then plot the image, your points, and join them into one using linkaxes.
Use freezeColors from the file exchange (or multiple colormaps, which I haven't ever tested personally). This is a very easy way to create a larger colormap, and automatically selecting the right portion of the colormap for display of images and colorbars.
As answered by anandr, convert your greyscale image to RGB; Matlab doesn't use colormaps on RGB images, which leaves you freedom to plot your points and show their colorbar independent of the image.
Example code for (3):
I = imread('cameraman.tif');
imshow(cat(3,I,I,I))
hold on
x = #() round(size(I,1) * rand(50,1));
y = #() round(size(I,2) * rand(50,1));
plot(x(), y(), 'r.')
plot(x(), y(), 'g.')
plot(x(), y(), 'b.')
colormap('jet')
colorbar
result:

How do you draw different surfaces with the same color scale in MATLAB?

I'm trying to represent several surface plots* for which the scale differs a bit. Each surface plot is drawn in a separate subplot and/or figure.
Right now, I'm using the default color mapping, which automatically scales the whole range of the color map to my figure, i.e. the maximum of my surface is always red (in 'jet' color mode) regardless of the magnitude of this maximum.
I'd like the colormap to be consistent between the figures instead of spread between the min and max of each individual graph. That way, readers could appreciate the difference in scale of the surfaces just by looking at the color map.
Any idea on how to do this?
**Actually, in case it makes a difference, I'm plotting results of a surface fitting operation using the plot command as follows:*
[myfit, gof] = fit( ... );
plot(fit)
You should use the caxis function. For example, if one surface has a height from 0 to 5 and the other has a height from 0 to 10, doing the following for both plots:
caxis([0 10]);
will force them both to use the same color scale as the plot that covers the larger range. You can also call caxis with an axes handle as the first argument:
caxis(hAxes, [0 10]); % Sets the color scaling for hAxes
If not specified, caxis adjusts the color scaling of the axes that is current.
I recently answered this question in video form on my blog:
http://blogs.mathworks.com/videos/2009/03/27/setting-the-colormap-to-be-consistent-across-axes/