Scatter Plot Legend Issue With Assignment

6 views (last 30 days)
Jacob Weiss
Jacob Weiss on 17 Jun 2021
Edited: Scott MacKenzie on 17 Jun 2021
I have five scatter plots that are plotted on one graph. I am trying to have a legend that shows the five colors instead of just the first color from each scatter plot which is green. An immage is attached for refrence.
Thank you
c = [0 0 1; 1 0 0; 0 1 0; 0 1 1; 1 0 1];
scatter(one,SCX00164,[],c,'Filled');
hold on
scatter(two,SCX100064,[],c,'Filled');
hold on
scatter(three,SCZ00164,[],c,'Filled');
hold on
scatter(four,SCZ100064,[],c,'Filled');
hold on
scatter(five,STX00164,[],c,'Filled');
hold on
scatter(six,STX100064,[],c,'Filled');
xlabel('Loading Direction and Strain Rate')
ylabel('Relative Activity')
ylim([0 1]);
dir = {'SCX 001/s'; 'SCX 1000/s'; 'SCZ 001/s'; 'SCZ 1000/s';'STX 001/s';'STX 1000/s'};
set(gca,'xtick',[1:6],'xticklabel',dir);
title('Relative Activites for 64 Elements 6 Crystals');
legend('BASAL','PRSIMATIC','PYRAMIDAL 2ND ORDER (C+A)','TENSILE TWINNING (TT1)','COMPRESSIVE TWINNING (CT2)');

Answers (2)

Scott MacKenzie
Scott MacKenzie on 17 Jun 2021
Edited: Scott MacKenzie on 17 Jun 2021
You only need to call the scatter function once. Re-organize your data, combining the values into a matrix. Then, you'll get different colours in the legend.
For example...
% test data
M = rand(6,5);
scatter(1:6, M, 'filled');
xlabel('Loading Direction and Strain Rate')
ylabel('Relative Activity')
ylim([0 1]);
xlim([.5 6.5]);
dir = {''; 'SCX 001/s'; 'SCX 1000/s'; 'SCZ 001/s'; 'SCZ 1000/s';'STX 001/s';'STX 1000/s'};
set(gca,'xtick',[0:6],'xticklabel',dir);
title('Relative Activites for 64 Elements 6 Crystals');
legend('BASAL','PRSIMATIC','PYRAMIDAL 2ND ORDER (C+A)','TENSILE TWINNING (TT1)','COMPRESSIVE TWINNING (CT2)');

Star Strider
Star Strider on 17 Jun 2021
I cannot run the code (no data).
That aside, try something llike this —
c = [0 0 1; 1 0 0; 0 1 0; 0 1 1; 1 0 1];
figure
hs{1} = scatter(1, rand(5,1),[],c,'Filled');
hold on
hs{2} = scatter(2, rand(5,1),[],c,'Filled');
hs{3} = scatter(3, rand(5,1),[],c,'Filled');
hs{4} = scatter(4, rand(5,1),[],c,'Filled');
hs{5} = scatter(5, rand(5,1),[],c,'Filled');
hs{6} = scatter(6, rand(5,1),[],c,'Filled');
hold off
xlim([0 6])
dir = {'SCX 001/s'; 'SCX 1000/s'; 'SCZ 001/s'; 'SCZ 1000/s';'STX 001/s';'STX 1000/s'};
set(gca,'xtick',[1:6],'xticklabel',dir);
legend(hs{1}(1:5),'BASAL','PRSIMATIC','PYRAMIDAL 2ND ORDER (C+A)','TENSILE TWINNING (TT1)','COMPRESSIVE TWINNING (CT2)');
The other five scatter handles may not be necessary. Use them only if you need them.
.

Community Treasure Hunt

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

Start Hunting!