Colors of Scatter Plot don't match Legend

3 visualizzazioni (ultimi 30 giorni)
SJ B
SJ B il 26 Gen 2018
Commentato: Walter Roberson il 29 Gen 2018
I noticed that my scatter plot colors don't match the legend using Matlab R2017A. I was wondering why this might be the case. I was thinking it was because it was a held plot with multiple scatters, so it was reusing the colour scheme but I'm not sure how to prevent this issue. I've attached an image and the code below :
% %2P Derivation
weibullparameters_field=wblfit(Damage_FieldMileageTotal);
WeibullScale_Field=weibullparameters_field(1);
WeibullSlope_Field=weibullparameters_field(2);
%Generation of Field Data with random 2P
CustomerSample=wblrnd(WeibullScale_Field,WeibullSlope_Field,numsamples,1);
Damage_CustomerSample_atTestSeverity=prctile(CustomerSample,TestSeverity*100);
WeibullScale_TestInit=Damage_CustomerSample_atTestSeverity*WeibullSlope_Test/WeibullSlope_Field;
%Initial Test Sample Generation
TestSample1=wblrnd(WeibullScale_TestInit, WeibullSlope_Test,numsamples,1);
Reliability=sum(TestSample1>Damage_CustomerSample_atTestSeverity)/numsamples;
BinWidth=range([CustomerSample;TestSample1])/200;
TestSampleCounts=histcounts(TestSample1,'BinWidth',BinWidth);
CustomerSampleCounts=histcounts(CustomerSample,'BinWidth',BinWidth);
h1=scatter(0:BinWidth:max(CustomerSample),CustomerSampleCounts);
hold on;
h2=scatter(0:BinWidth:max(TestSample1),TestSampleCounts);
xlabel('Damage')
ylabel('Counts')
i=2;
legendInfo{1}=['1'];
legendInfo{2}=['2'];
while (abs(Reliability-TestRelNum)/TestRelNum>1E-4)
TestSample=wblrnd(WeibullScale_TestInit, WeibullSlope_Test,numsamples,1);
Reliability=sum(TestSample>Damage_CustomerSample_atTestSeverity)/numsamples;
i=i+1;
TestSampleCounts=histcounts(TestSample,'BinWidth',BinWidth);
h3=scatter(0:BinWidth:max(TestSample),TestSampleCounts);
if (mod(i,50)~=0)
set(h3,'Visible','off')
end
legendInfo{i}=[num2str(i)];
if (Reliability>TestRelNum)
WeibullScale_TestInit=WeibullScale_TestInit-5E-6;
ScaleParDir='Weibull Scale Parameter is decreasing';
else
WeibullScale_TestInit=WeibullScale_TestInit+5E-6;
ScaleParDir='Weibull Scale Parameter is increasing';
end
end
legend([legendInfo(1:2) legendInfo(50:50:end)]);

Risposte (1)

Walter Roberson
Walter Roberson il 26 Gen 2018
legend entries get grayed out like that when the corresponding line has 'visible', 'off'
It is not obvious to me why 50 or 100 would be visible off but that the real items that are not visible do not show up. How are you invoking legend() ?
  2 Commenti
SJ B
SJ B il 26 Gen 2018
Ah right, I forgot to show that line, my bad:
legend([legendInfo(1:2) legendInfo(50:50:end)]);
Walter Roberson
Walter Roberson il 29 Gen 2018
Just as I suspected. legend() by default associates text legends with graphics handles in the order the handles were created. You are creating a whole bunch of scatter plots and then setting all but every 50th to be invisible, but the handles still exist. You are providing four text entries to legend(), so it labels the first four handles it finds, two of which have useful data, and the next two of which are the first two entries that you created but set to invisible.
I am at a loss as to why you are creating those graphics handles just to make them invisible. It seems to me it would make more sense to have the scatter() itself within the if body so that you only create scatter plots for what will be visible.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by