Borrar filtros
Borrar filtros

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 visualizaciones (últimos 30 días)
% 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);

Respuestas (1)

cdawg
cdawg el 29 de Jun. de 2023
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 comentarios
Asia Dofat
Asia Dofat el 29 de Jun. de 2023
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 el 29 de Jun. de 2023
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Scatter Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by