mark point on figure - matlab

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

Related

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:

Multiple y axes in MATLAB (axis alignment issue when printing)

Here is a sample of a strange problem. I'd like to plot curves with multiple y axes and I use a fairly common method in MATLAB.
function plot_test()
clear;
savefile = 1;
scrsz = get(0,'ScreenSize');
figure('Position',[1 1 0.9 * scrsz(3) 0.9 * scrsz(4)]);
hold on;
box on;
x = 0:1:10;
h1 = plot(x, x.^2 , 'r', 'LineWidth', 2.5);
%Axis label
xlabel('XLabel', 'FontSize', 20, 'Interpreter', 'latex');
ylabel('YLabel', 'FontSize', 20, 'Interpreter', 'latex');
set(gca, 'FontSize', 20, 'LineWidth', 3);
ax1 = gca;
ax2 = axes('Position',get(ax1,'Position'),'XAxisLocation','top','YAxisLocation','right','Color','none','XColor','none','YColor','k');
linkaxes([ax1 ax2],'x');
hold on
box on;
h2 = plot(x, x, 'b', 'Parent', ax2, 'LineWidth', 2.5);
ylabel('Second YLabel', 'FontSize', 20, 'Interpreter', 'latex');
set(gca, 'FontSize', 20, 'LineWidth', 3);
hl=legend([h1 h2],{'First Line','Second Line'});
set(hl,'FontSize',15,'Location','Northwest', 'Orientation','Vertical')
%Save pdf
if savefile
% Backup previous settings
prePaperType = get(gcf,'PaperType');
prePaperUnits = get(gcf,'PaperUnits');
preUnits = get(gcf,'Units');
prePaperPosition = get(gcf,'PaperPosition');
prePaperSize = get(gcf,'PaperSize');
% Make changing paper type possible
set(gcf,'PaperType','<custom>');
% Set units to all be the same
set(gcf,'PaperUnits','inches');
set(gcf,'Units','inches');
% Save the pdf
print -dpdf Test.pdf;
% Restore the previous settings
set(gcf,'PaperType',prePaperType);
set(gcf,'PaperUnits',prePaperUnits);
set(gcf,'Units',preUnits);
set(gcf,'PaperPosition',prePaperPosition);
set(gcf,'PaperSize',prePaperSize);
end
The objective is to print a PDF of the figure and save it in the same folder as Test.pdf. This is accomplished but the axes are misaligned. On my Windows machine it looks horrible while on a Mac it looks almost okay (but if you look closely, the y-axes are indeed misaligned at the bottom).
This only happens when I use a second axis. Without that, all this runs perfectly. Any idea why?
Okay, so I found a way: The trick is to use plotyy. Sample code below
function plot_test2()
clear;
savefile = 1;
scrsz = get(0,'ScreenSize');
figure('Position',[1 1 0.9 * scrsz(3) 0.9 * scrsz(4)]);
hold on;
box on;
x=(0:1:10);
y1=x;
y2=x.^2;
[hAx, hLine1, hLine2] = plotyy(x,y1,x,y2);
%Axis label
xlabel(hAx(1),'XLabel', 'FontSize', 20, 'Interpreter', 'latex', 'Color', 'k');
ylabel(hAx(1),'YLabel', 'FontSize', 20, 'Interpreter', 'latex', 'Color', 'k');
ylabel(hAx(2),'Second YLabel', 'FontSize', 20, 'Interpreter', 'latex');
set(hAx,{'ycolor'},{'k';'k'})
set(hAx,{'FontSize'},{20;20}, {'LineWidth'}, {3;3})
set(hLine1,'LineWidth', 3)
set(hLine2,'LineWidth', 3)
set(hLine1,'Color', 'r')
set(hLine2,'Color', 'b')
hl=legend([hLine1 hLine2],{'First Line','Second Line'});
set(hl,'FontSize',15,'Location','Northwest', 'Orientation','Vertical')
%Save pdf
if savefile
% Backup previous settings
prePaperType = get(gcf,'PaperType');
prePaperUnits = get(gcf,'PaperUnits');
preUnits = get(gcf,'Units');
prePaperPosition = get(gcf,'PaperPosition');
prePaperSize = get(gcf,'PaperSize');
% Make changing paper type possible
set(gcf,'PaperType','<custom>');
% Set units to all be the same
set(gcf,'PaperUnits','inches');
set(gcf,'Units','inches');
% Save the pdf
print -dpdf Test.pdf;
% Restore the previous settings
set(gcf,'PaperType',prePaperType);
set(gcf,'PaperUnits',prePaperUnits);
set(gcf,'Units',preUnits);
set(gcf,'PaperPosition',prePaperPosition);
set(gcf,'PaperSize',prePaperSize);
end

'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;