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

11 Ansichten (letzte 30 Tage)

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;

Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 29 Mai 2018
Bearbeitet: Ameer Hamza am 29 Mai 2018
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 Kommentare

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Greg
Greg am 29 Mai 2018
Bearbeitet: Greg am 29 Mai 2018
It isn't elegant, but space pad the tick label until it shows up better.
  2 Kommentare
rai7aeLa
rai7aeLa am 29 Mai 2018
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 am 29 Mai 2018
Yes, it is right justified to the Y axis. You'd had to right-pad until the text moves to the left.

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by