imagesc with multiple axis and ticklines - matlab

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:

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.

'hold on' not working for Multi-position subplot in 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.

Plot second y axis using plot and fill (without plotyy)

Here is my code
clear all;clc
x = linspace(0, 10, 100);
axes('Position', [.075,.075,.9,.2], ...
'XColor', 'k', ...
'YColor', [0 0 0]);
fill(x, circshift(sin(x), 50), 'red', 'EdgeColor','None');
ylabel('Fancy Sine', 'FontSize', 11);
% Adding this the upper vanishes and the y axes is on left not right side
axes('Position', [.075,.075,.9,.2], ...
'XColor', 'k', ...
'YAxisLocation', 'Right', ...
'Color', 'none');
plot(x, x, 'Color', [.2 .4 .8]);
ylabel('Line Graph', 'FontSize', 11);
xlabel('X', 'FontSize', 11);
The single axis works fine
But when I want to add the second axis, the first disappears and the new axis is not on the right, but left side..
But both plots should be in one axis and the second y axis should be on the right side.
How to achieve this?
plot automatically sets the axis properties to the default. Use hold to stop this or specify the axis properties after your plot call.
An example of the former:
clear all;clc
x = linspace(0, 10, 100);
ax1 = axes('Position', [.075,.075,.9,.2], ...
'XColor', 'k', ...
'YColor', [0 0 0]);
p1 = fill(x, circshift(sin(x), 50), 'red', 'EdgeColor','None','Parent',ax1);
ylabel('Fancy Sine', 'FontSize', 11);
% Adding this the upper vanishes and the y axes is on left not right side
ax2 = axes('Position', [.075,.075,.9,.2], ...
'XColor', 'k', ...
'YAxisLocation', 'Right', ...
'Color', 'none');
hold on
p2 = plot(ax2,x, x, 'Color', [.2 .4 .8]);
hold off
ylabel('Line Graph', 'FontSize', 11);
xlabel('X', 'FontSize', 11);
Edit1: The reason you don't need to do this for your fill call is because it creates a patch object in your axes and does not invoke a plot command.

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