Problem adding text to plot, background color doesn't work properly

85 Ansichten (letzte 30 Tage)
I have encountered a problem using text to put labels inside a plot. I set the background colour to white. This works when I write the text over data plotted with plot, but it doesn't work over lines plotted with xline.
Here's my code:
figure;
plot( datimPingMBRY, timePingMBRY, 'r-' );
hold on;
plot( datimPingMVOFLS2, timePingMVOFLS2, 'b-' );
xline( logItemTime );
datetick( 'x', 'keeplimits' );
yLimits = ylim;
yText = yLimits(2) * 0.2;
t = text( logItemTime, yText*ones(nLogItems,1), string(logItemWhat), ...
'FontSize', 8, 'BackgroundColor', 'w', 'Rotation', 90 );
And here's the plot.
Any suggestions?

Akzeptierte Antwort

Star Strider
Star Strider am 16 Mär. 2023
The line does not through the label. However it is necessary to specify the label in the xline call —
xl = xline(0.5,'-','X-Line Label');
xl.LabelHorizontalAlignment = 'center';
xl.LabelVerticalAlignment = 'middle';
Also, it is likely more efficient to use datetime arrays than using datenum values (as would appear to be the situation from the datetick call). The xline function can use datetime and duration arguments to define its position.
.
  5 Kommentare

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by