Plotting multiple fitobjects in the same figure in MATLAB 2016b - matlab

I have multiple fitobjects that I want to plot in the same figure. All curves are fitted from the same five points on the x-axes (ranging from 0.25 to 2.25). If I plot them individually the curve correctly spans only between does values, however, as soon as I add hold on to plot them in the same figure the first fitobject is plotted correctly, while the second spans from -Inf to +Inf, if I change the order of fitobjects the problem remains. So the first curve is always shown correctly the following ones are not. I am not even setting any axes properties yet. Also the two fitobjects have the same Boundaries for the fit.
Example code:
plot(fitobj{1}, 'r'); hold on %curve from 0.25 to 2.25
plot(fitobj{2}, 'b'); %curve is shown from -Inf to +Inf
Do you know why this is happening? Thank you

Related

MATLAB: Two different y-axis limits for Multiple plots on same graph

I need to plot two plots on same figure in MATLAB.
The maximum and minimum values in both the data samples have large variation, which I am unable to plot by taking same y-axis limits.
I do not wish to use two scales as explained in other Overlaying two axes in a Matlab plot but need to use a single y-axis and get the solution.
I tried the code:
x_axis_X = 1:length(S);
y_axis_Y = 1:length(N);
ylim([-1204200 -1841.6])
set(gcf,'color','w');
plot(x_axis_X, S,'o-', y_axis_Y, N, 'x-');
The result is as shown in the plot where one data sample is plotted without proper y-axis range.
The y limits for first data sample is -1204200 to -1841.6 and for the second it is -489429345.5 to -10408189.43.
How should be the ylim defined to fit both plots in the same figure?
I appreciate your inputs. Thank you.
In older versions of MATLAB use the function plotyy. In more recent versions of MATLAB use yyaxis. The following is the example from the documentation:
x = linspace(0,10);
y = sin(3*x);
yyaxis left
plot(x,y)
z = sin(3*x).*exp(0.5*x);
yyaxis right
plot(x,z)
ylim([-150 150])
I tried the idea of scaling one dataset so that it has a similar magnitude as the other data set. Here, I multiplied one dataset by 100 (or any suitable scaling parameter), and then it will be similar in size to the other data set. In order to clearly mention which data has been scaled in the graph I used the legend.
plot(x,data1,x,100*data2)
legend('data1','100*data2','location','southeast')
Thank you.
Scaling is not the best option, as you may need to work with the data later. Also does not work if for instance, you need a log scale.
Matlab has a few ways to deal it with. I particularly like to use a new axes in the figure, as i have done in the example below.
Just in case, you also found this answer in a simple google search!
Code:
a=1:10;
b=(10:-1:1)*100;
x=1:10;
hold on
plot(x,a,'b')
pax1=get(gca,'Position'); %get axis position
ax2 = axes('Position',pax1); %create a new axis
plot(ax2,x,b,'r') %plot new data
set(ax2, 'Yaxislocation','right',...
'color','none') % set it transparent and to the right

Scatter with 2 y-axes and datetick

I would really appreciate some help.
I have to independent datasets. Each dataset contains two variables: dates (as datenumber) and corresponding data. I need to plot both datasets on one scatter plot with dates on x-axis and two y-axes. I have been trying with the following code:
figure(1);
scatter(x1,y1,'g');
set(gca);
ax1=gca;
set(ax1,'YColor','g');
ax2 = axes('Position',get(gca,'Position'),'YAxisLocation','right', XTick'[],'Color','none','YColor','r');
hold on; scatter(x2,y2,'r');
Now, this gives correct y-axis on the right side, however on the right side I end up with two overlapping y-axes.
Also, I need to change x-axis so that it displays dates as opposed to datenumbers. I've tried to incorporate datetick into the code but it again gives me two overlapping x-axes.
Does anyone know how to go about it?
thank you
I tried your script with your sample input and found no problems. Anyway, here's a solution which uses the matlab function plotyy, which is suited to simple plots like this:
%generate input
x1=[732490 732509 732512 732513 732521 732528];
y1=[7.828 7.609 22.422 14.758 26.258 1.477];
x2=[732402 732403 732404 732404 732433];
y2=[0.693 0.645 0.668 0.669 0.668];
figure(1);
[ax, h1,h2]=plotyy(x1,y1,x2,y2,'scatter');
%set colors manually
green=[0 1 0];
red=[1 0 0];
set(h1,'cdata',green);
set(h2,'cdata',red);
set(ax(1),'ycolor',green);
set(ax(2),'ycolor',red);
%note the 'keepticks' and 'keeplimits' options
datetick(ax(1),'x','yyyy-mm-dd','keepticks','keeplimits');
datetick(ax(2),'x','yyyy-mm-dd','keepticks','keeplimits');
Without the datetick call the plotyy function synchronizes the xticks in the plot. When you call datetick, it recalculates the ticks, unless you explicitly tell it not to, see the option keepticks, and this is seen as two sets of x axes (even though the x coordinates are the same, the ticks are located at different positions). The keeplimits option is needed to preserve the original xlim. It obviously needs some more manual work to get plots like this sufficiently pretty.
Also note that I set the axes and data colors manually: there might be a way to do this more elegantly.
Update: keeplimits originally missing
Update2: changed sample data to correspond to updated question comment

MATLAB - Plot half the Nyquist Plot

Is there a way to only plot half the nyquist plot in MATLAB? Nyquist usually has the two lines that go in opposite directions (one going up, and one going down). I only want to plot the one going down.
I know that I can only show the negative axis to "hide" the positive bit of the plot, but I want to show the whole chart, but only have the one line:
Okay, I got the answer. Use nyquistplot instead of nyquist and set the option ShowFullContour to off:
% This is an example contour
G = tf(80, [1 9 30 40]);
h = nyquistplot(G);
setoptions(h, 'ShowFullContour', 'off');

How can we fill different levels of contour with a color in matlab

I have a figure which consists of different levels of a contour plotted using the hold on function. I want to fill the space between the levels of contour with a color. Could you please assist me how can I do that. I have already tried the contourf function. The figure consists of different red colored levels of a contour, what I want is a solid color filled between these contour levels.
I am not sure if I got exactly what you wanted but here goes a possible solution to your problem.
If you plotted using the hold on function I would therefore assume you have each contour in a different variable. If so you can use logic to check whether a contour is higher or lower relatively to another. If your problem is that you do not have the same x axis for each so that you can use logic just interpolate for each contour (for example between -0.8 and 0.8)
I will give an example of what I am saying. See if this helps.
%simulated contours
x=linspace(0,pi,100);
y = sin(x);
y2 = sin(x)*2;
y3 = sin(x)*3;
figure, hold on,
plot(x,y),
plot(x,y2),
plot(x,y3),
%fill contours
[X,Y]=meshgrid(x,0:3/100:3);
zzz=(Y<repmat(y,size(Y,1),1))+(Y<repmat(y2,size(Y,1),1))+(Y<repmat(y3,size(Y,1),1));
figure,imagesc(zzz)
set(gca,'YDir','normal')

Matlab plot a threshold line

I want to add a horizontal threshold line to my graph of 0.7. But I can't seem to get it to work.
Code
figure(1)
plot(Variance);
hold on;
plot([1 frames], threshold, 'red')
Variance is an array to be plotted and the frames are the number of variances that are plotted. I've tried moving the hold and plots around but it doesn't seem to work either.
I know this is an easy question, but everything I've looked at online doesn't seem to work either, with regards to adding them to the same plot().
Thanks
You should change the second plot line to
plot([1 frames], [threshold threshold], 'red');