klaus
klaus

Reputation: 113

Matlab - Move Y-Axis to origin?

I know that Matlab sometimes isn't the best tool to create "fancy" plots but since my university requires it I dont have much choice.

I want to move the Y-Axis and the "arrow"-Annotation of the following example to x=0.

X = -pi/2 : 0.001 : pi/2;
Y = cos(X).^2;
plot(X, Y,'Color',[0,0,1]);

ylim([0 1.2]);
set(gca, 'YTick',[1.2],'yticklabel',{'{\color[rgb]{0,0,1}X(f)}'});
set(gca, 'XTick',[-pi/2,pi/2],'xticklabel',{'-f_{max}' 'f_{max}'});

set(gca,'fontsize',16);
box off;
grid off;
fig_pos=get(gca,'Position');
xp1=fig_pos(1);
xp2=fig_pos(1)+fig_pos(3)+0.02;
yp1=fig_pos(2);
yp2=fig_pos(2)+fig_pos(4)+0.03;
a1=annotation('arrow', [xp1 xp2],[yp1 yp1]);
a2=annotation('arrow', [xp1 xp1],[yp1 yp2]);

enter image description here

I tried to use PlotAxesAtOrigin and axescenter of the FileExchange but due to the annotations this doesn't work properly.

Does anyone know a way to make this work?

Thanks for your help, Klaus!

Upvotes: 3

Views: 2473

Answers (2)

Phil Goddard
Phil Goddard

Reputation: 10762

Moving the y-axis arrow annotation is straight forward. Simply replace your last line with

a2=annotation('arrow', fig_pos(1)+fig_pos(3)/2*[1 1],[yp1 yp2]);

Moving the X(f) is slightly more problematic as you can no longer use the Ytick labels. Replace your 5th line with

set(gca, 'YTick','');

and add the following line

text(0.1,1.2,'{\color[rgb]{0,0,1}X(f)}','FontSize',16);

at the end of the code.

There's no way to remove the black vertical line that is still on the left side of the axes, so you need to mask it with another annotation. Something like

annotation('line',fig_pos(1)*[1 1],[fig_pos(2) fig_pos(2)+fig_pos(4)],...
'Color',get(gcf,'Color'),'LineWidth',2);

would do.

That gives

enter image description here

A final note is that you create a variable called fig_pos. That is misleading as it contains the axis position (on the figure), not the figure position (which is its position relative to the lower left corner of your monitor). Your variable should really be called axis_pos.

Upvotes: 1

Mina
Mina

Reputation: 11

You can use "axis" command, if you know the value of fmax. axis([xmin xmax ymin ymax])

Upvotes: 0

Related Questions