Clabel style in-line text for plot

3 次查看(过去 30 天)
Joris
Joris 2021-9-23
回答: Narvik 2024-10-30
I use 'clabel(C,h,v)' to add in-line text to the lines of the contour plot. Is there a way to do this for normal line plots? The example below shows what I mean, the orange line has 200 in-line, but the 900, 1200, 1400 & 1700 don't. Is there a way to do this for a normal line plot?

回答(1 个)

Narvik
Narvik 2024-10-30
Hi Joris,
As per my understanding, you are trying to add in-line labels to line plots.
You can use the "text" function to add in-line labels to line plots.
Refer to the following documentation for more information on the "text" function:
Refer to the following sample code with a simple contour and line plot with inline labels:
% Contour plot data
[X, Y] = meshgrid(-3:0.1:3, -3:0.1:3);
Z = X.^2 + Y.^2;
% Create the contour plot
figure;
[C, h] = contour(X, Y, Z, 8, 'LineWidth', 1);
clabel(C, h, 'FontSize', 8, 'Color', 'k');
hold on;
% Line plot data
x = -3:0.1:3;
y = x;
% Plot the line
plot(x, y, '-r', 'LineWidth', 1.5);
% Using "text" to add in-line label on the line plot
labelIndex = find(x == 0);
text(x(labelIndex), y(labelIndex), ' y = x ', 'BackgroundColor', 'w', 'VerticalAlignment', 'middle', 'HorizontalAlignment', 'center', 'Color', 'r');
% Customize plot
xlabel('X-axis');
ylabel('Y-axis');
title('Contour and Line Plot with In-line Labels');
grid on;
hold off;
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Contour Plots 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by