Filter löschen
Filter löschen

how to move text away from axis?

10 Ansichten (letzte 30 Tage)
Minka Califf
Minka Califf am 10 Jun. 2018
Beantwortet: Gautam am 29 Aug. 2024
I wanted to rotate my y axis label but now it is overlapping with the y axis. Is there a way that I can move the text (only for the y axis on the right) away from the axis?
%%f2=figure(2);
left_color = [1 0 0];
right_color = [0 0 1];
set(f2,'defaultAxesColorOrder',[left_color; right_color]);
yyaxis right
plot(x,CA2,'b','LineWidth',2)
hold on
plot(x,NY2,'-','MarkerFaceColor','b','LineWidth',2)
plot(x,FL2,'-','MarkerFaceColor','b','LineWidth',2)
ylabel('Percent Unsheltered','fontsize', 14);
set(get(gca,'YLabel'),'Rotation',-90)
yyaxis left
p1=plot(x,CA1/1000,'r')
hold on
p2=plot(x,NY1/1000,'r-^','MarkerFaceColor','r')
p3=plot(x,FL1/1000,'r-o','MarkerFaceColor','r')
legend([p1 p2 p3],{'\color{red} CA','\color{red} NY','\color{red} FL'},'Location','best','fontsize', 14);
ylabel('Homeless Population (Thousands)','fontsize', 14)
ylim([0 150]);
% line([2006.1 2006.1],[0 150],'Color','k','LineWidth',2)
% line([2010 2010],[0 150],'Color','k','LineWidth',2)
annotation('doublearrow',[0.14 0.31],[0.7 0.7],'LineWidth',1)
%gray box
annotation('rectangle',[0.13 0.11 0.193 0.814],'FaceColor',[0.6 0.6 0.6],'EdgeColor', 'none','FaceAlpha',0.2)
text(2007.15,115,'Recession')
text(2006.7,141,'CA','Color', 'b')
text(2006.7,122,'NY','Color', 'b')
text(2006.7,20,'FL','Color', 'b')
text(2017.1,135,'-3.4%','Color','r','fontsize', 14)
text(2017.1,90,'+43.0%','Color','r','fontsize', 14)
text(2017.1,33.5,'-33.0%','Color','r','fontsize', 14)
text(2009.8,100,'NY Shelter System Growth','Color','b','fontsize', 12)
annotation('arrow',[0.43 0.45],...
[0.68 0.84],'LineWidth',1,'Color','b')
%legend outside
legend1 = legend('show');
set(legend1,...
'Position',[0.0242817408376966 0.78412296037296 0.0675392670157068 0.0952797202797202],...
'FontSize',14);
box off
xlim([2006 2018])
xlabel('Years');
xticks([2007 2009 2011 2013 2015 2017])
box on
% Create line
annotation('line',[0.903664921465969 0.904712041884817],...
[0.924825174825175 0.11013986013986],'Color',[0 0 1],'LineWidth',3);
%turn on grid lines
set(gca,'XGrid','off')
set(gca,'YGrid','on')
ax = gca;
ax.GridColor = [0, 0, 0];
%white background
set(gcf,'color','w');
%window size
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 .5 .5]);
%title
title('Largest Homeless Populations 2007-2017', 'fontsize', 18);

Antworten (2)

JohnGalt
JohnGalt am 11 Jun. 2018
This may not be the most elegant solution but it is easily tailorable - if you don't rotate the lables you can append some white space to the labels and thereby add some distance to the axis - see the code below:
figure;plot(magic(10))
ylabels = get(gca,'YTickLabels');
for i=1:length(ylabels);
ylabels{i} = ([ylabels{i} ' ']);
end
set(gca,'YTickLabels',ylabels)

Gautam
Gautam am 29 Aug. 2024
Hello Minka,
I understand that you have the “ylabel” overlapping with the y-axis of your plot and that you want to increase the space between the ylabel” and the y-axis.
To accomplish this you can use the “set” command:
set(Labely, "Position", [-5 25.0000 1]);
The command takes the “ylabel” handle as the first input and changes the “Position” property to the desired value.
The code below shows the usage of the command in changing the position of the “ylabel
figure;
Z = peaks;
plot = contourf(Z);
Labely = ylabel("Y - Label");
Labelx = xlabel("X - Label");
set(Labely, "Position", [-5 25.0000 1]);
The plot before and after setting the position of the “ylabel” respectively is shown below:
Hope this helps

Kategorien

Mehr zu Labels and Annotations finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by