Plotting two Matlab figures next to each other - matlab

I'm trying to plot the two figures next to each other. The idea is to create square plots with 1 to 10 on the x-axis. I'm aware of the subplot function but some how it's not working for me. Help is greatly appreciated.
% plotting the data
data1_plot=data1.*100;
data2_plot=data2.*100;
figure(1),
subplot(1,2,1);
plot((1:10)',mean(data1_plot),'o-','LineWidth',1);hold on
for ii=2:10;
if mean(data1_plot(:,ii))<mean(data1_plot(:,ii-1));
plot([ii-1,ii],mean(data1_plot(:,ii-1:ii)),'r.-');
end
end
set(gca,'XTickLabel',{'Unprofitable';'2';'3';'4';'5';'6';'7';'8';'9';'Profitable'});
axis([0.8,10.2,0.0,1.5]),...
ylabel('Average return'),...
title('\rm Equally-weighted PMU portfolio returns, 1990-2015');hold off;
subplot(1,2,2);
plot((1:10)',mean(data2_plot),'o-','LineWidth',1);hold on
for ii=2:10;
if mean(data2_plot(:,ii))<mean(data2_plot(:,ii-1));
plot([ii-1,ii],mean(data2_plot(:,ii-1:ii)),'r.-');
end
end
set(gca,'XTickLabel',{'Weak';'2';'3';'4';'5';'6';'7';'8';'9';'Robust'});
axis([0.8,10.2,0.0,1.5]),...
ylabel('Average return'),...
title('\rm Equally-weighted RMW returns, 1990-2015');hold off;
plots

You can use
axis square
xlim([1 10])
The first command makes the current axes region square (web) and the second set the x-axis limit.
Example:
subplot(1,2,1)
plot(1:10);
axis square
xlim([0 12]);
subplot(1,2,2)
plot(1:10);
axis square
xlim([1 10]);

Related

How to set a common y-axis label for different subplots in the same figure [duplicate]

I have used the following loop to get subplots:
for j=1:19;
Aj=B(j,:);
subplot(5,4,j);
plot(Aj,h)
end
For all these subplots, I need to have only one x-label and one y-label. How to do this? Also how to insert legend to all the subplots?
You can use suplabel from the FileExchange to have combined x and y label for all subplots.
Example:
subplot(1,2,1);
plot(randperm(40)); hold on; plot(randperm(40)); %Plotting some random data
legend('show') %To show the legend
subplot(1,2,2);
plot(randperm(40)); hold on; plot(randperm(40)); %Plotting some random data
legend('show') %To show the legend
%Using suplabel from the FileExchange to give a single x and y label for all subplots
suplabel('Combined X label','x');
suplabel('Combined Y label','y');
Output:
Sometimes you have to maximize the figure window to see the xlabel when using suplabel.

Plot in child (Matlab)

Can someone please tell me how does the last plot command work in following script?
close all;
s=tf('s');
sys1 = 5/(s+5);
sys2=exp(-1*s);
G=ss(sys1)*ss(sys2);
opts = bodeoptions('cstprefs');
opts.Grid= 'ON';
% create a figure and get the handle of the figure
figHnd = figure;
bode(G,opts)
% get and display the children handles of the figure
childrenHnd =get(figHnd, 'Children');
% select magnitude plot and plot a line
axes(childrenHnd(3));
hold on;
plot([1 1], [-20 20], 'r')
hold off;
I am trying to add a horizontal line for cut-off frequency to my Bode plot (magnitude diagram) but I can't figure out how to do that. The current code adds a vertical line for me.
The question is about the line
plot([1 1], [-20 20], 'r')
which is a simple plot command. In general, you use
plot(x,y)
here it is the same: the x-vector is [1, 1] and the y-vector is [-20, 20]. So you draw a line from the point (1,-20) to (1,20). The last part (r) only specifies the color, i.e. red. This is exactly what you can see in the bode plot.
To create a horizontal line e.g. from (10^-1, -20) to (10^0, -20) you can similarily draw
plot([10^-1, 10^0], [-20, -20], 'r');
(don't forget to put it within hold on; ... hold off;, so the bode plot isn't erased.
change the last plot code similar to this:
plot([1 10], [-20 -20], 'r')
Refer plot and work on some examples to get an idea of how it works.

Matlab - Legend does not match my graph

I am having trouble matching my graph with my axis. The first two plots work and the second two do not. I am trying to plot Temperature versus Pressure for two Argo floats and then Salinity versus Pressure. Here is my code:
% First Plot
subplot(221);
plot(float1winter.T,float1winter.P,'b');
hold on;
plot(float1summer.T,float1summer.P,'r');
hold on;
tempAdiff = abs(float1summer.T-float1winter.T)
plot(tempAdiff,float1summer.P,'--k');
hold on;
set(gca,'ydir','reverse');
title('Argo Float #1901440 Temp Profiles');
legend(['float1winter','float1summer','tempAdiff'],{'11-29-2013','07-01-2013','Temperature Difference'},'location','southwest');
xlabel('Temperature (°C)');
ylabel('Pressure');
shg;
% Second Plot
subplot(222);
plot(float2winter.S,float2winter.P,'m');
hold on;
plot(float2summer.S,float2summer.P,'c');
hold on;
set(gca,'ydir','reverse');
title('Argo Float #1901440 Salinity Profiles');
legend(['float2winter','float2summer'],{'11-29-2013','06-02-2013'},'location','southwest');
xlabel('Salinity (psu)');
ylabel('Presure');
shg;
% Third Plot
subplot(223);
% Matrix demensions did not agree bewteen winter and summer profiles. The summer profile was 71 x 2 and the winter was 70 x 2. I tried "reshape"
% and that didn't work. So I changed the vector of float3summer.T to
% float3bsummer.T with an array of 70 x 2
float3bsummer.T = float3summer.T(1:70,1:2);
float3bsummer.P = float3summer.P(1:70,1:2);
plot(float3winter.T,float3winter.P,'Linewidth',1,'color','blue');
hold on;
plot(float3bsummer.T,float3bsummer.P,'Linewidth',1,'color','red');
hold on;
tempdiff = abs(float3bsummer.T-float3winter.T)
plot(tempdiff,float3bsummer.P,'--k');
hold on;
set(gca,'ydir','reverse'); % this line reverses the y-axis so that depth increases downward
title('Argo Float #1901415 Tempearture Profiles');
hold on;
summerfloat = plot(float3bsummer.T,float3bsummer.P,'r');
legend(['float3winter.T','summerfloat','tempdiff'],{'12-03-2013','07-03-2013','Temp Diff'},'location','southeast');
xlabel('Temperature (°C)');
ylabel('Pressure');
axis ([-1,4,0,2000]);
shg;
% Fourth Plot
subplot(224);
plot(float3winter.S,float3winter.P,'g');
% Changed matrix dimensions for Salinity of Summer
float3bsummer.S = float3summer.S(1:70,1:2);
float3bsummer.P = float3summer.P(1:70,1:2);
plot(float3bsummer.S,float3bsummer.P,'.b');
hold on;
set(gca,'ydir','reverse');
title('Argo Float #1901415 Salinity Profiles');
h4 = plot(float3winter.S,float3winter.P,'g');
hold on;
h5 = plot(float3bsummer.S,float3bsummer.P,'.b');
hold on;
legend(['float3winter','float3bsummer.T'],{'12-03-2013','07-03-2013'},'location','southwest');
xlabel('Salinity (psu)');
ylabel('Pressure');
axis ([33.8,34.8,0,2000]);
shg;
% Save File to Desktop
set(gcf,'color','w');
saveas(gcf,'~/Desktop/hw1_figure1.pdf','pdf');![enter image description here][1]
I guess that you're trying to associate the set of strings for your legend, {'11-29-2013','07-01-2013','Temperature Difference'}, with the plots made from the variables ['float1winter','float1summer','tempAdiff'].
However, this isn't how legend works. MATLAB has no way of associating the plot produced by plot(float1winter.T,float1winter.P,'b'); to the string float1winter. If you want to specify which plots go with which legend entries, you need to pass the object handles of the plots to legend, which is easiest done by returning the handles when you plot originally:
h(1) = plot(float1winter.T,float1winter.P,'b');
hold on;
h(2) = plot(float1summer.T,float1summer.P,'r');
h(3) = plot(tempAdiff,float1summer.P,'--k');
legend(h,{'11-29-2013','07-01-2013','Temperature Difference'});
Side note: you only need to call hold on once per axis - so once for each subplot but not after every plot call.
Alternatively, you can not give handles at all; legend will assign the text to the plots in the order that they were plotted:
legend({'11-29-2013','07-01-2013','Temperature Difference'})
Understanding graphics handles allows you a lot more control over plots, especially if you might want to make small adjustments to them. For example, if I decide that I want the first plot to be green rather than blue, then I can just do:
set(h(1),'Color','g');
This will change the plot color and the legend changes to match automagically. To see a list of all the properties of an object, use get with only the handle. You can set more than one property at a time. For example:
get(h(1))
set(h(1),'DisplayName','Winter','LineWidth',3,'Marker','x')

Matlab: geoshow's grid and frame

I'm currently trying to plot a map from a .shp file and the result is not aesthetically pleasing:
minx=-75;
maxx=-68;
miny=-40;
maxy=-30;
cntry02=shaperead('cntry02', 'UseGeoCoords', true);
figure
geoshow(cntry02, 'FaceColor', [1 1 1]);
axis([minx-1 maxx+1 miny-1 maxy+1])
grid on
which produces
Is there a way to
1) plot all the grid (over the countries)?
2) plot the entire frame?
3) display S and W, instead of negative values of latitude and longitude?
I've been fighting with this problem all the morning. Thanks in advance.
You can download the .shp file from here.
http://openmap.bbn.com/svn/openmap/trunk/share/data/shape/cntry02/
For problem 1 and2, the reason is that the axes are always behind the plot. So one solution is to add new axes on the current one and display grid, box, and customized ticks.
For problem 3, I use regexprep to replace negative latitude with S suffix (idem for longitude). The only problem I have is that longitude 0 will be 0E, and latitude 0, 0N.
And here is the code:
figure;
axes;
geoshow(cntry02, 'FaceColor', [1 1 1]);
axis([minx-1 maxx+1 miny-1 maxy+1]);
axis off;
hold on; %hold to add new axes
axes('Color','none'); %specify no background, else default is here white
axis([minx-1 maxx+1 miny-1 maxy+1]);
grid on;
box on;
set(gca,'XTick', minx-1:2:maxx+1);
%compute x tick labels
xticks = num2str(minx-1:2:maxx+1);
xticks = regexprep(regexprep(xticks,'-([\d.]+)','$1W'), '\b[\d\.]+','$0E');
xticks_cell = cellstr(regexp(xticks,'\s+','split'));
set(gca,'XTickLabel',xticks_cell)
set(gca,'YTick', miny-1:2:maxy+1);
% compute y tick labels
yticks = num2str(miny-1:2:maxy+1);
yticks = regexprep(regexprep(yticks,'-([\d.]+)','$1S'), '\b[\d\.]+','$0N');
yticks_cell = cellstr(regexp(yticks,'\s+','split'));
set(gca,'YTickLabel',yticks_cell)

Common colorbar for scatter plots in Matlab with small numbers

I want to have each subplot share the same colorbar scale. I know caxis works for integers greater than 1, but there appears to be a problem using caxis with values such as 0.001.
x = 0:1:10;
y = 0:1:10;
z1 = .1:-.01:0;
z2 = .01:-.001:0;
figure;
subplot(1,2,1);
scatter(x,y,10,z1); colorbar;
subplot(1,2,2);
scatter(x,y,10,z2); colorbar;
Now I want to have the scatter subplots to have a common colorbar scale. I tried to use caxis, however I do not get the desired results. The left axis does not scale correctly, as they are all brown. How can this be corrected?
ca = [0 .01];
figure;
subplot(1,2,1);
scatter(x,y,10,z1); caxis(ca); colorbar;
subplot(1,2,2);
scatter(x,y,10,z2); caxis(ca); colorbar;
What you're seeing is the correct behaviour of caxis. When you set caxis([0 0.01]), all values greater than 0.01 are assigned the colour red (or brown, whatever you call it). In z1, all except the last point are greater than 0.01 and so they're all marked in red. If you tried caxis([0 0.1]), you'll see that the plot on the right is all blue.The dynamic range of your two subplots are an order of magnitude apart and so, you won't be able to represent both adequately with the same caxis limits.
Have you tried using a logarithmic color scale? Try the following:
subplot(1,2,1);
scatter(x,y,10,log10(z1)); colorbar;
caxis([-3 -1])
subplot(1,2,2);
scatter(x,y,10,log10(z2)); colorbar;
caxis([-3 -1])
Does the above plot look better?