How to set the opacity for a plot? - matlab

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.

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;
Even color gradients are nothing special anymore.

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:

Related

Matlab: patch area between two curves which depend on the curves values

I'm trying to fill an area between two curves with respect to a function which depends on the values of the curves.
Here is the code of what I've managed to do so far
i=50;
cc = #(xx,x,y) 1./(1+(exp(-xx)/(exp(-x)-exp(-y))));
n_vec = 2:0.1:10;
x_vec = linspace(2,10,length(n_vec));
y_vec = abs(sin(n_vec));
N=[n_vec,fliplr(n_vec)];
X=[x_vec,fliplr(y_vec)];
figure(1)
subplot(2,1,1)
hold on
plot(n_vec,x_vec,n_vec,y_vec)
hp = patch(N,X,'b')
plot([n_vec(i) n_vec(i)],[x_vec(i),y_vec(i)],'linewidth',5)
xlabel('n'); ylabel('x')
subplot(2,1,2)
xx = linspace(y_vec(i),x_vec(i),100);
plot(xx,cc(xx,y_vec(i),x_vec(i)))
xlabel('x'); ylabel('c(x)')
This code produces the following graph
The color code which I've added represent the color coding that each line (along the y axis at a point on the x axis) from the area between the two curves should be.
Overall, the entire area should be filled with a gradient color which depends on the values of the curves.
I've assisted the following previous questions but could not resolve a solution
MATLAB fill area between lines
Patch circle by a color gradient
Filling between two curves, according to a colormap given by a function MATLAB
NOTE: there is no importance to the functional form of the curves, I would prefer an answer which refers to two general arrays which consist the curves.
The surf plot method
The same as the scatter plot method, i.e. generate a point grid.
y = [x_vec(:); y_vec(:)];
resolution = [500,500];
px = linspace(min(n_vec), max(n_vec), resolution(1));
py = linspace(min(y), max(y), resolution(2));
[px, py] = meshgrid(px, py);
Generate a logical array indicating whether the points are inside the polygon, but no need to extract the points:
in = inpolygon(px, py, N, X);
Generate Z. The value of Z indicates the color to use for the surface plot. Hence, it is generated using the your function cc.
pz = 1./(1+(exp(-py_)/(exp(-y_vec(i))-exp(-x_vec(i)))));
pz = repmat(pz',1,resolution(2));
Set Z values for points outside the area of interest to NaN so MATLAB won't plot them.
pz(~in) = nan;
Generate a bounded colourmap (delete if you want to use full colour range)
% generate colormap
c = jet(100);
[s,l] = bounds(pz,'all');
s = round(s*100);
l = round(l*100);
if s ~= 0
c(1:s,:) = [];
end
if l ~= 100
c(l:100,:) = [];
end
Finally, plot.
figure;
colormap(jet)
surf(px,py,pz,'edgecolor','none');
view(2) % x-y view
Feel free to turn the image arround to see how it looks like in the Z-dimention - beautiful :)
Full code to test:
i=50;
cc = #(xx,x,y) 1./(1+(exp(-xx)/(exp(-x)-exp(-y))));
n_vec = 2:0.1:10;
x_vec = linspace(2,10,length(n_vec));
y_vec = abs(sin(n_vec));
% generate grid
y = [x_vec(:); y_vec(:)];
resolution = [500,500];
px_ = linspace(min(n_vec), max(n_vec), resolution(1));
py_ = linspace(min(y), max(y), resolution(2));
[px, py] = meshgrid(px_, py_);
% extract points
in = inpolygon(px, py, N, X);
% generate z
pz = 1./(1+(exp(-py_)/(exp(-y_vec(i))-exp(-x_vec(i)))));
pz = repmat(pz',1,resolution(2));
pz(~in) = nan;
% generate colormap
c = jet(100);
[s,l] = bounds(pz,'all');
s = round(s*100);
l = round(l*100);
if s ~= 0
c(1:s,:) = [];
end
if l ~= 100
c(l:100,:) = [];
end
% plot
figure;
colormap(c)
surf(px,py,pz,'edgecolor','none');
view(2)
You can use imagesc and meshgrids. See comments in the code to understand what's going on.
Downsample your data
% your initial upper and lower boundaries
n_vec_long = linspace(2,10,1000000);
f_ub_vec_long = linspace(2, 10, length(n_vec_long));
f_lb_vec_long = abs(sin(n_vec_long));
% downsample
n_vec = linspace(n_vec_long(1), n_vec_long(end), 1000); % for example, only 1000 points
% get upper and lower boundary values for n_vec
f_ub_vec = interp1(n_vec_long, f_ub_vec_long, n_vec);
f_lb_vec = interp1(n_vec_long, f_lb_vec_long, n_vec);
% x_vec for the color function
x_vec = 0:0.01:10;
Plot the data
% create a 2D matrix with N and X position
[N, X] = meshgrid(n_vec, x_vec);
% evaluate the upper and lower boundary functions at n_vec
% can be any function at n you want (not tested for crossing boundaries though...)
f_ub_vec = linspace(2, 10, length(n_vec));
f_lb_vec = abs(sin(n_vec));
% make these row vectors into matrices, to create a boolean mask
F_UB = repmat(f_ub_vec, [size(N, 1) 1]);
F_LB = repmat(f_lb_vec, [size(N, 1) 1]);
% create a mask based on the upper and lower boundary functions
mask = true(size(N));
mask(X > F_UB | X < F_LB) = false;
% create data matrix
Z = NaN(size(N));
% create function that evaluates the color profile for each defined value
% in the vectors with the lower and upper bounds
zc = #(X, ub, lb) 1 ./ (1 + (exp(-X) ./ (exp(-ub) - exp(-lb))));
CData = zc(X, f_lb_vec, f_ub_vec); % create the c(x) at all X
% put the CData in Z, but only between the lower and upper bound.
Z(mask) = CData(mask);
% normalize Z along 1st dim
Z = normalize(Z, 1, 'range'); % get all values between 0 and 1 for colorbar
% draw a figure!
figure(1); clf;
ax = axes; % create some axes
sc = imagesc(ax, n_vec, x_vec, Z); % plot the data
ax.YDir = 'normal' % set the YDir to normal again, imagesc reverses it by default;
xlabel('n')
ylabel('x')
This already looks kinda like what you want, but let's get rid of the blue area outside the boundaries. This can be done by creating an 'alpha mask', i.e. set the alpha value for all pixels outside the previously defined mask to 0:
figure(2); clf;
ax = axes; % create some axes
hold on;
sc = imagesc(ax, n_vec, x_vec, Z); % plot the data
ax.YDir = 'normal' % set the YDir to normal again, imagesc reverses it by default;
% set a colormap
colormap(flip(hsv(100)))
% set alpha for points outside mask
Calpha = ones(size(N));
Calpha(~mask) = 0;
sc.AlphaData = Calpha;
% plot the other lines
plot(n_vec, f_ub_vec, 'k', n_vec, f_lb_vec, 'k' ,'linewidth', 1)
% set axis limits
xlim([min(n_vec), max(n_vec)])
ylim([min(x_vec), max(x_vec)])
there is no importance to the functional form of the curves, I would prefer an answer which refers to two general arrays which consist the curves.
It is difficult to achieve this using patch.
However, you may use scatter plots to "fill" the area with coloured dots. Alternatively, and probably better, use surf plot and generate z coordinates using your cc function (See my seperate solution).
The scatter plot method
First, make a grid of points (resolution 500*500) inside the rectangular space bounding the two curves.
y = [x_vec(:); y_vec(:)];
resolution = [500,500];
px = linspace(min(n_vec), max(n_vec), resolution(1));
py = linspace(min(y), max(y), resolution(2));
[px, py] = meshgrid(px, py);
figure;
scatter(px(:), py(:), 1, 'r');
The not-interesting figure of the point grid:
Next, extract the points inside the polygon defined by the two curves.
in = inpolygon(px, py, N, X);
px = px(in);
py = py(in);
hold on;
scatter(px, py, 1, 'k');
Black points are inside the area:
Finally, create color and plot the nice looking gradient colour figure.
% create color for the points
cid = 1./(1+(exp(-py)/(exp(-y_vec(i))-exp(-x_vec(i)))));
c = jet(101);
c = c(round(cid*100)+1,:); % +1 to avoid zero indexing
% plot
figure;
scatter(px,py,16,c,'filled','s'); % use size 16, filled square markers.
Note that you may need a fairly dense grid of points to make sure the white background won't show up. You may also change the point size to a bigger value (won't impact performance).
Of cause, you may use patch to replace scatter but you will need to work out the vertices and face ids, then you may patch each faces separately with patch('Faces',F,'Vertices',V). Using patch this way may impact performance.
Complete code to test:
i=50;
cc = #(xx,x,y) 1./(1+(exp(-xx)/(exp(-x)-exp(-y))));
n_vec = 2:0.1:10;
x_vec = linspace(2,10,length(n_vec));
y_vec = abs(sin(n_vec));
% generate point grid
y = [x_vec(:); y_vec(:)];
resolution = [500,500];
px_ = linspace(min(n_vec), max(n_vec), resolution(1));
py_ = linspace(min(y), max(y), resolution(2));
[px, py] = meshgrid(px_, py_);
% extract points
in = inpolygon(px, py, N, X);
px = px(in);
py = py(in);
% generate color
cid = 1./(1+(exp(-py)/(exp(-y_vec(i))-exp(-x_vec(i)))));
c = jet(101);
c = c(round(cid*100)+1,:); % +1 to avoid zero indexing
% plot
figure;
scatter(px,py,16,c,'filled','s');

Matlab: Indicate on a plot when data is off the plot?

Is there a way to have a mark on a plot axis for any data points off the current plot in Matlab? It is great if the method works with zoom and pan, but I can live without it.
I have several data sets that I am plotting using subplots. Each plot contains 20 data points, similar to marking where darts landed on a dart board. For most data sets, these plots are within a standard range, ie the size of the dart board, so I am using this standard for the axis range of each plot to make the subplots easily visually comparable. However, a few data sets have an outlier that is outside this standard range (typically way outside). I do not want to change the axis to show the outlier, but I want the user to be aware that there is 1 or more data points off the plot, and preferably in which direction they are off.
I thought a bit about trying to use the axis markers (set(gca, 'Ytick', xLowerLimit: markSpacing: xUpperLimit)) and adding an extra marker in a different color to indicate the location of an outlier, but I couldn't see how to do this without disrupting the regular markers and in a automated way that could allow for multiple outlier marks.
One approach is to see if there is any data that exceed your Y Axis limit and then place some text somewhere to notify the user. Options include text (puts text on axis) or uicontrol (puts text somewhere in figure window).
Here is an example using uicontrol:
% Set up figure window and axes
h.f = figure;
h.ax = axes('Units', 'Normalized', 'Position', [0.13 0.2 0.75 0.75]);
% Plot some sample data and window our axes to replicate the question
x = 1:10;
y = [1:9 20];
xmin = 0; xmax = 10;
ymin = 0; ymax = 10;
plot(h.ax, x, y);
axis(h.ax, [xmin xmax ymin ymax]);
% Generate logical matrix to find if any y data is beyond our axis limit
ymask = y > ymax;
if any(ymask) % Loop triggers if any y value is greater than ymax
str = sprintf('There are %u data points greater than current y axis limit', sum(ymask));
uicontrol('Parent', h.f, ...
'Style', 'text', ...
'Units', 'Normalized', ...
'Position', [0.01 0.01 0.9 0.05], ...
'String', str ...
);
end
Which generates the following (annotations added manually):
This can be extended to other directions with some simple copy + paste and tweaks.
Edit: see bottom for improved method using callbacks.
You can find data points exceeding the axes limits and draw them on the limits, using different symbols:
A = randn(20, 2);
x_range = [-1.5, 1.5];
y_range = [-1.5, 1.5];
figure(1);
clf;
ah = axes;
plot(ah, A(:,1), A(:,2), 'o')
hold on
set(ah, 'XLim', x_range, 'YLim', y_range)
x_lt = A(:,1) < x_range(1);
x_gt = A(:,1) > x_range(2);
y_lt = A(:,2) < y_range(1);
y_gt = A(:,2) > y_range(2);
A_out = A;
A_out(x_lt, 1) = x_range(1);
A_out(x_gt, 1) = x_range(2);
A_out(y_lt, 2) = y_range(1);
A_out(y_gt, 2) = y_range(2);
A_out = A_out(x_lt | x_gt | y_lt | y_gt, :);
plot(ah, A_out(:,1), A_out(:,2), 'rx')
This produces a plot like this:
Edit: add callbacks
If you really want to go fancy, you can add callbacks so that outliers are automatically drawn (and removed) when the figure is zoomed. Adding the same functionality for panning is left as an exercise to the reader ;)
mark_outliers.m:
function mark_outliers(fig, ax)
ah = ax.Axes;
lobj = findobj(ah, 'Type', 'Line');
x_range = ah.XLim;
y_range = ah.YLim;
ah.NextPlot = 'add';
for i_l = 1:numel(lobj)
xd = lobj(i_l).XData;
yd = lobj(i_l).YData;
x_lt = xd < x_range(1);
x_gt = xd > x_range(2);
y_lt = yd < y_range(1);
y_gt = yd > y_range(2);
outliers = x_lt | x_gt | y_lt | y_gt;
if any(outliers)
xd_out = xd;
xd_out(x_lt) = x_range(1);
xd_out(x_gt) = x_range(2);
yd_out = yd;
yd_out(y_lt) = y_range(1);
yd_out(y_gt) = y_range(2);
xd_out = xd_out(outliers);
yd_out = yd_out(outliers);
plot(ah, xd_out, yd_out, 'xr', 'Tag', 'Outliers')
end
end
delete_outliers.m:
function delete_outliers(fig, ah)
ah = ah.Axes;
delete(findobj(ah, 'Tag', 'Outliers'));
example.m:
A = randn(20, 2);
fh = figure(1);
clf;
ah = axes;
plot(ah, A(:,1), A(:,2), 'o')
h = zoom(fh);
h.ActionPreCallback=#delete_outliers;
h.ActionPostCallback=#mark_outliers;

Nicer errorbars when multiple data is shown

So I need to plot some errobar plots in a figure. Specifically I need 4 errorbar plots in each figure, the problem is that the figure gets a bit unreadable when several data is plotted.
Example:
clear all
close all
clc
x = 0:pi/10:pi;
y = sin(x);
y2=cos(x);
y3=atan(x);
e = std(y)*ones(size(x));
e2 = std(y2)*ones(size(x));
e3 = std(y3)*ones(size(x));
figure
hold on
errorbar(x,y,e)
errorbar(x,y2,e2)
errorbar(x,y3,e3)
My idea to solve the problem is to fill the area that the corners of the errorbars delimit with the same color of the plot and low alpha, so the overlapping of the areas is visible.
The problem is that the only way I can imagine of doing this is to create a mesh in the area delimited by the errorbar corners and then fill them with patch. This is indeed possible, but quite annoying, as a plot will not have a convex hull, therefore I will need to iteratively go creating the triangles one by one. So the question is : Is there a more elegant way of doing this?
Additionally, I am open to suggestions of a better way of visualizing this data, if anyone has.
Approach 1
Plot the graphs normally, and then plot the errorbars manually using patches. The data for the patches (coordinates and color) is taken from the plotted graphs, and the alpha of the patch can be set to any desired value.
clear all
close all
clc
error_alpha = .4;
error_width_factor = .01;
x = 0:pi/10:pi;
y = sin(x);
y2 = cos(x);
y3 = atan(x);
e = std(y)*ones(size(x));
e2 = std(y2)*ones(size(x));
e3 = std(y3)*ones(size(x));
ee = [e; e2; e3];
figure
hold on
hp(1) = plot(x,y);
hp(2) = plot(x,y2);
hp(3) = plot(x,y3);
w = diff(xlim)*error_width_factor;
for m = 1:numel(hp)
for n = 1:numel(hp(m).XData)
patch(hp(m).XData(n)+[-1 1 1 -1]*w, hp(m).YData(n)+[-1 -1 1 1]*ee(m,n), 'w',...
'FaceColor', hp(m).Color, 'FaceAlpha', error_alpha, 'EdgeColor', 'none');
end
end
Approach 2
Similar as before, but use narrower patches and plot them with a graph-dependent horizontal shift (as suggested by #Patrik). Applying an alpha value helps keep the figure lighter.
The code is a modified version of that of approach 1. The example shown here contains 101 data values, and is still rather visible.
clear all
close all
clc
error_alpha = .4;
error_width_factor = .003;
x = 0:pi/50:pi;
y = sin(x);
y2 = cos(x);
y3 = atan(x);
e = std(y)*ones(size(x));
e2 = std(y2)*ones(size(x));
e3 = std(y3)*ones(size(x));
ee = [e; e2; e3];
figure
hold on
hp(1) = plot(x,y);
hp(2) = plot(x,y2);
hp(3) = plot(x,y3);
w = diff(xlim)*error_width_factor;
m0 = (numel(hp)+1)/2;
for m = 1:numel(hp)
for n = 1:numel(hp(m).XData)
patch(hp(m).XData(n)+[-1 1 1 -1]*w+w*(m-m0),...
hp(m).YData(n)+[-1 -1 1 1]*ee(m,n), 'w', 'FaceColor', hp(m).Color, ...
'FaceAlpha', error_alpha, 'EdgeColor', 'none');
end
end

Change color of each point in scatter plot sequentially

I am new to Matlab, I was trying to you scatter plot to plot 4 points in an axes.
for example
x = [0;0;1;-1];
y = [1;-1;0;0];
scatter(x,y);
what I wanted to do was to change the color of one coordinate in the above plot continuously in clock wise direction
Like the above pic.
If not is there another way I can do so?
Thanks in advance.
You can add a 4th argument to scatter in order to set the color (the 3rd argument sets the size, you can leave it empty):
col = lines(4); % create 4 colors using the 'lines' colormap
scatter(x,y,[],col);
You can use some other colormaps (type doc colormap in Matlab for more details), or just enter some vector of numbers to use the current colormap.
Edit I've just realized you wanted to change the color of only one point; you can do it with (for example) col = [2 1 1 1].
You need to plot each point separately, get a handle to each, and then change their 'color' property sequentially in a loop:
%// Data
x = [-1;0;1;0]; %// define in desired (counterclockwise) order
y = [0;1;0;-1];
color1 = 'g';
color2 = 'r';
%// Initial plot
N = numel(x);
h = NaN(1,N);
hold on
for n = 1:N
h(n) = plot(x(n), y(n), 'o', 'color', color1);
end
axis([-1.2 1.2 -1.2 1.2]) %// set as desired
%// Change color of one point at a time, and restore the rest
k = 0;
while true
k = k+1;
pause(.5)
n = mod(k-1,N)+1;
set(h(n), 'color', color2);
set(h([1:n-1 n+1:end]), 'color', color1);
end

quiver not drawing arrows just lots of blue, matlab

Can somebody tell me what I am doing wrong with the quiver plotting function when I don't really get arrows, it just fills the empty space with lots of blue.Look at the image below and then look at my code.
This is just a part of my contour since this eats up proccessing power if I try to draw it larger. But my function, the contours and everything else works, it's just the quiver I'm having trouble with.
interval = -100:100;
[X Y] = meshgrid(interval, interval);
h = figure;
contour(X, Y, Z);
hold on;
[FX,FY] = gradient(-Z);
quiver(X, Y, FX, FY);
hold off;
If I make my matrix more sparse, e.g. with "interval = linspace(-800, 1600, 1200);" the result will look like this:
EDIT:
What I need are contour lines like that, but the arrows should flow with them. Right now these just look like dots, even if I zoom in further. If I zoom out the entire window will be blue.
Here is the script in its entirety if anyone wants to play with it to figure this out.
m1 = 1;
m2 = 0.4;
r1 = [1167 0 0];
r2 = [-467 0 0];
G = 9.82;
w = sqrt( G*(m1+m2) / norm(r1-r2)^3 );
interval = linspace(-800, 1600, 1200);
% Element-wise 2-norm
ewnorm = #(x,y) ( x.^2 + y.^2 ).^(1/2);
% Element-wise cross squared
ewcross2 = #(w,x,y) w^2.*( x.*x + y.*y );
[X Y] = meshgrid(interval, interval);
Z = - G*m1 ./ ewnorm( X-r1(1), Y-r1(2) ) - G*m2 ./ ewnorm( X-r2(1), Y-r2(2) ) - 1/2*ewcross2(w,X,Y);
h = figure;
contour(Z);
daspect([1 1 1]);
saveas(h, 'star1', 'eps');
hold on;
[FX,FY] = gradient(-Z);
quiver(X, Y, FX,FY);
hold off;
The problem is that the mesh is too dense. You only want to have as few elements as necessary to generate a useful mesh. As such, try reducing the density of the mesh:
interval = -100:2:100
If you're going to be changing the limits often, you probably want to avoid using the X:Y:Z formulation. Use the linspace function instead:
interval = linspace(-100,100,10);
This will ensure that no matter what your limits, your mesh will be 10x10. In the comment below, you mention that the arrows are appearing as dots when you use a very large mesh. This is to be expected. The arrows reflect "velocity" at a given point. When your plot is scaled out to a very large degree, then the velocity at any given point on the plot will be almost 0, hence the very small arrows. Check out the quiver plot documentation, as well as the quivergroup properties, to see more details.
If you absolutely must see arrows at a large scale, you can try setting the AutoScale property to off, or increasing the AutoScaleFactor:
quiver(X, Y, FX, FY, 'AutoScale', 'off');
quiver(X, Y, FX, FY, 'AutoScaleFactor', 10);
You may also want to play with the MarkerSize and MaxHeadSize properties. I really just suggest looking at all the QuiverGroup properties and trying things out.
You could use a threshold
interval = -100:100;
[X Y] = meshgrid(interval, interval);
h = figure;
contour(X, Y, Z);
hold on;
[FX,FY] = gradient(-Z);
GM = sqrt(FX.^2 + FY.^2);
threshold = 0.1;
mask = GM > threshold;
quiver(X(mask), Y(mask), FX(mask), FY(mask));
hold off;
This will show only vectors with a magnitude > 0.1;