![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/656985/image.jpeg)
Scatter Plot Legend Issue With Assignment
3 次查看(过去 30 天)
显示 更早的评论
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)');
0 个评论
回答(2 个)
Scott MacKenzie
2021-6-17
编辑:Scott MacKenzie
2021-6-17
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)');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/656985/image.jpeg)
0 个评论
Star Strider
2021-6-17
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.
.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Scatter Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!