77 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
CS am 25 Jun. 2020
Kommentiert: Star Strider am 2 Mai 2022
Akzeptierte Antwort: Star Strider
In MATLAB Online öffnen
Hello,
I want to plot some data points and fit a line to the data. I have done so and everything is ok; the only issue is there are some more items in the legend box corresponding to the fitted lines.
C=['b','r','g','m'];
format short
a=1:4;
for i=1:length(a)
scatter(LT, Ra(i,:),C(i),'filled');
hold on
end
grid on
grid minor
xticks([30, 40, 50, 60, 70])
xticklabels({'30','40','50','60','70'})
for i=1:4
coeffs = polyfit(LT, Ra(i,:), 1);
% Get fitted values
fittedX = linspace(min(LT), max(LT), 200);
fittedY = polyval(coeffs, fittedX);
% Plot the fitted line
plot(fittedX, fittedY,C(i) , 'LineWidth', 2);
end
lgd=legend('25%','50%','75%','100%','FontSize',20)
lgd.Title.FontSize = 20;
The resulted figure is as follows:
Although I only have
lgd=legend('25%','50%','75%','100%','FontSize',20)
in the script, the fitted lines are unwantedly shown in the legend box with no dedicated name:
My question:
How can I get rid of those extra lines in the legend box?
Any help is highly appredicated!
0 Kommentare -2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden
-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden
Melden Sie sich an, um zu kommentieren.
Melden Sie sich an, um diese Frage zu beantworten.
Akzeptierte Antwort
In MATLAB Online öffnen
‘How can I get rid of those extra lines in the legend box?’
Refer only to the scatter plots by handle reference:
LT = 0.5:9.5;
Ra = randn(4,numel(LT));
C=['b','r','g','m'];
hold on
for i = 1:size(Ra,1)
hs(i) = scatter(LT, Ra(i,:),C(i),'filled'); % Return Handles To ‘scatter’ Objects
coeffs = polyfit(LT, Ra(i,:), 1);
fittedX = linspace(min(LT), max(LT), 200);
fittedY = polyval(coeffs, fittedX);
plot(fittedX, fittedY,C(i) , 'LineWidth', 2);
end
hold off
lgd=legend(hs, '25%','50%','75%','100%','FontSize',20); % Use Only ‘scatter’ Objects In ‘legend’ call
Include the other lines in your code that I omitted here.
.
4 Kommentare 2 ältere Kommentare anzeigen2 ältere Kommentare ausblenden
2 ältere Kommentare anzeigen2 ältere Kommentare ausblenden
CS am 25 Jun. 2020
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/554764-remove-the-legends-for-some-lines-in-a-plot#comment_913576
It worked very well! Thanks!
Star Strider am 25 Jun. 2020
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/554764-remove-the-legends-for-some-lines-in-a-plot#comment_913582
As always, my pleasure!
Rodrigo Martinez am 2 Mai 2022
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/554764-remove-the-legends-for-some-lines-in-a-plot#comment_2135950
Dear Star Strider,
I've been working for a while with Matlab but I have always worked with my own data and figures. This time, however, my colleague sent me these figures I need to put in a paper. The legneds I need to modify are exactly the same as the ones in this post: scatter data and fit curves for which I don't need legends. Is it possible to get rid of the extra legends without needing to ask for the data to create new figures?
All the best,
Star Strider am 2 Mai 2022
Direkter Link zu diesem Kommentar
https://de.mathworks.com/matlabcentral/answers/554764-remove-the-legends-for-some-lines-in-a-plot#comment_2136100
In MATLAB Online öffnen
@Rodrigo Martinez — I am not certain that I understand what you want.
One option to select only some data series for the legend might be to use 'DisplayName' —
LT = 0.5:9.5;
Ra = randn(4,numel(LT));
C=['b','r','g','m'];
dnv = 25:25:100;
hold on
for i = 1:size(Ra,1)
hs(i) = scatter(LT, Ra(i,:),C(i),'filled', 'DisplayName',sprintf('%.0f%%',dnv(i))); % Return Handles To 'scatter' Objects, Use 'DisplayName'
coeffs = polyfit(LT, Ra(i,:), 1);
fittedX = linspace(min(LT), max(LT), 200);
fittedY = polyval(coeffs, fittedX);
plot(fittedX, fittedY,C(i) , 'LineWidth', 2);
end
hold off
lgd=legend(hs([1 4]),'FontSize',20, 'Location','best'); % Use Only Selected 'scatter' Objects In 'legend' Call
.
Melden Sie sich an, um zu kommentieren.
Weitere Antworten (1)
dpb am 25 Jun. 2020
In MATLAB Online öffnen
If you only want the scatter points in legend, set the 'Annotation' property to not show the lines...
plot(fittedX, fittedY,C(i) , 'LineWidth', 2,'Annotation','off');
0 Kommentare -2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden
-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden
Melden Sie sich an, um zu kommentieren.
Melden Sie sich an, um diese Frage zu beantworten.
Siehe auch
Kategorien
MATLABGraphicsFormatting and AnnotationLabels and AnnotationsLegend
Mehr zu Legend finden Sie in Help Center und File Exchange
Tags
- extra lines in legend box
- fitted line to data
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
Es ist ein Fehler aufgetreten
Da Änderungen an der Seite vorgenommen wurden, kann diese Aktion nicht abgeschlossen werden. Laden Sie die Seite neu, um sie im aktualisierten Zustand anzuzeigen.
Website auswählen
Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .
Sie können auch eine Website aus der folgenden Liste auswählen:
Amerika
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asien-Pazifik
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Kontakt zu Ihrer lokalen Niederlassung