How to set the opacity for a plot?

CroCo picture CroCo · Aug 17, 2015 · Viewed 14.6k times · Source

I have some data to be plotted in one figure. Noise data is ruining other data. How can I change the transparency level of a given data? In my case, I'm using hold all command for plotting several data. One of the solution is to change the LineWidth but I couldn't find a way for transparency option. I've tried alpha as follows

plot( noise_x, 'k', 'LineWidth', 1, 'alpha', 0.2)

but with no luck.

Answer

thewaywewalk picture thewaywewalk · Aug 17, 2015

With the introduction of the new graphic engine HG2 in Matlab R2014b, things got pretty easy. One just needs to dig a little.

The color property now contains a forth value for opacity/transparency/face-alpha, so that's all you need to change:

x = linspace(-10,10,100); y = x.^2;
p1 = plot(x,y,'LineWidth',5); hold on
p2 = plot(x,-y+y(1),'LineWidth',5);

% // forth value sets opacity
p1.Color(4) = 0.5;
p2.Color(4) = 0.5;

enter image description here

Even color gradients are nothing special anymore.