'hold on' not working for Multi-position subplot in matlab - matlab

I am animating some subplots in MATLAB (R2015a). However, when I attempted to position a subplot to take up multiple positions the hold on command no longer works.
Here is a simplified form of my problem:
clear
N = 20;
Analysis(:,1) = linspace(1,N,N);
Analysis(:,2:5) = randi([1, 20],20,4);
for n = 1:N;
subplot(2,2,[1,2]);
title('Particle counts at step number');
plot(Analysis(n,1), Analysis(n,2), '.', 'Markersize', 8, 'color', 'red'), hold on;
plot(Analysis(n,1), Analysis(n,3), '.', 'Markersize', 8, 'color', '[0,0.5,0]');
legend({'Methane','Oxygen'},'FontSize',8,'FontWeight','bold', 'Location', 'northeastoutside');
xlim([0,N]);
ylim([0, 20]);
subplot(2,2,[3,4]);
title('Temperature(k) and Pressure');
plot(Analysis(n,1), Analysis(n,4), '.', 'Markersize', 8, 'color', 'red'), hold on;
plot(Analysis(n,1), Analysis(n,5), '.', 'Markersize', 8, 'color', 'blue');
legend({'Temperature','Pressure'},'FontSize',8,'FontWeight','bold', 'Location', 'northeastoutside');
xlim([0,N]);
ylim([0, 20]);
pause(0.1);
drawnow;
end
The hold on command appears to work again when i remove the legend or change the position of the subplot to be singular but i need it to work with both.

As I said above, this does appear to be a bug. One possible workaround is to modify the XData and YData properties of your line objects:
For example:
N = 20;
Analysis(:,1) = linspace(1,N,N);
Analysis(:,2:5) = randi([1, 20],20,4);
subplot(2,2,[1,2]);
title('Particle counts at step number');
hold on;
ph(1) = plot(Analysis(1,1), Analysis(1,2), '.', 'Markersize', 8, 'color', 'red');
ph(2) = plot(Analysis(1,1), Analysis(1,3), '.', 'Markersize', 8, 'color', '[0,0.5,0]');
hold off;
legend({'Methane','Oxygen'},'FontSize',8,'FontWeight','bold', 'Location', 'northeastoutside');
xlim([0, N]);
ylim([0, 20]);
subplot(2,2,[3,4]);
title('Temperature(k) and Pressure');
hold on;
ph(3) = plot(Analysis(1,1), Analysis(1,4), '.', 'Markersize', 8, 'color', 'red');
ph(4) = plot(Analysis(1,1), Analysis(1,5), '.', 'Markersize', 8, 'color', 'blue');
hold off;
legend({'Temperature','Pressure'},'FontSize',8,'FontWeight','bold', 'Location', 'northeastoutside');
xlim([0 ,N]);
ylim([0, 20]);
for n = 2:N;
k = 2;
for ii = 1:4
ph(ii).XData = Analysis(1:n, 1);
ph(ii).YData = Analysis(1:n, k);
k = k + 1;
end
pause(0.1);
drawnow;
end
I believe this gives you what you're looking for. Not the prettiest but it's functional.

Related

grouped bar chart with 2 y-axes is displayed as stacked bar

I would like to plot the grouped bar chart for the comparison of three methods. I tried the following code and it is displayed as stacked bar chart.can you please help
dice =[0, 3, 5];
no_of_region=[42, 12, 5];
figure;
bar(dice',.2,'grouped','FaceColor',[0 .5 .5],'EdgeColor',[0 .9 .9],'LineWidth',1.5)
ylabel('Dice Similarity index')
yyaxis right
bar(no_of_region, .2,'grouped','EdgeColor', 'r', 'LineWidth', 2);
legend('Dice Similarity Index','Number of regions')
legend('Location','northwest')
XTickLabel={'a' ; 'b';'c'};
XTick=[1 2 3]
set(gca, 'XTick',XTick);
set(gca, 'XTickLabel', XTickLabel);
set(gca, 'XTickLabelRotation', 45);
xlabel('Different Methods', 'Fontsize', 16)
ylabel('Number of Regions', 'Fontsize', 16)
title('Comparison of Algorithms', 'fontsize', 20);
set(gcf,'color','w');
OUTPUT I GOT AS:
One easy solution is to shift the bars left and right by half of their width (or more if you would like space between them). Here is the new code, which is also cleaned a bit stylistically:
% parameters
dice = [0, 3, 5];
no_of_region = [42, 12, 5];
X = [1, 2, 3]; % common x values for plot
bar_width = 0.2; % width of bars
% initialize figure
figure;
% left plot
bar(X.' - bar_width/2, dice', bar_width, 'grouped', ...
'FaceColor', [0 .5 .5], ...
'EdgeColor', [0 .9 .9], ...
'LineWidth', 1.5);
ylabel('Dice Similarity index');
% right plot
yyaxis right;
bar(X.' + bar_width/2, no_of_region, bar_width, 'grouped', ...
'EdgeColor', 'r', ...
'LineWidth', 2);
% plot formatting
legend({'Dice Similarity Index', 'Number of regions'}, ...
'Location', 'northwest');
XTickLabel = {'a' ; 'b'; 'c'};
XTick = X;
set(gca, ...
'XTick', XTick, ...
'XTickLabel', XTickLabel, ...
'XTickLabelRotation', 45);
xlabel('Different Methods', 'FontSize', 16);
ylabel('Number of Regions', 'FontSize', 16);
title('Comparison of Algorithms', 'FontSize', 20);
set(gcf, 'color', 'w');
Depending on desired behavior, you also might consider replacing figure; by clf;, which clears the existing figure or opens a new figure window if there is not already one open.

multiple plot in one plot in MATLAB using hold on/off statements

How can I fix this so that it would show all three plots in one plot?
I followed the instruction in this SO answer but didn't work out: https://stackoverflow.com/a/8773571/2414957
figure;
t = -pi:0.01:pi;
a = sin(t);
plot(t, a, 'r', 'DisplayName', 'a'); hold on;
fhat = (21./(8*pi.^10))*(33*pi.^4-3465*pi.^2+31185)*t.^2 +(3750*pi.^4 -30*pi.^6 -34650*pi.^2)*t.^3 +(5*pi^8-765*pi.^6+7425*pi.^4)*t;
plot(t, fhat, 'c', 'DisplayName', 'fhat');
hold on;
p = t - (t.^3)/factorial(3) + (t.^5)/factorial(5);
plot(t, p, 'b', 'DisplayName', 'p');
hold on;
title('Sine plot by sin(t)');
xlabel('t');
ylabel('sin(t)');
legend('show');
The 3 plots have variant range, therefore, you need to make normalization to plot all functions on the same space
figure;
t = -pi:0.01:pi;
a = sin(t);p = t - (t.^3)/factorial(3) + (t.^5)/factorial(5);
fhat = (21./(8*pi.^10))*(33*pi.^4-3465*pi.^2+31185)*t.^2 +(3750*pi.^4 -30*pi.^6 -34650*pi.^2)*t.^3 +(5*pi^8-765*pi.^6+7425*pi.^4)*t;
%display
plot(t, p/norm(p), 'b', 'DisplayName', 'p');
hold on; %you need only one 'hold on'
plot(t, a/norm(a), 'r', 'DisplayName', 'a');
plot(t, fhat/norm(fhat), 'c', 'DisplayName', 'fhat');
title('Sine plot by sin(t)');
xlabel('t');
ylabel('sin(t)');
legend('show');
You are plotting all three functions. What happens is that p is drawn over a. They are both very small compared to that, and therefore fall on the same pixels on your screen.
To verify this you can zoom in:
set(gca,'ylim',[-1.5,1.5])
Alternatively, plot p using dots or dashes, so the other line shows through in between:
figure;
t = -pi:0.01:pi;
a = sin(t);
plot(t, a, 'r', 'DisplayName', 'a'); hold on;
fhat = (21./(8*pi.^10))*(33*pi.^4-3465*pi.^2+31185)*t.^2 +(3750*pi.^4 -30*pi.^6 -34650*pi.^2)*t.^3 +(5*pi^8-765*pi.^6+7425*pi.^4)*t;
plot(t, fhat, 'c', 'DisplayName', 'fhat');
hold on;
p = t - (t.^3)/factorial(3) + (t.^5)/factorial(5);
plot(t, p, 'b--', 'DisplayName', 'p'); % Note the 'b--' line format here!
hold on;
title('Sine plot by sin(t)');
xlabel('t');
ylabel('sin(t)');
legend('show');

imagesc with multiple axis and ticklines

I have a [12 x 6] matrix A that I represent with imagesc and on which I impose some thick tick-lines:
figure
imagesc(A)
set(gca,'xtick', linspace(0.5,size(A,2)+0.5,C+1),...
'ytick', linspace(0.5,size(A,1)+0.5,B*al+1),'linewidth',3);
set(gca,'xgrid', 'on', 'ygrid', 'on', 'gridlinestyle', '-', 'xcolor',
'k', 'ycolor', 'k');
set(gca, 'XTickLabelMode', 'manual', 'XTickLabel', [],'YTickLabel', []);
colorbar
I then want to impose some thinner tick-lines to split in two each box delimited by the thicker lines:
ax1 = gca;
ax1_pos = get(ax1,'Position'); % position of first axes
ax2 = axes('Position',ax1_pos,...
'XAxisLocation','top',...
'YAxisLocation','right',...
'Color','none');
set(gca,'xtick', linspace(0.5,size(A,2)+0.5,2*C+1),'linewidth',1);
The result is clearly not satisfying. How could I fix it? I thought that 'Color','none' would have done the trick...
Instead of trying to modify tick lengths and adding a second axes, I would just plot some extra lines on top of your image. This should achieve what you want:
% Some sample data:
A = rand(12, 6);
% Plot image:
imagesc(A);
set(gca, 'Visible', 'off');
hold on;
colorbar;
% Plot horizontal lines:
yLines = repmat((0:size(A, 1))+0.5, 2, 1);
xLines = repmat([0.5; size(A, 2)+0.5], 1, size(yLines, 2));
line(xLines, yLines, 'LineWidth', 3, 'Color', 'k');
% Plot thick vertical lines:
xLines = repmat((0:2:size(A, 2))+0.5, 2, 1);
yLines = repmat([0.5; size(A, 1)+0.5], 1, size(xLines, 2));
line(xLines, yLines, 'LineWidth', 3, 'Color', 'k');
% Plot thin vertical lines:
xLines = repmat((1:2:size(A, 2))+0.5, 2, 1);
yLines = repmat([0.5; size(A, 1)+0.5], 1, size(xLines, 2));
line(xLines, yLines, 'LineWidth', 1, 'Color', 'k');
And here's the plot:

How can I make a contour plot doesn't overlap on the line plot in one axes?

I've made 2 plot in one axes using pushbutton in matlab guide, the first plot is line plot
here is the plot
http://i1275.photobucket.com/albums/y443/Kaito_Aokage/Capture2_zpsbc76be37.png?t=1403148417
code for line plot
% X
for i = 1.5:7;
cur_x = i * 3.8;
line([cur_x, cur_x], [0 5], 'color', 'r', 'LineWidth', 1.5);
drawnow;
end;
% Y
for i = 2:7;
cur_y = i * 4;
line([0 4],[cur_y, cur_y], 'color', 'r', 'LineWidth', 1.5);
drawnow;
end;
% X2
for i = 1.5:7;
cur_x2 = i * 3.8;
line([cur_x2, cur_x2], [25 31], 'color', 'r', 'LineWidth', 1.5);
drawnow;
end;
% Y2
for i = 1:8;
cur_y2 = i * 3.5;
line([26 31],[cur_y2, cur_y2], 'color', 'r', 'LineWidth', 1.5);
drawnow;
end;
% X
line( [5.7 cur_x], [5 5], 'color', 'r', 'LineWidth', 1.5);
% Y
line( [4 4], [8 cur_y], 'color', 'r', 'LineWidth', 1.5);
% X2
line( [5.6 cur_x2], [25 25], 'color', 'r', 'LineWidth', 1.5);
% Y2
line( [26 26], [3.5 cur_y2], 'color', 'r', 'LineWidth', 1.5);
handles.axes2;
grid on;
hold on;
axis([0 30 0 30]);
and the second plot is contour plot
http://i1275.photobucket.com/albums/y443/Kaito_Aokage/Capture3_zpsfd46dedf.png?t=1403148576
code for contour plot
xMove = 3;
yMove = 10;
r = 30;
rx = -r:0.1:r;
ry = r:-0.1:-r;
[x_coor, y_coor] = meshgrid(rx, ry);
radius = sqrt(x_coor.^2+y_coor.^2);
contourf(x_coor + xMove, y_coor + yMove, radius,'edgecolor','none');
xlabel('Widht');
ylabel('Long');
axis([0 30 0 30]);
colorbar;
caxis([0 10]);
grid on;
handles.axes2;
set(gca,'layer','top');
hold on;
Pushbutton Floor is line plot and Pushbutton AP1 is contour plot. When i try to push plot contour button after line plot button, the line plot overlap by contour plot. I want line plot doesn't overlap by contour plot so the line plot can be seen after i push contour plot button. I already try holdor set(gca,'layer','top)but its not working. what should i do?
in what order are you executing the above codes ? I first executed second code then the first code and this is my output
Here is the whole code I executed, I had to remove the line handle.axis2; as it was throwing an error.( I am using matlab 2011)
close all
xMove = 3;
yMove = 10;
r = 30;
rx = -r:0.1:r;
ry = r:-0.1:-r;
[x_coor, y_coor] = meshgrid(rx, ry);
radius = sqrt(x_coor.^2+y_coor.^2);
contourf(x_coor + xMove, y_coor + yMove, radius,'edgecolor','none');
xlabel('Widht');
ylabel('Long');
axis([0 30 0 30]);
colorbar;
caxis([0 10]);
grid on;
set(gca,'layer','top');
hold on;
% X
for i = 1.5:7;
cur_x = i * 3.8;
line([cur_x, cur_x], [0 5], 'color', 'r', 'LineWidth', 1.5);
drawnow;
end;
% Y
for i = 2:7;
cur_y = i * 4;
line([0 4],[cur_y, cur_y], 'color', 'r', 'LineWidth', 1.5);
drawnow;
end;
% X2
for i = 1.5:7;
cur_x2 = i * 3.8;
line([cur_x2, cur_x2], [25 31], 'color', 'r', 'LineWidth', 1.5);
drawnow;
end;
% Y2
for i = 1:8;
cur_y2 = i * 3.5;
line([26 31],[cur_y2, cur_y2], 'color', 'r', 'LineWidth', 1.5);
drawnow;
end;
% X
line( [5.7 cur_x], [5 5], 'color', 'r', 'LineWidth', 1.5);
% Y
line( [4 4], [8 cur_y], 'color', 'r', 'LineWidth', 1.5);
% X2
line( [5.6 cur_x2], [25 25], 'color', 'r', 'LineWidth', 1.5);
% Y2
line( [26 26], [3.5 cur_y2], 'color', 'r', 'LineWidth', 1.5);
grid on;
axis([0 30 0 30]);
hold off;

mark point on figure

From this Matlab code :
a=[1:.001:5] ;
f=(1./a)-(a-1) ;
plot(a,f)
I want to mark the point when (f==0) on the figure, assuming the value of a is unknown and I should take it from the figure.
I want it to look like this:
Use the command 'text' http://www.mathworks.com/help/matlab/ref/text.html as follows,
[~,idx] = find(abs(f)<1e-3);
text( a(idx(1)), f(idx(1)), 'here we touch/cut/cross x-axis')
You can use interp1 to find the point where f = 0;
a_for_f_equal_zero = interp1(f, a, 0);
line(a_for_f_equal_zero, 0, 'marker', 'o', 'color', 'r', 'linestyle', 'none')
x_lim = get(gca, 'XLim');
y_lim = get(gca, 'YLim');
line(a_for_f_equal_zero * [1,1], [y_lim(1), 0], 'color', 'k') % vertical line
line([x_lim(1), a_for_f_equal_zero], [0,0], 'color', 'k') % horizontal line