plot two signals with different axis and limits in matlab - 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.

Related

plotting graphs with x-axis of desired length in matlab

I want to plot graphs in matlab, In hand, I had two raw data obtained from the market, say at year 0.25,0.5,0.75,1,2,3 and 4, corresponding values of product A are [0.9998,0.997,0.887,0.779,0.661,0.442,0.345] and B are [0.878,0.765,0.662,0.594,0.436,0.304,0.211] respectively. When I use
plot([0.25,0.5,0.75,1,2,3,4],[0.9998,0.997,0.887,0.779,0.661,0.442,0.345],'k+',[0.25,0.5,0.75,1,2,3,4],[0.878,0.765,0.662,0.594,0.436,0.304,0.211],'b*')
However, the graphs produced gives 4 lines. What should be done to fix the problem?
You need to create a graphic handler before modifying the XTick, Use the following
figure
ax = gca ;
plot([0.25,0.5,0.75,1,2,3,4],[0.9998,0.997,0.887,0.779,0.661,0.442,0.345],'k+',[0.25,0.5,0.75,1,2,3,4],[0.878,0.765,0.662,0.594,0.436,0.304,0.211],'b*')
ax.XTick = [0 0.25 0.5 0.75 1 2 3 4];

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

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:

Connecting final and initial point in simple x-y plot (Plotting closed curve/polygon)

Say, for example, I had ...
x = [1 1 2 2];
y = [1 2 2 1];
plot(x, y, 'b-');
I will get a plot with lines connecting the points (1,1), (1,2), and (2,2). Is there any way to connect the final point with the first, thus completing the square on the plot?
I'm also pulling in lines of text with points, so simply adding another point 1,1 is not an option.
impoly can be useful, however, it creates a modifiable curve which is slower than plot.
You can write a simple function for that:
function plotc(x,y,varargin)
x = [x(:) ; x(1)];
y = [y(:) ; y(1)];
plot(x,y,varargin{:})
end
By the way, the (:) colon operator is used as defensive programming means. In this way, x and y can be either row or column vectors.
The varargin allows using additional parameters, like:
plotc(x,y,'Color','r');
plotc(x,y,'Parent',a,'LineWidth',2);
Unless your final and last points are the same then plot won't know that you want a closed curve. So either add an additional point to your list to plot or try using, for example, rectangle.
Do you have the Image Processing Toolbox? If yes,
impoly(hparent, position, 'Closed')
might be of use to you.
http://www.mathworks.de/help/toolbox/images/ref/impoly.html

MatLab: Scatter Plots and Raster Plots

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.