Add legend to plot colored by colormap function
이전 댓글 표시
I am using currently making a plot where the color of the points is determined by a code (1-3) in the 3 column of the matrix. I can't seem to figure out how to get matlab to make a legend for this and not a colorbar. Since they're plotted as one thing the automatic legend function only includes one point. As it stands I've just been adding a legend in illustrator but it's a bit time consuming with lots of graphs and I'd prefer to have it done in matlab. Is it possible to either make a legend from scratch and specify each entry and label or get matlab to do it automatically? Thanks!
The code I am using and the figure are below:
x = [HL_conpor HL_perm HL_class];
colors = [0.8 0.8 0;
1 0.5 0
1 0 0
]; %
scatter(x(:,1), x(:,2),[], x(:,3),'filled')
set(gca, 'YScale', 'log')
xlabel('Connected Porosity(\phi_c)');
ylabel('Permeability (m^2)');
colormap(colors)
legend_labels = {'VDP1', 'VDP2', 'VDP3'};
legend(legend_labels, 'Location', 'Best');

채택된 답변
추가 답변 (1개)
Scott MacKenzie
2021년 8월 12일
One approach is to do three scatters, one for each value in the 3rd column in your data. Here's the general idea using a modified version of your code:
x = [rand(25,1) rand(25,1) randi(3,25,1)];
colors = [0.8 0.8 0; 1 0.5 0; 1 0 1];
c1 = x(:,3) == 1;
c2 = x(:,3) == 2;
c3 = x(:,3) == 3;
scatter(x(c1,1), x(c1,2),100, 'filled');
hold on;
scatter(x(c2,1), x(c2,2),100, 'filled');
scatter(x(c3,1), x(c3,2),100, 'filled');
set(gca, 'YScale', 'log')
xlabel('Connected Porosity(\phi_c)');
ylabel('Permeability (m^2)');
colormap(colors)
legend_labels = {'VDP1', 'VDP2', 'VDP3'};
legend(legend_labels, 'Location', 'Best');

카테고리
도움말 센터 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
