Highlighten specific x-axis values

89 次查看(过去 30 天)
Hey guys !
I need some help to plot a small figure for my thesis. I think the question is quite simple but however I have not found a solution yet...
What I want to do is to plot my figure with its x-axis that ranges from 0 to 5 in steps of 0.5. Now I want to add 2 specific values (say 1.1 and 3.387) to the x-axis and label them as f_1 and f_2. I tried using xtick but it did not work. Here's my code:
% Values
x_max = 0.22;
v_max = 1.6;
a_max = 35;
f_1 = v_max/(x_max*2*pi);
f_2 = a_max/(v_max*2*pi);
a_1 = x_max * (2*pi*f_1)^2;
a_2 = a_max;
% Curves
f_c1 = [0 f_1];
y_c1 = [0 a_1];
f_c2 = [f_1 f_2];
y_c2 = [a_1 a_2];
f_c3 = [f_2 5];
y_c3 = [a_max a_max];
% Plot
figure
hold on
grid on
box on
xlabel('Frequency [Hz]')
ylabel('Acceleration [m/s^2]')
title('Dynamic Operating Space')
set(gca,'xtick', [f_1 f_2], 'xticklabel', {'f_1'; 'f_2'})
% Characteristic Frequencies
plot([f_1 f_1], [0 40], '--', 'color', 'red')
plot([f_2 f_2], [0 40], '--', 'color', 'red')
% Dynamic Curves
plot(f_c1, y_c1, 'LineWidth', 2, 'color', 'black')
plot(f_c2, y_c2, 'LineWidth', 2, 'color', 'black')
plot(f_c3, y_c3, 'LineWidth', 2, 'color', 'black')
an_1 = text(0.3, 5.5,'x_{max} = 0.22m');
set(an_1,'Rotation',45)
%set(an_1,'FontSize',12)
% ar_1 = area(f_c1, y_c1);
% set(ar_1, 'FaceColor', [1 0.5 0]);
an_2 = text(2, 22.5,'v_{max} = 1.6m/s');
set(an_2,'Rotation',45)
an_3 = text(4, 36.5,'a_{max} = 35m/s^2');
This is what I got:
figure.jpg
If the x-axis would now display the full range in addition to above f_1 and f_2 I'd be very happy.
Any clue ?
Additionally, is there any way to grey out the area under the curves ?
Thank you !
  3 个评论
Aladin Djuhera
Aladin Djuhera 2019-12-30
Hi ! Thank you, but I would not like to see the actual numbers but rather the text f_1 and f_2. Is that possible ?
darova
darova 2019-12-30
This is what your code produces on my PC
img1.png

请先登录,再进行评论。

采纳的回答

Adam Danz
Adam Danz 2019-12-30
编辑:Adam Danz 2019-12-30
"What I want to do is to plot my figure with its x-axis that ranges from 0 to 5 in steps of 0.5. Now I want to add 2 specific values (say 1.1 and 3.387) to the x-axis and label them as f_1 and f_2."
If you want all ticks 0 : 0.5 : 5 and also the values in f_1 and f_2,
xtickVals = unique([0 : 0.5 : 5, f_1, f_2]);
xtickLabs = compose('%.3g',xtickVals);
xtickLabs(ismembertol(xtickVals,[f_1,f_2])) = {'f_1','f_2'};
set(gca,'xtick', xtickVals, 'xticklabel', xtickLabs, 'xlim', [min(xtickVals),max(xtickVals)])
If you want only the f_1 and f_2 labels,
set(gca,'xtick', [f_1, f_2], 'xticklabel', {'f_1','f_2'}, 'xlim', [0,5])
A better idea might be to use xline() to label those values.
set(gca,'xtick', 0 : 0.5 : 5, 'xlim', [0,5])
xline(f_1,'--r','f_1','LabelVerticalAlignment','bottom')
xline(f_2,'--r','f_2','LabelVerticalAlignment','bottom')
Finally, if you want the underscrore rather than a subscript, set the interpreter to none.
xline(f_1,'--r','f_1','LabelVerticalAlignment','bottom','Interpreter','None')
  3 个评论
Adam Danz
Adam Danz 2019-12-30
编辑:Adam Danz 2019-12-30
Thanks, I fixed that typo.
To rotate the label, see the LabelOrientation property of xline().
Aladin Djuhera
Aladin Djuhera 2019-12-30
Perfect! Thank you very much, Adam :)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Grid Lines, Tick Values, and Labels 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by