필터 지우기
필터 지우기

for some reason, when i am trying to make a bar graph which each datapoint plotted on each bar, the legend is not correct. only 2 of the 4 labels appear correctly

조회 수: 2 (최근 30일)
% Define a color palette (RGB values)
colorPalette = [0.2 0.4 0.6; % Example colors
0.8 0.2 0.4;
0.5 0.7 0.2;
0.9 0.6 0.1];
% Get the column indices of the selected columns (every 4th column starting from column 4)
NormObjAreaMm2 = 4:4:size(combinedTable, 2);
% Extract the data for the selected columns (excluding the last 3 rows)
data = combinedTable{1:end-3, NormObjAreaMm2};
% Get the column names for the selected columns
NormObjAreaMm2ColumnNames = combinedTable.Properties.VariableNames(NormObjAreaMm2);
% Determine the number of selected columns
NormObjAreaMm2NumColumns = numel(NormObjAreaMm2);
% Create a bar graph for each column with custom colors
figure;
hold on;
for i = 1:NormObjAreaMm2NumColumns
bar(i, mean(data(:,i)), 'FaceColor', colorPalette(i, :));
scatter(repmat(i, size(data, 1), 1), data(:,i), 'k', 'filled');
end
hold off;
% Set the x-axis label
xlabel('Total Normalized Object Count');
% Set the y-axis label
ylabel('mm^2');
% Remove the x-axis tick marks and labels
set(gca, 'XTick', []);
% Create the legend using the column names and custom colors
figureLegend = legend(NormObjAreaMm2ColumnNames);
legendEntries = get(figureLegend, 'String');
for i = 1:NormObjAreaMm2NumColumns
legendEntries{i} = sprintf('\\color[rgb]{%.2f,%.2f,%.2f}%s', colorPalette(i, :), legendEntries{i});
end
set(figureLegend, 'String', legendEntries);

답변 (1개)

cdawg
cdawg 2023년 6월 29일
Your legend is showing the first four objects you plotted, which is bar, scatter, bar, scatter. You're only giving it 4 legend entries instead of 8, so I'm assuming you want only the bar or the scatter to show.
I'm not sure if you want the scatter plots to show in the legend or the bar plots, but whichever ones you want hidden from the legend, put 'HandleVisibility', 'off'. E.g. plot(x,y,'HandleVisibility', 'off').
I don't have access to your data, but here's an example:
barDat = [3 5 1];
scatterDat = [1 2 3; 4 5 6; 7 8 9];
cols = {'red','cyan','green'};
names = {'1','2','3'};
figure()
for ii = 1:length(barDat)
bar(ii,barDat(ii),'FaceColor',cols{ii});
hold on
scatter(repmat(ii,size(scatterDat,2),1), scatterDat(ii,:),'k','filled','HandleVisibility','off')
end
l = legend(names);
  댓글 수: 3
Asia Dofat
Asia Dofat 2023년 6월 29일
I want the bar and the scatter to show, but i want the legend to only show the bar colors. For some reason dots are appearing as 2 of the legend markers
cdawg
cdawg 2023년 6월 29일
So use scatter(x,y, "HandleVisibility", "off"). This might sound like it won't show in the figure, it will.. it just won't show in the legend.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Scatter Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by