Edit the x limits of least squares line - matlab

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.

Related

How to superimpose two contour maps onto each other in matlab?

I have two contour maps in Matlab and each of the two maps has a single curve specifying a single Z-value. I want to super impose the two contour maps so that I can find the single solution where the two z-value curves intersect. How could I go about super imposing the two contour maps?
% the two contour maps are coded the exact same way, but with different z-values
x = 0.05:0.05:1;
y = 0.0:0.05:1;
[X, Y] = meshgrid(x, y);
% Z-value data is copied from excel and pasted into an array
Z = [data]
contourf(X, Y, Z);
pcolor(X, Y, Z); hold on
shading interp
title();
xlabel();
ylabel();
colorbar
val = %z-value to plot onto colormap
tol = %tolerance
idxZval = (Z <= val+tol) & (Z >= val-tol);
plot(X(idxZval), Y(idxZval))[enter image description here][1]
The end result you seek is possible using contourc or using contour specifying the same contours (isolines).
This answer extends this answer in Approach 1 using contourc and provides a simple solution with contour in Approach 2.
You might ask "Why Approach 1 when Approach 2 is so simple?"
Approach 1 provides a way to directly access the individual isolines in the event you require a numerical approach to searching for intersections.
Approach 1
Example Data:
% MATLAB R2018b
x = 0:0.01:1;
y = 0:0.01:1;
[X,Y] = meshgrid(x,y);
Z = sqrt(X.^3+Y); % Placeholder 1
W = sqrt(X.*Y + X.^2 + Y.^(2/3)); % Placeholder 2
Overlay Single Isoline from 2 Contour Plots
Mimicking this answer and using
v = [.5 0.75 .85 1]; % Values of Z to plot isolines
we can visualize these two functions, Z, and W, respectively.
We can overlay the isolines since they share the same (x,y) domain. For example, they both equal 0.8 as displayed below.
val = 0.8; % Isoline value to plot (for Z & W)
Ck = contourc(x,y,Z,[val val]);
Ck2 = contourc(x,y,W,[val val]);
figure, hold on, box on
plot(Ck(1,2:end),Ck(2,2:end),'k-','LineWidth',2,'DisplayName',['Z = ' num2str(val)])
plot(Ck2(1,2:end),Ck2(2,2:end),'b-','LineWidth',2,'DisplayName',['W = ' num2str(val)])
legend('show')
Overlay Multiple Isolines from 2 Contour Plots
We can also do this for more isolines at a time.
v = [1 0.5]; % Isoline values to plot (for Z & W)
figure, hold on, box on
for k = 1:length(v)
Ck = contourc(x,y,Z,[v(k) v(k)]);
Ck2 = contourc(x,y,W,[v(k) v(k)]);
p(k) = plot(Ck(1,2:end),Ck(2,2:end),'k-','LineWidth',2,'DisplayName',['Z = ' num2str(v(k))]);
p2(k) = plot(Ck2(1,2:end),Ck2(2,2:end),'b-','LineWidth',2,'DisplayName',['W = ' num2str(v(k))]);
end
p(2).LineStyle = '--';
p2(2).LineStyle = '--';
legend('show')
Approach 2
Without making it pretty...
% Single Isoline
val = 1.2;
contour(X,Y,Z,val), hold on
contour(X,Y,W,val)
% Multiple Isolines
v = [.5 0.75 .85 1];
contour(X,Y,Z,v), hold on
contour(X,Y,W,v)
It is straightforward to clean these up for presentation. If val is a scalar (single number), then c1 = contour(X,Y,Z,val); and c2 = contour(X,Y,W,val) gives access to the isoline for each contour plot.

How to turn y axis of histogram to show percentage ranging from 0 to 1

I want to change the y axis of the histogram to show percentages ranging from 0 to 1. this is what I've tried, but it doesn't seem to be working.
myTolerance=1e-12; % in erg units.
nbins=50;
for j=1:ntM/100:ntM
H = histfit(Wkinet(abs(Wkinet(:,j))>myTolerance, j) * erg2eV, nbins);
%Select from column j all rows in column j whose absolute values are
%greater than the tolerance.
H(1).delete; %%Remove bins, only keep the fit.
set(gca, 'YScale', 'log');
set(gca, 'XScale', 'log'); % Make logarithmic X
yt = get(gca, 'YTick');
set(gca, 'YTick', yt, 'YTickLabel',
yt/numel(Wkinet(abs(Wkinet(:,j))>myTolerance)))
pause;
end
This is what is currently looks like:
This is what I want:
Just to simplify the discussion below, the line
H = histfit(Wkinet(abs(Wkinet(:,j))>myTolerance, j) * erg2eV, nbins);
is equivalent to
data = Wkinet(abs(Wkinet(:,j))>myTolerance, j) * erg2eV;
H = histfit(data, nbins);
This means below we'll assume data is a vector.
histfit computes and plots a histogram though histogram, then fits a function to it through fitdist. Since you don't want to plot the histogram itself, just stick to fitdist:
pd = fitdist(data,'Normal'); % this is the default distribution used in `histfit`, is it correct?
x = linspace(min(data),max(data),200); % 200 points in the graph, you might want to change this?
y = pdf(pd,x);
plot(x,y);
Now it's simple to normalize the plot however we want. For example set the first element to 1:
pd = fitdist(data,'Normal');
x = linspace(min(data),max(data),200);
y = pdf(pd,x);
y = y/y(1); % <<< Normalize
plot(x,y);
You can set limits on your y-axis using
ylim([1e-3 1]) %lower limit is nonzero since it's plotted on log scale
or
set(gca, 'ylim', [1e-3 1])

How to customize contour lines in Matlab?

I am preparing a contour map where I am supposed to highlight the contour line for a specific level. For Example, my contour line values are lying between -1 and 1 and I want to highlight the line corresponding to the value 0. I tried to do this using the following procedure,
[M,c]=contourf(longitude,latitude,delta',-1:0.2:1);
s=size(c.LevelList,2);
for i=1:s
if (c.LevelList(i)==0)
c.LevelWidth=2;
end;
end;
However, it does nothing to the contour map. Can anyone please help me with the appropriate procedure?
I would suggest simply using contour on your desired levels to highlight after the initial contourf, like so:
% Input.
x = linspace(-2*pi, 2*pi, 101);
y = x + pi;
[X, Y] = meshgrid(x, y);
Z = 0.5 * (sin(X) + cos(Y));
% Levels to plot with contourf.
levelsf = -1:0.2:1;
% Levels to highlight.
levels = [0 0.3];
figure(1);
hold on;
% Contourf all levels.
contourf(X, Y, Z, levelsf);
% Highlight levels with simple contour.
contour(X, Y, Z, levels, 'r', 'LineWidth', 2);
hold off;
For highlighting levels = [0 0.3], you'll get:

plot some data such as pairs in matlab

I want to plot some data, but I can't.
It is assumed that we have 820 rows in 2 columns, representing the x and y coordinates.
My code is as follows:
load('MyMatFileName.mat');
[m , n]=size(inputs);
s = zeros(m,2);
for m=1:820
if inputs(m,end-1)<2 & inputs(m,end)<2
x = inputs(m,end-1)
y = inputs(m,end)
plot(x,y,'r','LineWidth',1.5)
hold on
end
end
I've edited your code and added comments to explain the changes you could make, but see below I've also re-written your code to be more like how it should be done:
load('MyMatFileName.mat'); % It's assumed "inputs" is in here
% You don't use the second size output, so use a tilde ~
[m, ~] = size(inputs);
%s = zeros(m,2); % You never use s...
% Use a different variable for the loop, as m is already the size variable
% I've assumed you wanted ii=1:m rather than m=1:820
figure
hold on % Use hold on and hold off around all of your plotting
for ii=1:m
if inputs(m,end-1)<2 && inputs(m,end)<2 % Use double ampersand for individual comparison
x = inputs(m,end-1)
y = inputs(m,end)
% Include the dot . to specify you want a point, not a line!
plot(x, y, 'r.','LineWidth',1.5)
end
end
hold off
A better way of doing this whole operation in Matlab would be to vectorise your code:
load('MyMatFileName.mat');
[m, ~] = size(inputs);
x = inputs(inputs(:,end-1) < 2 & inputs(:,end) < 2, end-1);
y = inputs(inputs(:,end-1) < 2 & inputs(:,end) < 2, end);
plot(x, y, 'r.', 'linewidth', 1.5);
Note that this will plot points, if you want to plot the line, use
plot(x, y, 'r', 'linewidth', 1.5); % or plot(x, y, 'r-', 'linewidth', 1.5);

Animating plot with matlab / octave

I'm trying to animate a plot for this equation see below I'm trying to animate it for b when 300>= b <= 486
clear all, clc,clf,tic
m=3.73;
a=480;
b=486;
r=1;
fs=44100;
x=linspace(0,2*pi,fs)';
y=m^3*cos(sqrt(a*r*x)).^(0.77)/r + m^3*cos(sqrt(b*r*x)).^(0.77)/r-20;
normalize_y=(y/max(abs(y))*0.8);
plot(x,y)
I'm using octave 3.8.1 which is a clone of matlab
Put your plotting code in a for loop with b as the iterating variable, then place a pause for a small amount of time. After, plot your graph, then use drawnow to refresh the plot. In other words, try this code. I've placed %// Change comments in your code where I have introduced new lines:
m=3.73;
a=480;
r=1;
fs=44100;
x=linspace(0,2*pi,fs)';
figure;
for b = 300 : 486 %// Change
y=m^3*cos(sqrt(a*r*x)).^(0.77)/r + m^3*cos(sqrt(b*r*x)).^(0.77)/r-20;
normalize_y=(y/max(abs(y))*0.8);
pause(0.1); %// Change
plot(x,y);
title(['b = ' num2str(b)]); %// Change
drawnow; %// Change
end
As a bonus, I've put what the current value of b is at each drawing of the plot. BTW, I don't know why normalize_y is in your code when you aren't using it. Do you mean to plot normalize_y instead of y? Just an after thought. Anyway, try that out and see how it looks. Good luck!
Another solution would be to use the handle of a plot and then only update the 'YData'-property of a plot. That is especially useful for more complex plots where there are more than 1 line but you only want to change one line. Also Axis-labels are not overwritten then, which generally prevents alot of overhead.
In Matlabcode it could look like this:
% // Parameter and x-range
m=3.73;
a=480;
r=1;
fs=44100;
x=linspace(0,2*pi,fs)';
% // function to compute y for given x and parameter b
f = #(x, b) m^3*cos(sqrt(a*r*x)).^(0.77)/r + m^3*cos(sqrt(b*r*x)).^(0.77)/r-20;
% // first plot out of loop (to get plot handle)
figure;
b = 300;
y = f(x, b);
handle = plot(x, y);
xlabel('x') % // only set once
ylabel('y=f(x,b)') % // only set once
title(['b = ' num2str(b)]);
pause(0.1);
% // animate for b = 301 to 86
for b = 301:486 %// Change
set(handle, 'YData', f(x, b)) % set new y-data in plot handle
pause(0.1); %// update plot
title(['b = ' num2str(b)]); %// update title
end
This will work with octave 3.8.1
% // Parameter and x-range
m=3.73;
a=480;
r=1;
fs=44100;
x=linspace(0,2*pi,fs)';
% // function to compute y for given x and parameter b
f = #(x, b) m^3*cos(sqrt(a*r*x)).^(0.77)/r + m^3*cos(sqrt(b*r*x)).^(0.77)/r-20;
% // first plot out of loop (to get plot handle)
figure;
b = 300;
y = f(x, b);
handle = plot(x, y);
xlabel('x') % // only set once
ylabel('y=f(x,b)') % // only set once
title(['b = ' num2str(b)]);
pause(0.1);
% // animate for b = 301 to 86
for b = 301:486 %// Change
%set(handle, 'YData', f(x, b)) % set new y-data in plot handle
%To work with octave 3.8.1 use the line below
set(handle, 'YData', real (f(x, b)))
pause(0.1); %// update plot
title(['b = ' num2str(b)]); %// update title
end