MATLAB Answers

Overlay of dashed cells on imagesc

3 views (last 30 days)
simonb on 8 Apr 2018
Commented: simonb on 8 Apr 2018
I have a 20x20 matrix that I use to create a figure via imagesc which looks like this:
The matrix has numbers between 0 and 40+. I would like to be able to create an overlay such that I can automatically create a subtle pattern for cells below a threshold (e.g., 20) so that they are dashed over. Looking like this:
Instead of painfully doing this via paint (like I did for this figure), I had created something simple where thresholdmet=data<20; is a new matrix, and use that to create another imagesc. I played with transparency, and where it's not a hard black but a pattern, but could never get it to work.
It obviously does not have to be the dashes, even a subtle X or something else that does not hide the color underneath would work. Any thoughts or help would be very appreciated!

Accepted Answer

Ahmet Cecen
Ahmet Cecen on 8 Apr 2018
I just answered a question like this, but you have a unique aspect with the pattern. See if you can make sense of this:
A = zeros(21,21);
A(15,11) = 1;
A = 10000*imgaussfilt(A,7);
Mask = A < 20;
%%Scale the Pixels UP
ABig = repelem(A,11,11);
MaskBig = repelem(Mask,11,11); %Mask should be 1 where there will be patterns.
%%Create Diagaonal Pattern - This is a little Derp
%Someone Else might have a better way.
Pattern = diag(ones(2,1),-9) + diag(ones(5,1),-6) + diag(ones(8,1),-3) ...
+ diag(ones(8,1),3) + diag(ones(5,1),6) + diag(ones(2,1),9) ...
+ diag(ones(11,1),0);
Patterns = repmat(Pattern,size(Mask));
%%Plot Overlay
% Behind Image
a1 = axes;
h1 = imagesc(a1,ABig); colormap(a1,'parula'); c1 = colorbar; axis image;
% Forward Image
a2 = axes;
h2 = imagesc(a2,Patterns); colormap(a2,'gray'); c2 = colorbar; axis image;
% Make Forward Image Transparent at Padding
alpha(h2, (MaskBig > 0) * 0.2);
% Remove the Axes (the white canvas in a normal figure)
a1.Visible = 'off';
a2.Visible = 'off';
% Make background colorbar invisible.
% If you delete the colorbar, they will not align.
c1.Visible = 'off';
  1 Comment
simonb on 8 Apr 2018
This was super helpful, and exactly what I needed. Thank you so much!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!