Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the opacity for a plot?

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.

like image 617
CroCo Avatar asked Aug 17 '15 18:08

CroCo


People also ask

How do you change the opacity of a plot in Python?

Matplotlib allows you to regulate the transparency of a graph plot using the alpha attribute. By default, alpha=1. If you would like to form the graph plot more transparent, then you'll make alpha but 1, such as 0.5 or 0.25.

How do you make a surface plot transparent?

There is a property called alpha. After the surf command just say alpha(0.5). The value lies between 0 and 1. The closer to zero the more transparent it is.

How do I change the transparency of a plot in Matlab?

Description. alpha value sets the face transparency for objects in the current axes that support transparency. Specify value as 'clear' or 'opaque' , or as a number in the range [0, 1]. A value of 0 makes the objects transparent, and value of 1 makes the objects fully opaque.

Which parameter should you use to change a plots transparency?

In order to change the transparency of a graph plot in matplotlib we will use the matplotlib. pyplot. plot() function.


2 Answers

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.

like image 139
Robert Seifert Avatar answered Oct 12 '22 03:10

Robert Seifert


You can use the patchline submission from the File Exchange, in which you can manipulate line objects as if they were patch objects; i.e. assign them transparency (alpha) values.

Here is some sample code using the function:

clc;clear;close all

n = 10;
x = 1:n;

y1 = rand(1,n);
y2 = rand(1,n);
y3 = rand(1,n);

Y = [y1;y2;y3];

linestyles = {'-';'-';'--'};
colors = {'r';'k';'b'};
alphavalues = [.2 .5 .8];

hold on
for k = 1:3
    patchline(x,Y(k,:),'linestyle',linestyles{k},'edgecolor',colors{k},'linewidth',4,'edgealpha',alphavalues(k))
end

and output:

enter image description here

like image 28
Benoit_11 Avatar answered Oct 12 '22 02:10

Benoit_11