필터 지우기
필터 지우기

How to show an individual YTickLabel to the right of the single Y axis?

조회 수: 10 (최근 30일)
rai7aeLa
rai7aeLa 2018년 5월 29일
댓글: Greg 2018년 5월 29일

Hello,

I would like to place an YTickLabel to the right of the Y axis. Currently, my figure looks like this:

Now, I would like to move the lower label $-M_{r0}$ (which is overlapping with the graph) to the right of the Y axis. How can I achieve that? Thanks!

Here's my MWE:

close all;
set(groot, 'defaultAxesTickLabelInterpreter','latex'); 
figure1=figure('Position',[320 50 600 450]);
axes1=axes('Parent',figure1,'FontSize',14);
o=linspace(-5,5,1000);
M=sign(o).*(10+o.^2);
plot1=plot(o,M,'Parent',axes1,'LineWidth',2);
set(plot1,'Color',[1 0 0]);
ylabel('Torque $M_r(\omega)$','FontSize',14,'Interpreter','latex');
xlabel('Velocity $\omega$','FontSize',14,'Interpreter','latex');
title('');
set(gca,'xtick',[],'xticklabel',[]);
set(gca,'ytick',[-10,10],'yticklabel',{'\fontsize{14pt}{17pt}$-M_{r0}$','\fontsize{14pt}{17pt}$M_{r0}$'});
set(gca,'XAxisLocation','origin','YAxisLocation','origin'); 
axp=get(gca,'Position'); % needed to put arrows in the right position
annotation('arrow', [axp(1) axp(1)+axp(3)],[axp(2)+axp(4)/2 axp(2)+axp(4)/2]);
annotation('arrow', [axp(1)+axp(3)/2 axp(1)+axp(3)/2],[axp(2) axp(2)+axp(4)]);
box off;

채택된 답변

Ameer Hamza
Ameer Hamza 2018년 5월 29일
편집: Ameer Hamza 2018년 5월 29일
Instead of using ylabel to write the labels, use annotation to place the labels at the required position. For example,
set(gca,'ytick',[],'yticklabel',[]); % remove the yticks
annotation('textbox', 'String', '\fontsize{14pt} -M_{r0}', 'Position', [0.5, 0.4 0.1 0.1], 'EdgeColor', [1 1 1])
annotation('textbox', 'String', '\fontsize{14pt} M_{r0}', 'Position', [0.5, 0.6 0.1 0.1], 'EdgeColor', [1 1 1])
You might need to experiment with the position property to get the desired position.
Edit: To keep the location of labels linked with your graph values, use the following statements
text('String', '\fontsize{14pt} -M_{r0}', 'Position', [1 -3])
text('String', '\fontsize{14pt} M_{r0}', 'Position', [1 3])
Here Position property specifies the x and y data value on the graph, not the normalized coordinates (0 to 1) as required by annotation(). So if the axis limit change, these labels will also change accordingly.
  댓글 수: 7

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Greg
Greg 2018년 5월 29일
편집: Greg 2018년 5월 29일
It isn't elegant, but space pad the tick label until it shows up better.
  댓글 수: 2
rai7aeLa
rai7aeLa 2018년 5월 29일
I have tried, but it does not work: Spaces are indeed added, but the position of the visible part (-Mr0) stays the same. Maybe the text is aligned to the right?
Greg
Greg 2018년 5월 29일
Yes, it is right justified to the Y axis. You'd had to right-pad until the text moves to the left.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by