Multiple plots on same figure - matlab

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.

Related

Change line transparency in matlab

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?

Matlab: how to remove extra y axis generated by 'axes'?

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.

How to set background in multiple colors in semilogy plot (MATLAB) ?

I am looking for a way to shade the background of my semilog plot in two colors.
For example, in the following image, I have am plotting three polynomials and they all are equal at x=1. I want one rectangle for x<1 region and other for x>1 region. How can I insert two such rectangles, of different colors, in background to highlight these two regions.
MWE:
x = 0.1:0.1:10;
y1 = polyval([1, 0], x); % Evaluate y = x;
y2 = polyval([1, 0, 0], x); % Evaluate y = x^2;
y3 = polyval([1, 0, 0, 0], x); % Evaluate y = x^3;
figure
semilogy(x, y1, '.k', x, y2, '.b', x, y3, '.r'); title ('Three
polynomials on a semilog y scale') xlabel('x'); ylabel('y');
legend({'y= x', 'y = x^2', 'y = x^3'}, 'Location', 'Northwest')
You can solve that using area or patch.
As pointed by #SardarUsama, there are others questions with good examples on it, however, you need to avoid to have any zeros in the area data, otherwise it will fail.
Follows the code setting one area only.
x = 0.1:0.1:10;
y1 = polyval([1, 0], x); % Evaluate y = x;
y2 = polyval([1, 0, 0], x); % Evaluate y = x^2;
y3 = polyval([1, 0, 0, 0], x); % Evaluate y = x^3;
figure
plot(x, y1, '.k', x, y2, '.b', x, y3, '.r'); %MODIFIED
hold on %ADDED
title ('Three polynomials on a semilog y scale')
set (gca, 'Yscale', 'log'); %ADDED
xlabel('x');
ylabel('y');
legend({'y= x', 'y = x^2', 'y = x^3'}, 'Location', 'Northwest')
area( [1 1 10 10],[1e-3 1e+3 1e+3 1e-3 ],'FaceColor','green','facealpha',0.3) %ADDED
The code above works for matlab after 2014b. If you have one before that, you can use the patch function (which requires some small change in the data, but uses the Facealpha option) or you can move the area to the background as i do below:
ax=get(gca,'Children'); %ADDED
set(gca,'Children',[ax(2) ax(3) ax(4) ax(1)]); %ADDED, move area to background
Note: Indeed, I missed the problem with the legend. I correct as mentioned, however for me the area was on top of the other graphs. To solve it i changed the order of the plots. If the area was with transparency, this will not be an issue.

Drawing 3D points in Matlab and connect them in order via line

I have an array that contains 3D float points. Not only I want to depict them in a figure but also I want to connect them with lines.
Example) lets say we have array called X:
X=[0, 0, 0; 0.48, -0.88, 0.09; -1.06, 0.55, 0.9; -0.65, 1.5, -1.44; 1.1, 0.59,
-1.11;0.76, 0.86, -0.52; -1.08, -0.28, 0.55; 1.47, -1.21, 0.14; 1.42, -2.15, 0.71; -0.64,
1.87, 2.4;2.32, -2.44, 2.02; 2.25, -2.56, -3.03; 2.35, 2.65, -1.5; 0.23, -2.25, 2.78; 2.47,
-3.12, -1.91; 2.27, 1.37, -3.05; 2.3, 1.9, -1.29; -1.77, -0.51, 2.33];
X1= [0,0,0]
X2=[0.48, -0.88, 0.09]
X3=[-1.06, 0.55, 0.9] ...
now I want that X1...Xn to be drawn in figure as points then X1 get connected to X2, X2 get connected to X3, X3 get connected to X4, etc
how could I do that?
Here is what I ve done but I get a wrong figure:
figure;hold on;
P=[];
for i=1:size(X,1)
x=X(i,1);
y=X(i,2);
z=X(i,3);
A=[ x,y,z];
P=vertcat(P,A);
plot(P);
end
And Here is the output:
Check out the plot3 documentation.
In particular, plot3(X, Y, Z) will plot the points and join them with a line.
In your case:
plot3(X(:, 1), X(:, 2), X(:, 3))

yticks does not show correctly when mixing plotyy and semilogy in matlab

I am plotting two curves in different axes in the same figure with plotyy. The first curve ranges from 10^-4 to 10^-1 and the second curve ranges from 0 to 10. If I plot in the following way,
[AX, H1, H2] = plotyy(x, y1, x, y2, 'semilogy', 'semilogy');
They will both plotted as semilogy and with correct scale in y. But I don't want to show y2 in log10 scale, so I change
[AX, H1, H2] = plotyy(x, y1, x, y2, 'semilogy', 'plot');
However, then on left and right y axis, the tick only show the min and max range, all detail inbetween gone. Why's that?
You can try this:
[AX, H1, H2] = plotyy(x, y1, x, y2, 'semilogy', 'plot');
% set yticks for the left axis
set(AX(1), 'ytick', yourDesiredYticks1)
set(AX(1), 'box', 'off') % to remove corresponding yticks on the right side of the plot
% set yticks for the right axis
set(AX(2), 'ytick', yourDesiredYticks2)
set(AX(2), 'box', 'off')
Try this:
%# create some data resembling what you described
x = 1:100;
y1 = rand(size(x))*1e-1 + 1e-4;
y2 = rand(size(x))*10;
%# plot
hAx = plotyy(x,y1, x,y2, 'semilogy', 'semilogy');
set(hAx(2), 'YScale','linear')