MatLab: Scatter Plots and Raster Plots - matlab

Scatter Plots in MatLab. I can create a scatter plot with x and y arrays being the same size as follows:
function RasterTest()
clear all % clear memory
clc; % clear command window
close all; % close any figure windows that are open
x=[1 2 3]; % x positions of the charges
y=[4 8 2]; % y positions of the charges
scatter(x,y,'filled')
axis square
end
However, what if I want every x to have multiple y values? I.e. the array sizes are different. I think this is actually called a raster plot but MatLab doesn't seem to have something to do this?
Any help would be great :).

plot allows diferent size vectors
plot(x,[sin(x);2*sin(x);3*sin(x)],'*')

When the array sizes are different, how can you map every y value to the according x value? It's ambigous.
When you generate your data, just make sure that you insert every pair of values into the x and y arrays:
x = [1 2 3 1 3];
y = [3 4 5 6 7];
In the above example you got multiple points for the x values 1 and 3.

Related

How to exempt an object from Matlab plot limits calculation

I want to add objects to my matlab plots which have defined x limits but span the whole y range. Examples are vertical lines or shaded regions delimited by two x values. I am aware of the option to use the current plot limits like this:
plot(1:10)
yl = ylim();
% Use y limits of current plot as y values
patch([ 3 3 5 5 ],[ yl(1) yl(2) yl(2) yl(1) ], 'red');
However I want my users to be able to increase plot y limits afterwards (e.g. to synchronize limits of multiple plots) and also want them to continue profiting from Matlab's automatic setting of plot limits.
This would be archivable if I would use the following code to set the y coordinates of my objects to the largest and smallest possible integers, respectively (intmax() and intmin() in Matlab) and tell Matlab not to consider that object during calculation of plot limits.
plot(1:10)
% Make graphical object which spans the whole possibly y range
p = patch([ 3 3 5 5 ],[ intmin intmax intmax intmin ], 'red');
% Does something like the following function exist?
exemptFromPlotLimitsCalculation(p)
Is this possible in Matlab?
You could plot the patch (or fill) really large (for example by using realmax) and exclude it from rescaling by setting the property YLimInclude to off
patch([3 3 5 5], realmax*[ -1 1 1 -1], 'red', 'YLimInclude', 'off');
have a look at this
In the postActionCallback you can resize your patch

plot two signals with different axis and limits in matlab

let's consider i am plotting two signals together on the same graph which has different limits and thus i want different axis
plot(a)
axis ([-2 10 -2 8])
hold 'on'
plot(b)
axis ([-1 4 -4 7])
hold 'off'
where 'a' and 'b' are two signal expression. the problem here is the signals are getting plot but only the second axis is working and plot a is not getting limited to the first specified axis. the reason being the second axis is obviously overwriting the first axes but any idea how to plot both signals with both axis limits?
You can select the data you wish to plot using logical operators.
Let's consider the case for plot a.
Assign each column of bs to a variable:
x1 = bs(:,1)
y1 = bs(:,2)
Then select only the values that meet the condition specified:
xPlot = x1(x1 > -2 & x1 < 10)
yPlot = y1(y1 > -2 & y1 < 8)
Assuming they both contain the same number of elements you can then plot them.
If not, you need to pad the smaller array with Nan, for example, to avoid getting an error about mismatching dimension.
Once you know which array is smaller, you can do it as follows. Let's say in this case xPlot is smaller than yPlot:
m = max(numel(xPlot),numel(yPlot)) %// Just getting the larger dimension
xPlot(numel(xPlot)+1:m) = NaN
Now you can call
plot(xPlot,yPlot,'b-','LineWidth',2)
and that should work. The same applies for the b plot.
Hope that helps!
You may want to have a look at plotyy to get 2 different y-axis.
If there is nothing in common in your plots, maybe you should plot them on 2 different axes, like
figure('Name', 'Example');
subplot(121);plot(rand(3));
subplot(122);plot(rand(3));
UPDATE
If you absolutely need two axes, you may try something like this
figure('Name', 'plotyy');
h = plotyy([0 1 2 4], 0:4, [4 5], [2 1]);
linkaxes(h, 'off');
axis(h(1), [0 4 0 4]);
axis(h(2), [4 5 1 2]);
If what you're looking for is something along the lines of plotyy but in the other direction, look at plotxx function from matlabcentral that does a similar thing in the x-direction.
You may have to tweak it to get it do everything you need, but it will give you a good starting point.
You can use plotyy which will create 2 y axes with different scales and limits.

Surface plot with 3 vectors Matlab

I need to be able to do a surface plot using data from 3 vectors. I found similar information, but no method seems to work with my data. My X and Y columns are evenly spaced, but not in increasing order. I tried different methods, but none of them seem to give me what I want, which is a simple surface linking close points together. I tried the following:
[X Y]=meshgrid(x,y);
Z=griddata(x,y,z, X,Y);
surf(X,Y,Z);
This is not exactly what I want, because it creates a surface at z=0 and makes it look more like a volume plot than just a surface. It also runs very slowly on my computer (probably from creating all the gridpoints). If I could get something that doesn't require as much memory it would be ideal (my vectors have about 20k values each), but this is not a necessity.
***Edit: I also tried using the scatteredInterpolant method found here,but the function doesn't seem to be recognized by MATLAB and I get this error:
Undefined function 'scatteredInterpolant' for input arguments of type 'double'.
Also here is an image of my problem:
You can see that we can't see under the surface, there is some z=0 plane blocking it.
If you have anything for me, any help is appreciated.
Thanks in advance.
**Edit 2: I added sample vectors, they're my x,y and z values from left to right.
***Edit 3: Here's an image of the triangulation I get. As you can see some points are being ignored for some reason, which gives those long and weird looking blue triangles.
Mike
As conventional methods seem to fail, I would suggest you to do it manually.
Create a Z matrix full of NaN values. The size of the matrix should be dependant on your x and y values.
Loop over all occuring x,y, pairs and put their (average?) z value in the right position of your Z matrix.
Loop over all NaN values and interpolate their value. Perhaps using filter2.
Use surf to plot the resulting surface
If you have points which are described by vectors, and you want to plot them you could always use a Delauny triangulation. The function in matlab is called Tri=delauny(X,Y,Z). The data generated by this function can be shown with either trimesh(Tri,X,Y,Z) or trisurf(Tri,X,Y,Z). Keep in mind trisurf is only for 3D data. If you want to adjust the transparancy of plots in your graph use the alpha setting.
I hope this helps
To me it looks like you just need to sort your data before plotting.
Here is an example which I believe is similar to your case (since I could not download your data).
x = [2 1 4 3 -1 -3 -4 -2];
y = [1 2 3 4 -1 -2 -3 -4];
z = 32 - x.*x - y.*y;
[X1 Y1] = meshgrid(x,y);
Z1 = 32 - X1.*X1 -Y1.*Y1;
surf(X1,Y1,Z1)
aux = sort([x;y],2);
x = aux(1,:);
y = aux(2,:);
[X2 Y2] = meshgrid(x,y);
Z2 = 32 - X.*X - Y.*Y;
figure()
surf(X2,Y2,Z2)
The first figure results in a very problematic surface:
The second figure contains the desired surface:

MATLAB: Plotting different numerical approximation in one plot

I have the following matlab code for approximating a differential equation via the Euler-method:
% Eulermethod
a=0;
b=0.6;
Steps=6;
dt=(b-a)/Steps;
x=zeros(Steps+1,1);
x(1,1)=1;
y=zeros(Steps+1,1);
for i=1:Steps
x(i+1,1)=x(i,1)+dt*(x(i,1)*x(i,1)+1);
end
plot(x)
I want to be able to plot the solution plot for several different values of Steps in one plot and have the x-axis go from 0 to 0.6 instead of from for example 1 to 100 000 etc. Can this be done?
If you use the hold on command this will allow you achieve multiple plots on the same figure. Similarly, if you separate your data into x and y vectors, you can plot them against eachother by passing 2 vectors to plot instead of just one. For example
figure
hold on
for i=1:m
x = [];
y = [];
%% code to populate your vectors
plot(x,y)
end
You should now see all your plots simultanesously on the same figure. If you want x to be composed of n equally spaced elements between 0 and 0.6, you could use the linspace command:
x = linspace(0.0,0.6,n);
In order to distinguish your plots, you can pass an extra paramter to the function .For example
plot(x,y,'r+')
will plot the data as a series of red + symbols.
Plot can take more arguments: plot(x_axis,values, modifiers);
If x-axis is a vector of M elements, values can be a matrix of MxN elements, each of which are drawn with a separate color.

How can I find equation of a plot connecting data points in Matlab?

I have various plots (with hold on) as show in the following figure:
I would like to know how to find equations of these six curves in Matlab. Thanks.
I found interactive fitting tool in Matlab simple and helpful, though somewhat limited in scope:
The graph above seems to be linear interpolation. Given vectors X and Y of data, where X contains the arguments and Y the function points, you could do
f = interp1(X, Y, x)
to get the linearly interpolated value f(x). For example if the data is
X = [0 1 2 3 4 5];
Y = [0 1 4 9 16 25];
then
y = interp1(X, Y, 1.5)
should give you a very rough approximation to 1.5^2. interp1 will match the graph exactly, but you might be interested in fancier curve-fitting operations, like spline approximations etc.
Does rxns stand for reactions? In that case, your curves are most likely exponential. An exponential function has the form: y = a*exp(b * x) . In your case, y is the width of mixing zone, and x is the time in years. Now, all you need to do is run exponential regression in Matlab to find the optimal values of parameters a and b, and you'll have your equations.
The advice, though there might be better answer, from me is: try to see the rate of increase in the curve. For example, cubic is more representative than quadratic if the rate of increase seems fast and find the polynomial and compute the deviation error. For irregular curves, you might try spline fitting. I guess there is also a toolbox in matlab for spline fitting.
There is a way to extract information with the current figure handle (gcf) from you graph.
For example, you can get the series that were plotted in a graph:
% Some figure is created and data are plotted on it
figure;
hold on;
A = [ 1 2 3 4 5 7] % Dummy data
B = A.*A % Some other dummy data
plot(A,B);
plot(A.*3,B-1);
% Those three lines of code will get you series that were plotted on your graph
lh=findall(gcf,'type','line'); % Extract the plotted line from the figure handle
xp=get(lh,'xdata'); % Extract the Xs
yp=get(lh,'ydata'); % Extract the Ys
There must be other informations that you can get from the "findall(gcf,...)" methods.