Legend Colors Don't Match

I am plotting a simple line graph of 3 different sets of data on one plot and the legend works, but the legend colors do not match the colors specified for the lines. I would also like to omit the xline I have added from the legend. Thank you.
Udeficit=MeanUvelo(:,50);
Udeficit2=MeanUvelo(:,100);
Udeficit3=MeanUvelo(:,150);
figure;
plot(Udeficit,Y,'k-');
hold on
plot(Udeficit2,Y,'b-');
hold on
plot(Udeficit3,Y,'g-');
legend('Position 45','Position 90','Position 135','location',...
'northeastoutside');
xlin=xline(AvgFree_Stream,'r-','Avg Free Stream');
hold off
xlin.LabelVerticalAlignment = 'middle';
title('Velocity Deficit Curves');
xlabel('U (m/s)');
ylabel('Y (m)');
set(gca,'Fontsize',16);

Réponses (2)

Image Analyst
Image Analyst le 13 Déc 2019

0 votes

The other data1 is added automatically because of the line. Would it be okay to do
legend('Position 45','Position 90','Position 135', 'Average Free Stream', ...
'location', 'northeastoutside');

1 commentaire

MR0d
MR0d le 13 Déc 2019
Hello, I commented out my original legend line and inserted yours and when I ran it collectively I got a figure that looks like this Reply1.jpg

Connectez-vous pour commenter.

Image Analyst
Image Analyst le 13 Déc 2019

0 votes

Try this (putting legend after the call to xline) -- it seemed to not have that last data1 legend for me:
figure;
plot(Udeficit,Y,'k-');
hold on
plot(Udeficit2,Y,'b-');
hold on
plot(Udeficit3,Y,'g-');
AvgFree_Stream = mean(Udeficit)
xlin=xline(AvgFree_Stream,'r-','Avg Free Stream');
legend('Position 45','Position 90','Position 135','location',...
'northeastoutside');
hold off
xlin.LabelVerticalAlignment = 'middle';
title('Osc Mako Velocity Deficit Curves');
xlabel('U (m/s)');
ylabel('Y (m)');
set(gca,'Fontsize',16);
grid on;
0000 Screenshot.png

5 commentaires

MR0d
MR0d le 15 Déc 2019
Hello, that was successful in removing the legend entry for the vertical line but the lines in the legend are still not the same color as those I used in the plot. They're all black.
Image Analyst
Image Analyst le 15 Déc 2019
Look at your screenshot. The lines are black (top), blue (middle), and red (bottom) which are the same as your plotted lines. Why do you say they're all black?
MR0d
MR0d le 15 Déc 2019
After copying and using your update to my code the resulting graph looks like this. The vertical line is no longer in the legend, but the lines are still all black in the legend.Reply 2.jpg
Image Analyst
Image Analyst le 15 Déc 2019
Attach your data in a .mat file. Give me the full code you are using to plot the 3 curves and one line.
MR0d
MR0d le 15 Déc 2019
I'm sorry but I'm not going to send my data.

Connectez-vous pour commenter.

Produits

Version

R2018b

Question posée :

le 13 Déc 2019

Modifié(e) :

le 15 Déc 2019

Community Treasure Hunt

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

Start Hunting!

Translated by