plot in all the subplots at the same time - matlab

I have a figure with 2 subplots.
I would like to know if it's possible (and how) to draw the same plots in all the subplots at the same time.
For example in the following plot I'd like to plot (x,y) simultaneously and then proceed separately.
fig1 = figure
subplot(2,1,1)
plot(x,y)
hold on
subplot(2,1,2)
plot(x,y)
hold on
subplot(2,1,1)
plot(x,z)
subplot(2,1,2)
plot(x,k)

You can do it with set using cell arrays as follows. See the documentation for details.
subplot(2,1,1);
h1 = plot(x,y); %// get a handle to the plot
subplot(2,1,2)
h2 = plot(x,y); %// get a handle to the plot
set([h1; h2], {'xdata'}, {x1; x2}, {'ydata'}, {y1; y2})
%// new values: x1 x2 y1 y2

If you're asking because you want to plot the same plot behind say 16 subplots, then you could do it in a loop:
for k= 1:16
s(k) = subplot(4,4,k);
plot(x,y);
hold on;
end
If you just want it for 2 subplots then there is nothing wrong with your current code

Related

Matlab: Plots and Subplots - How do I make Matlab zoom into all the subplots simultaneously?

I have multiple sub-plots. When I zoom into the first subplot to view a certain data set Matlab does not zoom the rest of the subplots. How do I make Matlab zoom into all subplots simultaneously?
subplot(2,1,1)
hold on
plot(Tim1.in,IA1.raw32SPC,'b-')
hold off
subplot(2,1,2)
hold on
plot(Tim1.in,IA1.cos,'r-*')
hold off
Since the x variable is the same, It makes sense to just link the x axes in this case. I added some code to create the x and y variables so that anyone can run the code below. You were also using hold unnecessarily.
x = 1:10;
y1 = 3*x;
y2 = x.^2;
ax(1) = subplot(2,1,1);
plot(x, y1, 'b-')
ax(2) = subplot(2,1,2);
plot(x, y2, 'r-*')
linkaxes(ax, 'x')

How to have a common label for all x and y axes in case of subplots?

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.

colorbar eastoutside vs westoutside

I put two colorbars in the same image using this submission on filexchange.
The position of the first colorbar is set by:
colorbar('WestOutside')
the position of the second one by:
colorbar('EastOutside')
Does anyone know why the first one is longer?
When looking at the Matlab documentation it seemed to me that they should be the same. What am I missing?
The skeleton of the code is the following:
%define coordinates of the nodes
theta=linspace(0,2*pi,33);
[x,y]=pol2cart(theta,1);
%define colormap of the links
cm = winter;
colormap(cm);
%plot the links
for ii=1:N
quiver(...)
end
%place the first colorbar
hcb=colorbar('EastOutside');
%freeze the first colorbar
cbfreeze(hcb);
%define the second colormap
cm = autumn;
colormap(cm);
%plot the dots
for ii=1:N
plot(...)
end
%place the second colorbar
hb=colorbar('EastOutside');
The key is to use two different axes. Based on your code:
%define coordinates of the nodes
theta=linspace(0,2*pi,33);
[x,y]=pol2cart(theta,1);
%plot the links
close
figure;
for ii=1:100
quiver(x,y)
end
%define colormap of the links
ax1 = gca;
colormap (ax1,winter)
%place the first colorbar
hcb=colorbar(ax1,'EastOutside');
%this is tentative, just to get the axes right position:
hb=colorbar(ax1,'WestOutside');
pos = ax1.Position;
hb.delete
% second colorbar
ax2 = axes;
colormap (ax2,autumn)
hb=colorbar(ax2,'WestOutside');
ax2.Position = pos;
axis off
ax1.Position = pos;
It creates this:
Using MATLAB 2015a.

Multiple y plots on same logarithmic scale for the x-axis

I am trying to plot 3 curves using the semilogx matlab function and add a fourth line to an additional y axis on the right. All of them should be plotted on the same logarithmic scale for the x-axis. The following code indicates the derived error; the x-axis is incorrect. The figure has to have a single x-axis mode of ticks, the logarithm one. How could this be fixed?
Plus, how can I add a legend for these 4 curves?
close all, clc
figure, semilogx([1:100:1000],[rand(1,10)],'bo-'),
xlabel('xlabel'),ylabel('ylabel'), hold on;
semilogx([1:100:1000], [rand(1,10)], 'ro-'), hold on,
semilogx([1:100:1000], [rand(1,10)], 'ko-'), hold off
legend('1','2','3','Location','Best')
ax1 = gca;
ax2 = axes('YAxisLocation','right',...
'Color' , 'none',...
'YColor', 'm');
linkaxes([ax1 ax2 ], 'x')
x4 = [1:100:1000];
y4 = [rand(1,10)*2];
line(x4, y4, 'color', 'm', 'Marker','x','LineStyle',':', 'parent',ax1)
ylabel('y2')
You can use plotyy function to plot two of your lines, one on the right and one on the left. You can then hold on and plot the remaining lines using semilogx.
plotyy([1:100:1000], [rand(1,10)], [1:100:1000], [rand(1,10)]*2, #semilogx);
hold on;
semilogx([1:100:1000], [rand(1,10)], 'ro-');
semilogx([1:100:1000], [rand(1,10)], 'mo-');
hold off;
legend('Line1','Line2','Line3','Line4','Location','Northwest')

how to fill colors between contours in matlab

I have plotted multiple contours in matlab with the hold on command, using the contour function of MatLab. How can I go ahead if I want to fill color between the first and the last contour. I tried contourf function but it didnt work out that way.
Thanks in advance.
I have written two simple lines which plots the zero level set contour after every iterations.
hold on;
contour(X,Y,phi,[0,0],'r');
This can be done by using the get command to get individual components from the graph. For example:
[x, y, z] = peaks; % Generate some data
figure; surf(x, y, z); % Show
figure;[c, h] = contourf(x, y, z, [0 0]); % Build and show contour plot
patches = get(h, 'children'); % Get different patches
set(patches(1), 'facecolor', 'r') % Colour one red