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

13 次查看(过去 30 天)

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 中查找有关 Grid Lines, Tick Values, and Labels 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by