Plotting pcolor plot on an image at a particular area.

Hi,
I have done some analysis on a particular region of an image which I now want to plot on the over the image. Following image is an example of what I am trying to get as output ( In my case it is a complete pacman shape but some region would be removed because of NaNs):
Now, I can plot the pcolor of the colored region separatly but am unable to get it with image. Colorbar changes to the gray values. Attached is the relevant binary matrix (region) which shows the region where the profile should be as well as image (img) which is suppose to be in the background and (out) is the values in the disc region.

2 commentaires

Hi,
It is not clear what you want to do with the 3 images you have attached. Can you explain clearly?
Hi Mahesh,
"region" is just a mask of the area in the image "img" over which I am trying to plot pcolor of the third variable. So idea is to plot the gray image in the background and then have pcolor plot over it. I hope this clearifies the statement. End result would be similar to something like the attached image with the question statement.

Connectez-vous pour commenter.

 Réponse acceptée

Walter Roberson
Walter Roberson le 8 Oct 2019

2 votes

pcolor() accepts x and y arguments as well as the data, so to plot it on top of something else, plot the other things first, "hold on", then pcolor() passing in appropriate coordinates.
pcolor() is, by the way, surf() followed by view(2) internally, except that pcolor() does not accept all of the options that surf() accepts.

3 commentaires

I followed the steps you mentioned as well. Results were again colormap changing to gray somehow.
All items on the same axes share the same colormap.
You can create multiple axes in the same place and have one colormap for each. If you do that, make sure that the background color of the top axes is set to 'none' .
However, the better approach is to convert the grayscale image to RGB so that it does not require a colormap. You can do that by using
rgb_version = repmat(YourGrayImage, 1, 1, 3);
Thank you for the suggestion. It was much simpler approach and did the job perfectly.
Cheers,

Connectez-vous pour commenter.

Plus de réponses (1)

You can just merge images
clc,clear
load matlab.mat
[m,n] = size(img);
[X,Y] = ndgrid(1:m,1:n);
Z = (X.*Y).*region; % try (X+Y)
Z = Z/max(Z(:)); % just scale 0 .. 1
% make RGB image: RED - 1, GREEN - changes, BLUE - 0
% result: red to orange
I1 = cat(3,Z*0+1,Z,Z*0);
mask = repmat(region,[1 1 3]);
I2 = double( repmat(img,[1 1 3]) );
I2 = I2/max(I2(:)); % scale 0 .. 1
I = I1 .* mask + I2 .* ~mask; % merge images
imshow(I);
cm = flipud(autumn);
colormap(cm)
h = colorbar;

20 commentaires

This is somewhat closer to what I am trying to plot but the scaling is an issue. Secondly, the end result is smoothed which is making it difficult to visualize the data that I want to observe. Any suggestions regarding that?
Cheers,
How do you do this with patch? Can you show?
I am not sure what you meant by patch. But, following is the plot that I am able to get using pcolor. This one of the exmples.i have changed xlim and ylim in these plots please ignore that. My goal is to achieve same sort of plot with image in the background. Something similar to what I shared in the question post.
I hope this clears things properly. Picture1.png
DId you try to change colormap or manipulate with caxis? Maybe it's just a question of color?
With the image in the background? Yes, I tried to plot output of pcolor with image in the background but the colormap changed to grayscale and if I changed the values of caxis then the image background was lost. I tried with different values and after futile efforts, decided to post it on the forum.
Can i see that script?
Sorry for the delay. I did not have access to MATLAB at that time.
imshow(img), axis ij tight equal
hold on,
pplot = pcolor(out);
pplot.LineStyle = 'none';pplot.FaceColor = 'flat';
But how do you paint that part in orange?
if we do not plot the imshow and simply use the code after pplot then you will get the contour. I am using hotprint colormap but any colormap should do the job.
Maybe create 2 different colormap
clc,clear
cla
load matlab.mat
h1 = imshow(img);
axis ij tight equal
% scale 'out': 255 .. 510
C2 = out - min(out(:));
C2 = C2/max(C2(:))*255;
C2 = round(C2)+255;
hold on
h2 = pcolor(C2);
hold off
set(h2,'EdgeColor','none')
set(h2,'FaceColor','flat')
% first 255 rows (1..255) is for 'img'
% second 255 rows (256..510) is for 'out'
colormap([gray(255); hot(255)]) % concantenate two different colormap
caxis([1 510])
% caxis([min(img(:)) max(C2(:))]) % doesn't work. Why?
This example from HERE
No this did not work. I dont know why but this should not be that difficult.
Here is what the script produces. Isn't it great?
img1.png
I am trying same on a different experiment with almost identical steps but plots are not the same. Visually, it is doing what we want in the end but problem again would be that we are rescaling the data for the colored region. Isn't it so?
Found the solution:
figure, ax1 = axes;
imshow(img)
axis ij equal tight
colormap(ax1,'gray')
hold on
ax2 = axes;
h2 = pcolor(out);
h2.EdgeColor = 'none';h2.FaceColor = 'flat';
axis ij equal tight
colormap(ax2,hot)
ax2.Visible = 'off';
linkprop([ax1 ax2],'Position');
colorbar
% YOU CAN SET THE VALUES OF CAXIS USING THE FOLLOWING COMMAND
%caxis(ax2,[lowerbound upperbound])
It is something similar to what you suggested regarding multiple colormaps.
Any suggestions on how to close the question? untitled.png
try this
caxis(ax2,[min(out(:)) max(out(:))])
Yeah I already did that. I am using custom limits for caxis.
Just a small followup question regarding the same question, I am using tight_subplot to plot different data points and now for that I am getting this white background which to my understanding is because I am generatiing new axis to plot data rather than using the axis that was generated by tight_subplot.
Is it correct? Any solutions regarding this issue. I do not want to use normal subplot but I am pretty sure I will have the same issues there as well. Attached figure will clear the question. untitled.png
You can hide it like you did before
ax2.Visible = 'off';
Or what do you mean? How do you want those figure look like?
I tried that but now the spacing between the figures is all messed up and apparently title xlabel etc are not working either. With tight_subplot, i can control the spacings too.
You can hide ticks
set(ax2,'xtick',[])
set(ax2,'xticklabel',[])

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by