I am new to matlab and trying to plot some regression lines but make them transparent. I read here:https://uk.mathworks.com/matlabcentral/answers/44442-change-opacity-of-lines that it should be possible to do this by specifying the alpha as the final value in the colour, however, this does not change anything for me. Any ideas how I can make my lines transparent? I am running version R2021a.
plot(MC_age(:,i),f(i,:),'LineWidth',0.1,"Color", [0.4, 0.4, 0.4, 0.8])
Thanks
Sorry I don't have 2021a, but I have tested the following code on R2020b and R2017a
x = 1:10;
y1 = rand(size(x));
y2 = rand(size(x));
y3 = rand(size(x));
figure;hold all;
plot(x,y1,'LineWidth',10,"Color", [0.4, 0.4, 0.4, 1])
plot(x,y2,'LineWidth',10,"Color", [0.4, 0.4, 0.4, 0.5])
plot(x,y3,'LineWidth',10,"Color", [0.4, 0.4, 0.4, 0.2])
and it works as you would like, but unfortunately it seems that others have experienced a problem doing the same thing with 2021a.
There is this function called patchline(), which essentially draws your plots as patch objects instead. Downloading it, then using the following code (in the same directory where patchline was saved)
x = 1:10;
y1 = rand(size(x));
y2 = rand(size(x));
y3 = rand(size(x));
figure;hold all;
p1 = patchline(x,y1,'edgecolor',[0.4 0.4 0.4],'linewidth',2,'edgealpha',1);
p2 = patchline(x,y2,'edgecolor',[0.4 0.4 0.4],'linewidth',2,'edgealpha',0.5);
p3 = patchline(x,y3,'edgecolor',[0.4 0.4 0.4],'linewidth',2,'edgealpha',0.2);
gives me the following image
I have also tested this on 2017a and 2020b... does that work for you on 2021a?
Related
I would like to plot a function in Matlab with a shaded area indicating the uncertainty over it (e.g., confidence interval). This can be achieved by using the fill function to create a color patch. For example
x = linspace(0, 2*pi, 100);
f = cos(x);
fUp = cos(x) + 1;
fLow = cos(x) - 1;
x2 = [x, fliplr(x)];
plot(x, f, 'k')
hold on
fill(x2, [f, fliplr(fUp)], 0.7 * ones(1, 3), 'linestyle', 'none', 'facealpha', 0.4);
fill(x2, [fLow, fliplr(f)], 0.7 * ones(1, 3), 'linestyle', 'none', 'facealpha', 0.4);
This creates a shaded gray area between the functions fLow and fUp, with f in the middle represented as a solid black line, as in the picture below.
I would like now to have the shaded area degrade its color when we approach the lower (resp. upper) bound of the confidence interval. In particular, I would like that while approaching its boundaries, the shaded area gets brighter and brighter. Is there a way to do it?
I'm doing two separate patches because I think it may be necessary for my purpose.
You can split your CI into n subarea:
x = linspace(0, 2*pi, 100);
f = cos(x);
n = 20; % step number
g = 0.3; % grayscale intensity
fUp = cos(x) + linspace(0,1,n).';
fLow = cos(x) - linspace(0,1,n).';
x2 = [x, fliplr(x)];
plot(x, f, 'k')
hold on
fill(x2, [repmat(f,n,1), fliplr(fUp)], g * ones(1, 3), 'linestyle', 'none', 'facealpha', [1/n]);
fill(x2, [fLow, repmat(fliplr(f),n,1)], g * ones(1, 3), 'linestyle', 'none', 'facealpha', [1/n]);
Which produce:
The subarea are overlapping and produce a maximum facealpha of n*(1/n) * g = g
Noticed that this method is not really memory efficient (since it produce n subarea on each side) and will only works with a linear shading.
If your CI is non linear then you should adjust this part:
% Prediction Linear CI
% ↓ ↓
cos(x) + linspace(0,1,n).';
cos(x) - linspace(0,1,n).';
to
% Prediction Non linear CI
% ↓ ↓
cos(x) + your_non_linear_CI_distribution;
cos(x) - your_non_linear_CI_distribution;
I am now having trouble plotting something by matlab.
x1 = 1:2500;
y1 = 1:2500:2500^2;
y2 = 1:2400:2500*2400;
figure
subplot(2,2,1);
semilogy(x1, y1, '-', x1, y2, '-.');
set(gca,'xticklabel',{[]});
pp = subplot(2,2,2);
pospp = get(pp, 'Position');
ax3 = axes('Position',pospp);
ax4 = axes('Position',[0.7 0.7 0.1 0.1]);
axes(ax3);
semilogy(x1, y1, '-', x1, y2, '-.');
set(gca,'xticklabel',{[]});
axes(ax4);
semilogy(2000:2500, y1(2000:2500), '-', 2000:2500, y2(2000:2500), '-.');
set(ax3,'xticklabel',{[]});
set(ax4,'xticklabel',{[]});
Here is the figure
I am wondering how can I remove one y axis from the right subplot? The one with "0, 0.2, 0.4, 0.6, 0.8, 1.0"? I only want to keep the log-scale one.
Thank you.
In this line:
ax3 = axes('Position',pospp);
you create an extra axes object. Remove this line and the axes will be gone. Use pp instead of ax3.
I was trying to produce the following figure:
as similar as possible. I was having difficulties because I wasn't sure how to plot the unit circle. How does one do that? I tried using the polar function but I couldn't then make it also plot the blue crosses and red crosses. Anyone has an idea?
Edited answer
Here is a part of the code:
% --- Plot everything on the same axes
hold on
% --- Plot the unit circle:
theta = linspace(0, 2*pi, 360);
plot(cos(theta), sin(theta), 'k--');
% --- Plot the blue points
x = [0.2 0.4 0.4 0.8];
y = [0.4 0.2 0.8 0.4];
scatter(x, y, 'bs');
% --- Plot the red points:
x = [0.4 0.8];
y = [0.4 0.8];
scatter(x, y, 'ro');
There are still many modifications to do to get the final plots, but at least this is a starting point.
Second edit
To answer your question about the intersection of a line and a circle, you have to start with the maths behind. Line and circle are defined by the following equations:
y = a.x + b % Cartesian definition of a line
(x-x0)² + (y-y0)² = r² % Cartesian definition of a circle
If you combine the two, you realize that finding the intersection is similar to finding the solution of:
(a²+1).x² + 2(a(b-y0)-x0).x + x0²+(b-y0)²-r² = 0
i.e. the roots of a polynom. As this is a trinom, there are 3 possibilities: 0 solution (no intersection), 1 solution (line is tangent to the circle) and 2 solutions (line crossing the circle).
So, in practice you have to:
Get the parameters a, b, x0, y0 and r of your problem
Find the roots of the polynom (for instance, with the function roots)
Decide what to do based on the number of roots.
Hope this helps,
%% Construct polar grid (put inside another function)
range = 0.2:0.2:1;
n = 50;
for i=1:length(range)
ro = ones(1,n);
ro = ro.*range(i);
th = linspace(0,pi*2,n);
[xx,yy] = pol2cart(th,ro);
plot(xx,yy, 'color',[.9 .9 .9]), grid on, hold on
n = round(n * 1.5);
end
%% Plot like normal
x1 = [.2 .4 .4 .8];
y1 = [.4 .2 .8 .4];
x2 = [.4 .8];
y2 = [.4 .8];
plot(x1,y1, 'bx',...
x2,y2, 'ro');
xlim([-.05 1]);
ylim([-.05 1]);
I am very new to MATLAB and expect a step-by-step solution. I have data, series(y), which I have to plot against (x). Also I have the standard deviation values for each data point of (y). Now I have to plot these series highlighting the error bars. How can I do that?
The data is in a text file sorted in columns as:
X = -50, -49, -48, -47....0....1, 2, 3, 4, 5....till 50
Y = 1.2, 1.0, 1.1, 1.9, 1.3.....
Standard deviation = 0.6, 0.5, 0.3, 0.6, 0.6.....
Also, how do I control the ticks and appearance property for these kinds of graphs?
x = 1:0.1:10;
y = sin(x);
e = 0.1 * randn(length(x), 1);
errorbar(x,y,e)
set(gca, 'Xlim', [4 10])
set(gca, 'XTick', 4:2:10)
See also get(gca) and get(gcf) for other properties to change.
For help on any of these functions, do, for example, help errorbar.
x = [0.35, 0.65, 0.8, 1]
y1 = [0.1, 0.21, 0.29, 0.35]
y2 = [0.11, 0.26, 0.28, 0.39]
y3 = [0.1, 0.2, 0.28, 0.36]
y4 = [0.1, 0.25, 0.31, 0.37]
I need to plot all this data on the same graph, such that there is one x-axis and two y-axes, one on the left and one on the right. The interval for y-axes is 0.1:0.1:0.4, and that for x-axis is 0:0.1:1.
I have tried plotyy:
[ax, h1, h2] = plotyy(x, y2, x, y4);
hold on
[bx, h3, h4] = plotyy(x, y3, x, y1);
but one of the vectors does not plot on the same graph. When you run this, and use the PAN option on the graph, you'll realize that only 3 curves are plotted while the 4th curve is plotted on another figure. When I check the legend U get only 3 curves, and not 4.
How about something like this:
plot(x, [y1(:) y2(:) y3(:) y4(:)])
legend({'y1' 'y2' 'y3' 'y4'}, 'Location','NorthWest')
set(gca, 'XLim',[0 1], 'YLim',[0.1 0.4])
Skip the hold and use
[ax, h1, h2] = plotyy(x, [y2;y3], x, [y4;y1]);
Edit:
This is kind of a hack since it is does not support data sets were x differ. That is y2 and y3 should be specified for the same x. The same is true for y4 and y1.