How can I represent as legend the x-points that the curves in the figure diverge from the x-axis?
Showing the value on the x-axis as a legend the moment the curve deviates from the x-axis
45 views (last 30 days)
Show older comments
Hi everyone. How can I represent the point where the curves in the figure I sent diverge from the x-axis as a legend?

Accepted Answer
Umar
on 17 Dec 2025 at 7:43
Edited: Umar
on 17 Dec 2025 at 8:00
@Erkan, I have gone through your comments and understood what you're trying to accomplish! Based on your figure, you want to mark where each curve starts to diverge from the x-axis and show those points in your legend. I notice in your clarifying comment you specifically mentioned wanting to represent the x-points, meaning the actual x-coordinate values in the legend. I have implemented a solution that displays the actual x-coordinates in your legend, which I think is what you're asking for. Here's a complete script that detects where each curve diverges from the x-axis using threshold detection, places markers at those exact points, and shows the actual x-coordinate values in the legend:
%% plotDivergencePoints - Mark and label x-coordinates where curves %diverge % % This script identifies where curves diverge from the x-axis and %displays % the x-coordinate values as legend entries.
%% Initialize workspace clear variables; close all; clc;
%% Configuration parameters config.divergencePoints = [0.040, 0.075, 0.110]; % x-coordinates [m] config.growthRates = [800, 2000, 1500]; % Slope after divergence config.nPoints = 500; % Resolution config.xRange = [0, 0.2]; % Domain limits config.threshold = 1e-6; % Divergence threshold
%% Generate synthetic data (replace with your actual data) x = linspace(config.xRange(1), config.xRange(2), config.nPoints)';
nCurves = length(config.divergencePoints); yData = zeros(length(x), nCurves);
% Create curves with different shapes
curveFunctions = {@tanh, @(z) z, @(z) z.^0.8};
for iCurve = 1:nCurves
xShifted = x - config.divergencePoints(iCurve);
rawCurve = config.growthRates(iCurve) * ...
curveFunctions{iCurve}(xShifted * 20);
yData(:, iCurve) = max(rawCurve, 0); % Clip to x-axis
end
%% Create figure
hFig = figure('Position', [100, 100, 900, 600], ...
'Color', 'w', ...
'Name', 'X-Axis Divergence Analysis');
% Custom color scheme - distinct from default 'lines' colormap % Using a warm color palette for better visual distinction customColors = [ 0.85, 0.33, 0.10; % Burnt orange 0.93, 0.69, 0.13; % Golden yellow 0.49, 0.18, 0.56 % Deep purple ]; colors = customColors; hLines = gobjects(nCurves, 1); hMarkers = gobjects(nCurves, 1);
%% Plot curves
hold on;
for iCurve = 1:nCurves
hLines(iCurve) = plot(x, yData(:, iCurve), ...
'LineWidth', 2, ...
'Color', colors(iCurve, :), ...
'DisplayName', sprintf('Curve %d', iCurve));
end
%% Detect and mark divergence points divergenceLabels = cell(nCurves, 1);
for iCurve = 1:nCurves % Find first point where curve exceeds threshold idxDiverge = find(yData(:, iCurve) > config.threshold, 1, 'first');
if ~isempty(idxDiverge)
xDiverge = x(idxDiverge);
yDiverge = yData(idxDiverge, iCurve); % Create marker at divergence point
hMarkers(iCurve) = plot(xDiverge, yDiverge, 'o', ...
'MarkerSize', 10, ...
'MarkerEdgeColor', colors(iCurve, :), ...
'MarkerFaceColor', colors(iCurve, :), ...
'LineWidth', 1.5); % Format x-coordinate for legend (this answers your question!)
divergenceLabels{iCurve} = sprintf('x_{div} = %.4f', xDiverge);
else
hMarkers(iCurve) = plot(NaN, NaN, 'o');
divergenceLabels{iCurve} = 'No divergence';
end
endhold off;
%% Configure axes
xlabel('x-axis [m]', 'FontSize', 12, 'FontWeight', 'bold');
ylabel('Function Value', 'FontSize', 12, 'FontWeight', 'bold');
title({'Curves Diverging from X-Axis'; 'with X-Coordinate Legend Markers'}, ...
'FontSize', 14, 'FontWeight', 'bold');
grid on;
axis padded;
xlim(config.xRange);
ylim([0, max(yData(:)) * 1.1]);
%% Create legend with x-coordinates
allHandles = [hLines; hMarkers];
allLabels = [arrayfun(@(i) sprintf('Curve %d', i), 1:nCurves, ...
'UniformOutput', false)'; ...
divergenceLabels];
hLegend = legend(allHandles, allLabels, ...
'Location', 'northwest', ...
'FontSize', 10);
title(hLegend, 'Legend', 'FontWeight', 'bold');
%% Display summary
fprintf('\n%s\n', repmat('=', 1, 60));
fprintf('DIVERGENCE POINT ANALYSIS SUMMARY\n');
fprintf('%s\n', repmat('=', 1, 60));
for iCurve = 1:nCurves
idxDiverge = find(yData(:, iCurve) > config.threshold, 1, 'first');
if ~isempty(idxDiverge)
fprintf('Curve %d: Diverges at x = %.6f m\n', iCurve,
x(idxDiverge));
else
fprintf('Curve %d: No divergence detected\n', iCurve);
end
end
fprintf('%s\n', repmat('=', 1, 60));
fprintf('Configuration:\n');
fprintf(' Threshold: %.2e\n', config.threshold);
fprintf(' Resolution: %d points\n', config.nPoints);
fprintf(' Domain: [%.3f, %.3f] m\n', config.xRange(1),
config.xRange(2));
fprintf('%s\n\n', repmat('=', 1, 60));
Results: please see attached.
The key part that answers your question about representing x-points in the legend is where I use:
divergenceLabels{iCurve} = sprintf('x_{div} = %.4f', xDiverge);
This creates legend entries that show the actual x-coordinate values like "x_div = 0.0401" instead of just generic labels. The x_{div} notation uses TeX formatting to create a subscript, which looks professional in the legend.The find() function is the standard MATLAB approach for threshold detection. It efficiently locates the first index where your curve exceeds the threshold, meaning where it diverges from the x-axis. You can read more about it in the find documentation:
https://www.mathworks.com/help/matlab/ref/find.html
For the legend, I combine both the curve handles and marker handles using [hLines; hMarkers] so your legend shows both the curve names and their corresponding x-coordinates. This approach is documented in the legend function reference:
https://www.mathworks.com/help/matlab/ref/legend.html
If you're working with your own data instead of synthetic curves, you can replace the data generation section with:
% Replace the synthetic data generation with your actual data x = yourXData; % Your x-axis data yData = yourYData; % Your curve data (each column = one curve) nCurves = size(yData, 2);
The rest of the code will work the same way. You might also want to adjust the config.threshold value depending on what you consider as "diverging from the x-axis" for your specific data.
Hope this helps! Let me know if you need any clarification or want to modify the formatting of the x-coordinates in the legend.
Additional references that might be useful:
sprintf function for formatting: https://www.mathworks.com/help/matlab/ref/sprintf.html
DisplayName property for legend entries: https://www.mathworks.com/help/matlab/ref/matlab.graphics.chart.primitive.line-properties.html
0 Comments
More Answers (1)
Matt J
on 17 Dec 2025 at 2:24
Edited: Matt J
on 17 Dec 2025 at 2:26
Something like this, perhaps:
Hlines=plot(1:5, max(0,-2:2), 1:5, max(0,-1:3)); axis padded
h=arrayfun(@addIt,Hlines);
leg=legend([Hlines,h]);
function h=addIt(H)
i=find(H.YData==0,1,'last');
x=H.XData(i);
y=H.YData(i);
hold on
h=plot(x,y,'s','Color',H.Color);
hold off
end
0 Comments
See Also
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!