How to create multiple plots on the same graph in matlab - matlab

How I can plot the following graph in Matlab, and how to name av_d, av_c, ...
Thank you so much!

To get you started:
% Example data
x = 0:0.01:2*pi;
z = sin(x);
y = ones(size(x));
% Three identical lines plotted in x-z for different y
line([x;x;x;x]',[0*y;y;2*y;3*y]',[z;z;z;z]')
% Rename the yticks
set(gca,'YTickLabel',{'L1','L2','L3','L4'})
grid on
% Change the viewing angle
view(45,45)
Output:

Related

How to limit the amount of error bars Matlab

I have over 1 thousand data points on a graph. I would like to plot some error bars every 100 or so points data points.
x = 1:1500:1100;
y = [1:1200];
err = ?
plot(x.y);
hold on;
errorbar(x,y,err);
What value do I need for 'err' so I would only get 10 error bars?
As David pointed out in his comment, your code is no proper MATLAB code. Nevertheless, I presume you have sufficient MATLAB understanding: Basically you need to set up separate x and y values for your errorbar as well as the actual errors err, which should be plotted. Then, you can use the proper errorbar command.
Let's have a look at this small example:
x = -5:0.1:5;
y = sin(x);
xErr = linspace(-5, 5, 11); % Specify x locations for errorbar plot
yErr = sin(xErr); % The y values at these x locations
err = rand(1, 11); % The actual errors, here: some random values
plot(x, y, 'r'); % Plot
hold on;
errorbar(xErr, yErr, err, 'o'); % Actual errorbar plot at specific x locations
hold off;
You'll get an output like this:
Disclaimer: I made this with Octave 5.1.0, but the syntax should be identical to MATLAB. If not, please report any errors.
Hope that helps!

Edit the x limits of least squares line

I created two scatter plots and then used lsline to add regression lines for each plot. I used this code:
for i=1:2
x = ..;
y = ..;
scatter(x, y, 50, 'MarkerFaceColor',myColours(i, :));
end
h_lines = lsline;
However, the darker line extends far beyond the last data point in that scatter plot (which is at around x=0.3):
lsline doesn't seem to have properties that allow its horizontal range to be set. Is there a workaround to set this separately for the two lines, in Matlab 2016a?
For a single data set
This is a workaround rather than a solution. lsline internally calls refline, which plots a line filling the axis as given by their current limits (xlim and ylim). So you can change those limits to the extent you want for the line, call lsline, and then restore the limits.
Example:
x = randn(1,100);
y = 2*x + randn(1,100); % random, correlated data
plot(x, y, '.') % scatter plot
xlim([-1.5 1.5]) % desired limit for line
lsline % plot line
xlim auto % restore axis limit
For several data sets
In this case you can apply the same procedure for each data set sequentially, but you need to keep only one data set visible when you call lsline; otherwise when you call it to create the second line it will also create a new version of the first (with the wrong range).
Example:
x = randn(1,100); y = 2*x + randn(1,100); % random, correlated data
h = plot(x, y, 'b.'); % scatter plot
axis([min(x) max(x) min(y) max(y)]) % desired limit for line
lsline % plot line
xlim auto % restore axis limit
hold on
x = 2*randn(1,100) - 5; y = 1.2*x + randn(1,100) + 6; % random, correlated data
plot(x, y, 'r.') % scatter plot
axis([min(x) max(x) min(y) max(y)]) % desired limit for line
set(h, 'HandleVisibility', 'off'); % hide previous plot
lsline % plot line
set(h, 'HandleVisibility', 'on'); % restore visibility
xlim auto % restore axis limit
Yet another solution: implement your own hsline. It's easy!
In MATLAB, doing a least squares fit of a straight line is trivial. Given column vectors x and y with N elements, b = [ones(N,1),x] \ y; are the parameters to the best fit line. [1,x1;1,x2]*b are the y locations of two points along the line with x-coordinates x1 and x2. Thus you can write (following Luis' example, and getting the exact same output):
N = 100;
x = randn(N,1); y = 2*x + randn(N,1); % random, correlated data
h = plot(x, y, 'b.'); % scatter plot
hold on
b = [ones(N,1),x] \ y;
x = [min(x);max(x)];
plot(x,[ones(2,1),x] * b, 'b-')
x = 2*randn(N,1) - 5; y = 1.2*x + randn(N,1) + 6; % random, correlated data
plot(x, y, 'r.') % scatter plot
b = [ones(N,1),x] \ y;
x = [min(x);max(x)];
plot(x,[ones(2,1),x] * b, 'r-')
You can get the points that define the line using
h_lines =lsline;
h_lines(ii).XData and h_lines(ii).YData will contain 2 points that define the lines for each ii=1,2 line. Use those to create en equation of a line, and plot the line in the range you want.

Using roof of a graph as x-axis for another

I've plotted a graph using pcolor which gives me the following graph-
My aim is to use the roof of the graph (by roof) I mean the highest axis (in this case, the line which is defined by y=57) as the base for a further graph.
I was able to use hold on to generate the following-
Code for this (removed some parts that defined the axis labels etc for brevity)-
load sparsemap ;
load d ;
residues = 57 ;
z = zeros(residues,residues); % define the matrix
index = find(sparsemap(:,3) ~= 0) ;
values = length(index);
hold on
%Plots the map you see in the first photo-
for k = 1:values
z(sparsemap(index(k),1),sparsemap(index(k),2)) = sparsemap(index(k),3);
z(sparsemap(index(k),2),sparsemap(index(k),1)) = sparsemap(index(k),3);
end
%Plots the line plot at the bottom of the graph.
A = d(:,1);
B = d(:,2) ;
plot(A, B) ;
pcolor(1:residues,1:residues,z);
works = load('colormap_works');
colormap(works);
colorbar;
As you can see, the line plot is using the same x axis as the first graph.
I am trying to get the line plot to come on top of the figure. I imagine a final figure like so-
Any ideas as to how I can use the top part of the first graph?
You can use 2 subplots. Here is an example:
data = randi(50,20,20); % some data for the pcolor
y = mean(data); % some data for the top plot
subplot(5,1,2:5) % create a subplot on the lower 4/5 part for the figure
pcolor(data) % plot the data
colormap hot;
h = colorbar('east'); % place the colorbar on the right
h.Position(1) = 0.94; % 'push' the colorbar a little more to the right
ax = gca;
pax = ax.Position; % get the position for further thightning of the axes
ax.YTick(end) = []; % delete the highest y-axis tick so it won't interfere
% with the first tick of the top plot
subplot(5,1,1) % create a subplot on the upper 1/5 part for the figure
plot(1:20,y) % plot the top data
ylim([0 max(y)]) % compact the y-axis a little
ax = gca;
ax.XAxis.Visible = 'off'; % delete the x-axis from the top plot
ax.Position(2) = pax(2)+pax(4); % remove the space between the subplots
Which creates this:

Plotting a 3D graph of normalized prices in MatLab

I'm doing Gaussian processes and I calculated a regression per year from a given matrix where each row represents a year , so the code is:
M1 = MainMatrix; %This is the given Matrix
ker =#(x,y) exp(-1013*(x-y)'*(x-y));
[ns, ms] = size(M1);
for N = 1:ns
x = M1(N,:);
C = zeros(ms,ms);
for i = 1:ms
for j = 1:ms
C(i,j)= ker(x(i),x(j));
end
end
u = randn(ms,1);
[A,S, B] = svd(C);
z = A*sqrt(S)*u; % z = A S^.5 u
And I wanna plotting each regression in a Graph 3D as the below:
I know that plot is a ribbon, but I have not idea how can I do that
The desired plot can be generated without the use of ribbon. Just use a surf-plot for all the prices and a fill3-plot for the plane at z=0. The boundaries of the plane are calculated from the actual limits of the figure. Therefore we need to set the limits before plotting the plane. Then just some adjustments are needed to generate almost the same appearance.
Here is the code:
% generate some data
days = (1:100)';
price = days*[0.18,-0.08,0.07,-0.10,0.12,-0.08,0.05];
price = price + 0.5*randn(size(price));
years = 2002+(1:size(price,2));
% prepare plot
width = 0.6;
X = ones(size(price,1),1)*0.5;
X = [-X,X]*width;
figure; hold on;
% plot all 'ribbons'
for i = 1:size(price,2)
h = surf([days,days],X+years(i),[price(:,i),price(:,i)]);
set(h,'MeshStyle','column');
end
% set axis limits
set(gca,'ZLim',[-20,20]);
% plot plane at z=0
limx = get(gca,'XLim');
limy = get(gca,'YLim');
fill3(reshape([limx;limx],1,[]),[flip(limy),limy],zeros(1,4),'g','FaceAlpha',0.2)
% set labels
xlabel('Day of trading')
ylabel('Year')
zlabel('Normalized Price')
% tweak appearance
set(gca,'YTick',years);
set(gca,'YDir','reverse');
view([-38,50])
colormap jet;
grid on;
%box on;
This is the result:
That's a ribbon plot with an additional surface at y=0 which can be drawn with fill3

Plotting from different matlab files

I have four matlab codes and each of them generates a plot how can be able to combine all the plots into one plot to show the transition of each?
If you want to plot multiple lines on the same figure you can use hold on For example:
plot(x1,y1,'ok');
hold on
plot(x2,y2,'or');
If you are saying that they all form one single line then try concatenate your input vectors like this:
%Mock input
x1 = 0:9;
x2 = 10:19;
x3 - 20:29;
x4 = 30:39;
y1 = 2*x1 -20;
y2 = 2*x2 -20;
y3 = 2*x3 -20;
y4 = 2*x4 -20;
%Example of plotting concatenated vectors
plot( [x1;x2;x3;x4], [y1;y2;y3;y4]);
If you want all four to be on the same figure (say figure 1) then you can do this:
%% In PlotCode1.m
figure(1)
hold on
...%your plotting code
%% In PlotCode2.m
figure(1)
hold on
...%your plotting code
And if you run each of your PlotCode.m files without closing or clearing figure 1 then all the lines will show up on the same figure.
Alternately, you can turn each of your different plotting files into functions that take in the figure number as an input. For instance:
% In PlotCode1.m
function PlotCode1(num)
figure(num)
hold on
%Your plotting code
% In PlotCode2.m
function PlotCode2(num)
figure(num)
hold on
%Your plotting code
And now you can call each of these functions in one script:
fignum = 2;
PlotCode1(fignum)
PlotCode2(fignum)
And now everything will plot on figure 2.