drawing two plots in one plot - matlab

I have two plots. The x-axis for both is same, however the y-axis is different. I would like to plot both plots in one plot in a way that they share the same x-axis. x-axis represents time for both graphs. Any suggestions how to do that instead of plotting each one alone. The following picture shows what I'm looking for.

you can use subplot and delete the x-axis tick labels of the top one, for example:
x=rand(1,100);
subplot(2,1,1);
plot(1./x);
ylabel('Label 1')
set(gca,'XTickLabel',[])
subplot(2,1,2);
plot(x)
ylabel('Label 2')
xlabel('x Label')
If you want the plots to be closer to one another you can use one of the FEX files such as subplot_tight etc (for example this one , or this one)
Or you can just use axes...

Related

Legend each curve in a single figure from a matlab plot

I want to plot four curves in a single figure from matlab, so I am using hold on. Furthermore, I want to create an legend to each curve, so I wrote the code:
clear all
x=linspace(0,10,100);
x2=linspace(-5,15,100);
x3=linspace(-10,20,100);
x4=linspace(35,40,100);
figure(1)
plot(x,x2)
legend('x2')
hold on
plot(x,x3)
legend('x3')
hold on
plot(x,x4)
legend('x4')
hold on
plot(x,x)
legend('x')
hold off
But the result is that all my curves are in the same color, and just the last legend "x" has appeared in the figure (see it below).
How can I set one legend to each curve? All curves must have different colors.
This depends a bit on your matlab version. In older versions (and in octave), plots added through the use of hold on get the same color. In R2015b (i don't know when this was introduced), the individual plots get different colors, but still only one legend is displayed.
To get multiple colors and multiple legend entries, you can specify all data to be plotted in one call, the same for the legends:
plot(x, x, x, x2, x, x3, x, x4);
or
plot(x, [x', x2', x3', x4']);
For the legends, approach the same way:
legend('x', 'x2', 'x3', 'x4');
If you want to build up a legend without knowing the number of entries before hand, you would want to search for "dynamic legends". See for example here:
http://undocumentedmatlab.com/blog/legend-semi-documented-feature

Can set axis with decimal power of 10? [duplicate]

I have plotted to vectors against each other, and they are already logarithmic and everything is fine with that. But now that I have my plot i want the grid to be logarithmic. I write "grid on" in my code, and I think there should be a way to do this in the plot, but I can't remember how. How do I make the grid logarithmic?
If you use loglog, semilogx or semilogy instead of plot, the grid will automatically be on a log scale for the corresponding axes when using grid on.
If you have already plotted the axes, you can execute the following on the command line:
set(gca,'yscale','log') %# to set the y-axis to logarithmic
set(gca,'xscale','log') %# to set the x-axis to logarithmic
Have a look at axes properties to find out what else you can modify.

How to break the axis in matlab

How could one break the x axis in the same figure (not the subplot function)?
You can see an example in line graph in panel b in the following picture, one single row data have been split into three parts as well as the x axis.
the question is how this plot can be achieved in matlab.
(like the panel b in this figure)
Ill leave you an exmaple here using surf.
The trick is inserting nans wherever you want the lines to appear "empty"
z=peaks(100);
% If you want to delete a certain amount of rows/cols
z2=z;
z2(:,10:15)=NaN;
z2(:,50:55)=NaN;
z2(:,75:80)=NaN;
% If you want to separate you data without deleting anything
z3=z;
z3=[z3(:,1:15) nan(size(z3,1),5) z3(:,16:75) nan(size(z3,1),5) z3(:,75:100) ];
% This last bit is only for plotting, so you can try it in your computer
subplot(131)
title('Original')
surf(z,'edgecolor','interp')
axis off
view(2)
axis equal
subplot(132)
title('Deleted columns')
surf(z2,'edgecolor','interp')
axis off
view(2)
axis equal
subplot(133)
title('Separatedd data')
surf(z3,'edgecolor','interp')
axis off
view(2)
axis equal
There are a couple of utilities on the File Exchange that allow you to do that:
Break X Axis
BreakXAxis

Matlab: making the grid in plot logarithmic

I have plotted to vectors against each other, and they are already logarithmic and everything is fine with that. But now that I have my plot i want the grid to be logarithmic. I write "grid on" in my code, and I think there should be a way to do this in the plot, but I can't remember how. How do I make the grid logarithmic?
If you use loglog, semilogx or semilogy instead of plot, the grid will automatically be on a log scale for the corresponding axes when using grid on.
If you have already plotted the axes, you can execute the following on the command line:
set(gca,'yscale','log') %# to set the y-axis to logarithmic
set(gca,'xscale','log') %# to set the x-axis to logarithmic
Have a look at axes properties to find out what else you can modify.

i am trying to plot "scatter" on top of "imagesc" which dosen't work.

i am trying to plot "scatter" on top of "imagesc" which dosen't work. However, i can plot "scatter" figure separately. I even tried "hold all" instead of "hold on". Can someone help me out? Thank you.
figure(2)
imagesc(lat1,height,scatter0')
hold on;
scatter(lat1,top2,'k')
title('2012_12_4')
colormap(colors)
axis xy
It is probably x-y limits that are not matched, or that the z-values that are not properly normalized to plot side by side. The normalization is important because imagesc and scatter will share the same colormap. Other than that, your code works nicely for me. For example, I'm normalizing in the range [0,1] the "z" values of both plots:
load seamount
m=peaks(200);
m=(m-min(m(:)))./(max(m(:))-min(m(:)));
imagesc([0.996 1.0005],[1,1.012],m);
hold on ;
z=(z-min(z(:)))./(max(z(:))-min(z(:)));
scatter(x./max(x(:)),y./max(y(:)),5,z);