label each curve on semilog graph

16 views (last 30 days)
Sonali
Sonali on 15 Dec 2023
Commented: Star Strider on 15 Dec 2023
Hi
I have multiple columns/arrays that I need to plot using semilogx. I tried plotting using the following :
semilogx(array_1,array_2,'.',LineStyle='-',DisplayName='no_1')
hold on
....
I am getting plots but without labels for individual curves. I was wondering if there was a better way of labeling the curves at their first point/beginning. really appreciate some help here.

Answers (2)

Star Strider
Star Strider on 15 Dec 2023
I am not certain what you want, however creating a legend entry for each line does not appear to be the desired result.
It is relatively straightforward to use text objects to label plots —
x = logspace(-3,1,500).';
y = [exp(-0.1*x).*sin(2*pi*x) exp(-0.2*x).*cos(2*pi*x)];
figure
semilogx(x, y)
grid
axis('padded')
text(x(1), y(1,1), "\leftarrow Sine Curve With Exponential Attenuation", 'Horiz','left', 'Vert','middle', 'Rotation',-30)
text(x(1), y(1,2), '\leftarrow Cosine Curve With Exponential Attenuation', 'Horiz','left', 'Vert','middle', 'Rotation',-30)
Make appropriate changes to get the result you want.
.
  3 Comments
Dyuman Joshi
Dyuman Joshi on 15 Dec 2023
If you have 22 plots on the same graph, then the figure will likely get cluttered with 22 text labels.
Star Strider
Star Strider on 15 Dec 2023
It might help to have the data. I cannot figure out how to simulate that plot.
You can have the colours not repeat by choosing a different colormap, and then specifying the colours ffor each line object.
The other problem however are the labels. You can still use text with them, however the number of them is going to be a problem.
x = logspace(-1, 3, 22).';
y = 1E3*randn(1,22) .* x;
figure
semilogx(x, y)
Ax = gca;
Ax.XAxis.TickLabels = [];
cmpds = {'H^+','H_3O^+','CO^+\ N_2^{++}'}
cmpds = 1×3 cell array
{'H^+'} {'H_3O^+'} {'CO^+\ N_2^{++}'}
NC = numel(cmpds);
xv = rand(1,NC)+logspace(-1,2,NC);
yv = ones(1,NC)*(-0.05*diff(ylim)+min(ylim));
for k = 1:NC
text(xv(k), yv(k), sprintf('$\\uparrow$\n$%s$',cmpds{k}), 'Interpreter','latex')
end
I cannot incorporate a newline in the third ‘cmpd’ so that it will print on two lines. The best I can do is a space.
Annotation objects might be able to do the longer arrows at specific angles, however they will not operate outside of the axis llimits (unlike text), so it might be possible to use them with an ‘axes in axes’ plot (see Position Multiple Axes in Figure for an illustration) and use them in the larger axis referring to the smaller axes, however this would require considerable effort, as well as all the data.
.

Sign in to comment.


Sulaymon Eshkabilov
Sulaymon Eshkabilov on 15 Dec 2023
Here are a couple of the possible ways to get this exercise done:
t = logspace(0, 2, 1e3);
Y1 = exp(sin(t));
Y2 = exp(cos(t));
figure % Way 1
semilogx(t, Y1, 'r-o', 'DisplayName', 'Y_1(t) = e^{sin(t)}')
hold on
semilogx(t, Y2, 'b--*', 'DisplayName', 'Y_2(t) = e^{cos(t)}')
legend('Show')
xlabel('t')
ylabel('Y_1(t), Y_2(t)')
grid on
figure % Way 2
semilogx(t, Y1, 'r-o')
hold on
semilogx(t, Y2, 'b--*')
legend('Y_1(t) = e^{sin(t)}', 'Y_2(t) = e^{cos(t)}')
xlabel('t')
ylabel('Y_1(t), Y_2(t)')
grid on

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!