How to define transparent element in colormap
Asked Answered
W

1

6

I would like to define a transparent color within the color map how do I do that?

The reason I need this is that I have a multiple layers in my axes (produced both by imagesc and plot). I know I could simply first use imagesc and then plot but I want to draw the lines behind non-zero values of the imagesc representation.

To color the zeros white I use

jet = colormap('jet');
jet(1:2,:) = 1;
colormap(jet);

Now I would like to make white transparent.

Workday answered 1/5, 2015 at 15:41 Comment(0)
K
11

The colormap doesn't have a fourth element for alpha, it's RGB only, so the way I do this sort of thing is to make a mask of the desired transparency layer - binary or greyscale will work - and then apply that to the image. To do this you need to store the handle of the image

% make random image
im = rand(100,100);
% make example alphamask
alphamask = im<0.3;
% store handle
hnd = imagesc(im);
% apply mask
set(hnd, 'AlphaData', alphamask);
Kawasaki answered 1/5, 2015 at 15:57 Comment(6)
Nice answer. I was just writing the same :-)Donte
Very nice, thank you. Now I only have to figure out how to export this nice figure into a pdf, because export_fig doesn't support alpha as far as I know.Workday
Thanks @LuisMendo : ) It's the best way!Kawasaki
@Workday I just export to png, I think you might have to use an external tool to convert to pdf (if you're on linux convert should do the job: askubuntu.com/questions/158093/…)Kawasaki
I'm on linux, but my typical workflow normally looks like this: matlab figure saved as pdf -> Inkscape saved as pdf + latex -> import it into latex. But maybe should think about simplifying this process anyway.Workday
I always used to use eps for my latex workflow but I never had to use images there let alone transparency! You could embed the convert call inside your matlab script using system?Kawasaki

© 2022 - 2024 — McMap. All rights reserved.