Adding text to axis label.

51 次查看(过去 30 天)
Ronald Bednarczyk
Ronald Bednarczyk 2020-1-15
I have several codes that read in data from Excel files based on parameter mneumonics. Those mneumonics are used to search another Excel file for a more descriptive parameter name. For example mneumonic ADCIAS_PCM2 will retieve INDICATED AIRSPEED (KTS) as a plot axis label. That axis label is universal and I use it for my x-axis. I want to add text at each end of the x-axis for REARWARD (at the negative x-axis end) and FORWARD (at the positive x-axis end). The current plot goes from -50 to +50. I used the following lines and they work, but the problem arises if the y-axis scaling is different the text will be higher or lower relative to the x-axis. How can I add this text such that it will stay inline with the xlabel?
text(-50,-0.1,'RWD','FontSize',8,'FontWeight','bold');
text( 47,-0.1,'FWD','FontSize',8,'FontWeight','bold');

回答(2 个)

Image Analyst
Image Analyst 2020-1-15
Use the xlabel() and ylabel() commands instead.
xlabel('RWD','FontSize',8, 'FontWeight','bold');
If you want to use text() because you want the text to be in some box somewhere on the graph, then you'll have to replace those numbers for x and y in text() with something based on xlim() and ylim(). For example
% Place text 40% of the way over in x and 60% up in y.
caption = sprintf('The direction is %s', directionString);
xl = xlim;
yl = ylim;
xt = xl(1) + 0.4 * (xl(2) - xl(1));
yt = yl(1) + 0.6 * (yl(2) - yl(1));
text(xt, yt, caption,'FontSize',8, 'FontWeight','bold', 'Horizontal Alignment', 'left');

Ronald Bednarczyk
Ronald Bednarczyk 2020-1-16
I did this instead. The maxy just sets the ylim to the next highest decimal.
xlabel(X_label_txt);
ylabel(cell2mat(Y_axis_label_cell(param_no)));
legend('show','Location','northwest');
xlim([-50 50]); %Fixed X-axis scale
if max(ylim)>=1
maxy=ceil(max(ylim));
elseif max(ylim)*10>=1
maxy=ceil(max(ylim)*10)/10;
elseif max(ylim)*100>=1
maxy=ceil(max(ylim)*100)/100;
end
set(gca, 'YLim', [0, maxy]);
grid on;
%Set X and Y-axis tick mark & label font type & size
ax = gca;
ax.YAxis.TickLabelFormat = '%,.2f';
ax.FontSize = 8;
ax.FontName = 'Courier';
ax.FontWeight = 'bold';
ax.GridColor = [0, 0, 0];
ax.GridLineStyle = ':';
ax.GridAlpha = 0.8;
ytext=-0.2*maxy;
text(-50,ytext,'LSF','FontSize',8,'FontWeight','bold');
text( 47,ytext,'RSF','FontSize',8,'FontWeight','bold');

类别

Help CenterFile Exchange 中查找有关 Axis Labels 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by