Efficiently Plot Markers of varying individual size - matlab

The plot function allows us to plot all markers with a constant size s.
figure;
x = -10 : 10;
y = x .^ 2;
s = 10;
plot(x, y, 'bo', 'MarkerSize', s);
Suppose instead we would like each marker to have some individual size sx. For example, sx = abs(x) + 1.
One way to achieve this would be to use a for loop.
figure;
x = -10 : 10;
y = x .^ 2;
sx = abs(x) + 1;
hold on;
for i = 1 : length(x)
plot(x(i), y(i), 'bo', 'MarkerSize', sx(i));
end
This works well enough for small number of x. However, what if x is larger? For example, x = -100 : 0.01 : 100.
This now takes significantly longer, while plot(x, y, 'bo', 'MarkerSize', 100) would still complete almost instantly. Ideally, we would be able to do something such as plot(x, y, 'bo', 'MarkerSize', sx) where sx is a vector of sizes with each entry in sx corresponding to an entry in x and y. Unfortunately, that would give an error of Value not a numeric scalar.
Is there an efficient way to plot markers where each marker have varying individual sizes?

What you're trying to do is possible using scatter instead of plot as follows:
scatter(x,y,abs(x)+1)

Related

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.

How to make a matrix for a particular surface plot?

I have a time column vector
t =
[0;
1;
3;
7;
10]
a frequency column vector
f =
[978;
573;
269;
102;
14]
and an impedance column vector
Z =
[0;
10;
64;
97;
103]
The kind of surface plot I'm looking for would be so that if you rotate the figure to display Z vs. t, you would see something equivalent to
plot(t, Z)
and if you rotate the figure to display Z vs. f, you would see something equivalent to
semilogx(f, Z)
x axis time, y axis frequency, and z axis impedance.
Here's what I have so far:
Zimp1 = flipud(toeplitz(flip(Z)));
figure(1);
surf(t, f, Zimp1);
set(gca, 'Yscale', 'log');
The problem lies within the Zimp1 matrix--the matrix that shows what's going on in the surface plot with the values of frequency and time. How do I construct the Zimp1 matrix to give me exactly that: plot(t, Z) on the view([-90, 0]) side and semilogx(f, Z) on the view([0, 0]) side?

How can I plot a pattern of random points in a specific region?

I wrote this matlab code to plot a set of random points in a specific region of the graph. I need to set the xlim and ylim in the range (1,512) (-1,512) but when I substituted this values in the following code nothing is plotted.I tried also to insert the range 150,350 that represent the central part of the graph in which I want to plot all points. How can I fix the problem?
x = rand(1, 50);
y = rand(1, 50);
plot(x,y,'.')
xlim([-0.2 1.2])
ylim([-0.2 1.2])
The output of rand is going to contain values between 0 and 1 so when you expand the x and y limits to [1 512] all data is going to be shown within the lower 1/512th of the axes and therefore you can't see the individual points.
If you want your random values to actually span the range [1 512] (for x) and [-1 512] (for y), then you'll want to alter the output of rand accordingly.
x = 1 + rand(1, 50) * 511;
y = rand(1, 50) * 513 - 1;
plot(x, y, '.')
xlim([1 512]);
ylim([-1 512]);
A more general solution would be to create an anonymous function which creates random numbers within the specified range
myrand = #(r, varargin)rand(varargin{:}) * diff(r) + min(r);
xrange = [1 512];
yrange = [-1 512];
x = myrand(xrange, 1, 50);
y = myrand(yrange, 1, 50);
plot(x, y, '.')
xlim(xrange);
ylim(yrange);
Or if you want your points to be within a certain region inside of the axes
x = myrand([50 100], 1, 50);
y = myrand([50 100], 1, 50);
plot(x, y, '.');
xlim([0 150])
ylim([0 150])

fill function in MATLAB

I'm having issues unterstanding the function fill in MATLAB , I have a PSD of a file the I want to change it background like :
[xPSD,f] = pwelch(x,hanning(4096),2048,4096*2 ,fs);
plot(f,10*log10(xPSD));
x= f(100:150);
y= 10*log10(xPSD(100:150))
fill(x,y,'y')
the result is in the right direction but not what I need :
I would like get the color tell x axis like :
is their a way to do this
A working solution is:
[xPSD,f] = pwelch(x,hanning(4096),2048,4096*2 ,fs);
plot(f,10*log10(xPSD));
hold on
x= f(100:150);
y= 10*log10(xPSD(100:150));
yMax = ylim;
yMax = yMax(2);
x = x'; % Use this line only in the case that the size(x, 1) > 1
X = [x fliplr(x)];
Y = [y' ones(1, length(y)) .* yMax];
fill(X, Y, 'y')
What you were missing is that fill method looks for an area to fill. In the above code the area is defined by pairs of points. That, for the first (i.e. lower part) of the area we have the x vector and the y points. The second area (i.e. the upper part) is defined by the reversed vector x (image your pencil to first starting drawing towards rights for the lower part and then for the upper going left) and the points of the upper limit of your axes.
Edit:
Minimal example with the handel data from MATLAB:
load handel;
x = y; % Just to be consistent with the OP
fs = Fs; % Just to be consistent with the OP
[xPSD,f] = pwelch(x,hanning(4096),2048,4096*2 ,fs);
plot(f,10*log10(xPSD));
hold on
x= f(100:150);
y= 10*log10(xPSD(100:150));
yMax = ylim;
yMax = yMax(2);
x = x'; % Use this line only in the case that the size(x, 1) > 1
X = [x fliplr(x)];
Y = [y' ones(1, length(y)) .* yMax];
fill(X, Y, 'y')
xlim([0 200]) % To focus on the result
The result is:
Yes, there is always a way ;)
In your case, you simply need to add two points in x and y that go to the top boudary of the plot:
x = f(100:150);
y = 10*log10(xPSD(100:150))
% Add two points
Y = ylim;
x = [x(1) ; x(:) ; x(end)];
y = [Y(2) ; y(:) ; Y(2)];
% Display filled area
fill(x,y,'y')
Best,

Gaussian Probabilities plot around a trajectory

I am trying to write some code to generate a plot similar to the one below on matlab (taken from here):
I have a set of points on a curve (x_i,y_i,z_i). Each point generates a Gaussian distribution (of mean (x_i,y_i,z_i) and covariance matrix I_3).
What I did is I meshed the space into npoint x npoints x npoints and computed the sum of the probability densities for each of the 'sources' (x_i,y_i,z_i) in each point (x,y,z). Then, if the value I get is big enough (say 95% of the maximum density), I keep the point. otherwise I discard it.
The problem with my code is that it is too slow (many for loops) and the graph I get doesn't look like the one below:
Does anyone know whether there is a package to get a similar plot as the one below?
Using isosurface we can do reasonably well. (Although I'm not honestly sure what you want, I think this is close:
% Create a path
points = zeros(10,3);
for ii = 2:10
points(ii, :) = points(ii-1,:) + [0.8 0.04 0] + 0.5 * randn(1,3);
end
% Create the box we're interested in
x = linspace(-10,10);
y = x;
z = x;
[X,Y,Z] = meshgrid(x,y,z);
% Calculate the sum of the probability densities(ish)
V = zeros(size(X));
for ii = 1:10
V = V + 1/(2*pi)^(3/2) * exp(-0.5 * (((X-points(ii,1)).^2 + (Y-points(ii,2)).^2 + (Z-points(ii,3)).^2)));
end
fv = isosurface(X,Y,Z,V, 1e-4 * 1/(2*pi)^(3/2), 'noshare');
fv2 = isosurface(X,Y,Z,V, 1e-5 * 1/(2*pi)^(3/2), 'noshare');
p = patch('vertices', fv.vertices, 'faces', fv.faces);
set(p,'facecolor', 'none', 'edgecolor', 'blue', 'FaceAlpha', 0.05)
hold on;
p2 = patch('vertices', fv2.vertices, 'faces', fv2.faces);
set(p2,'facecolor', 'none', 'edgecolor', 'red', 'FaceAlpha', 0.1)
scatter3(points(:,1), points(:,2), points(:,3));