Bar chart legend and colour

I have created a bar chart and I want to add the names of the variants on the legend and they shoud have different colours.
How can I realize that?
ds = dataset("xlsfile", "dsCompact");
clean_ds = rmmissing(ds);
%% Mean of Mean of parameter variants for each velocity
[groupVariantsVelocity, variants, velocity, scale] = findgroups(clean_ds.parameter_variants, clean_ds.velocity, clean_ds.scale);
meanVariantsVelocity = splitapply(@mean, clean_ds.score, groupVariantsVelocity);
tVariantsVelocity = table (variants, velocity, scale, meanVariantsVelocity);
figure;
bar(meanVariantsVelocity(scale == "like"));

6 Comments

" I want to add the names of the variants on the legend and they shoud have different colours"
In your current code, each bar is a unique variant. So each bar would have its own color and each bar would pre represented in the legend. There's 40 bars so that would be a lot of colors and a lot of rows in the legend. It wouldn't be useful.
Maybe the idea hasn't been expressed well. Do you want to create a grouped bar chart instead where the scale groups (4) have different colors?
Hi Adam
you have seen my data and I have for ex. chauffeur_shape 10 times but I want just a barchart where I can see this variant with just one scale ( for example like)
I want this for every variant (I only have 5) in one plot..
Then I will do the same for the other 3 scales (anth, comf, ueq).
I have found this tutorial, but I didn't get it work with my data
Adam Danz
Adam Danz on 19 Feb 2020
Edited: Adam Danz on 19 Feb 2020
The code you shared produces this figure. Each bar is the mean of a varient group just for scale = likeability.
"I want to add the names of the variants on the legend and they shoud have different colours"
I suggest adding the names of the variants to the x axis ticks rather than having a legend with 10 rows of labels and 10 different bar colors.
If you're trying to do something different, please explain in detail.
Hi Adam I have created a plot in paint what I am trying to do:
Sorry can you have a look at this data set?
It was the wrong one

Sign in to comment.

 Accepted Answer

Adam Danz
Adam Danz on 19 Feb 2020
Edited: Adam Danz on 19 Feb 2020
There are two approaches below. I recommend using the first one where the bars and labeled by the xtick labels. You can rotate them at any angle you wish. The second approach colors each bar and uses a colorbar to identify the color code. This requires a lot more work from the user to match the bar to the color. With a greater number of bars, the colors become too similar.
Also, consider reading the data in as a table instead of using datasets.
Use x tick labels
figure();
scaleSelection = strcmp(scale,'like'); % use strcmp(), not "==".
barData = meanVariantsVelocity(scaleSelection);
bh = bar(barData);
% add x tick labels
ax = bh.Parent;
ax.XTick = 1:numel(barData);
ax.XTickLabel = variants(scaleSelection);
ax.TickLabelInterpreter = 'none';
xtickangle(ax, 45) % or try 90 degrees
Colored bars with colorbar as legend
This is not recommended.
figure();
scaleSelection = strcmp(scale,'like'); % use strcmp(), not "==".
barData = meanVariantsVelocity(scaleSelection); % use strcmp(), not "==".
bh = bar(barData);
% set bar color
bh.FaceColor = 'flat';
bh.CData = jet(numel(barData)); % use any colormap you'd like
% Add colorbar with categorical color labels
colormap(bh.CData)
cb = colorbar();
caxis([0,numel(barData)])
cb.YTick = 0.5 : 1 : numel(barData);
cb.TickLabels = variants(scaleSelection);
cb.TickLabelInterpreter = 'none';
cb.FontSize = 8;
bh.Parent.Position(3) = 0.55; % make the axis narrower to fit the colorbar labels.

11 Comments

Wow that looks great. Thank you very much Adam.
I will take the first one if you say that is better but:
Do you know how to change the first plot that every bar that velocity == 50 --> variant has a different color.
And I want to add a error bar ( do you have any tipps)?
btw I get this error when I am trying to run the first plot:
Error setting property 'XTickLabel' of class 'Axes':
Value must be a character vector, string array, numeric array, or cell array of character vectors.
Error in statistical_analysis (line 80)
ax.XTickLabel = variants(scaleSelection);
I get this error if I run the 2nd code:
Error setting property 'TickLabels' of class 'ColorBar':
Value must be a character vector, string array, numeric array, or cell array of character vectors.
Error in statistical_analysis (line 95)
cb.TickLabels = variants(scale == "like");
Adam Danz
Adam Danz on 19 Feb 2020
Edited: Adam Danz on 19 Feb 2020
Have you looked into the errors yet? For example, what's the value of "variants(scaleSelection)"? I'm guessing it's empty which would suggest the string you're searching for doesn't exist.
Also, the scale == "like" should be replaced with strcmp() . I'll update the anwer to reflect that change.
Note that the answer works for the data you provided.
I have changed the code:
figure();
scaleSelection = strcmp(scale, 'like');
barData = meanVariantsVelocity(scaleSelection);
bh = bar(barData);
title = "likeability";
% add x tick labels
ax = bh.Parent;
[vgroup, vg] = findgroups (clean_ds.variants);
ax.XTick = 1:numel(barData);
ax.XTickLabel = vg;
ax.TickLabelInterpreter = 'none';
xtickangle(ax, 45) % or try 90 degrees
This works :)
Do you have any ideas for that?
Do you know how to change the first plot that every bar that velocity == 50 --> variant has a different color.
And I want to add a error bar ( do you have any tipps)?
"Do you know how to change the first plot that every bar that velocity == 50 --> variant has a different color."
Here are two methods you can try out.
figure();
scaleSelection = strcmp(scale,'like'); % use strcmp(), not "==".
velocitySelection = velocity == 50;
barData1 = meanVariantsVelocity(scaleSelection & ~ velocitySelection);
bh1 = bar(barData1);
hold on
barData2 = meanVariantsVelocity(scaleSelection & ~ velocitySelection);
x = numel(barData1)+1 : 1 : numel([barData1; barData2]);
bh2 = bar(x, barData2);
bh1.Parent.XTick = 1 : 1 : numel([barData1; barData2]);
figure();
scaleSelection = strcmp(scale,'like'); % use strcmp(), not "==".
barData = meanVariantsVelocity(scaleSelection);
bh = bar(barData);
bh.FaceColor = 'flat';
% Find which bars have velocity==50
velocitySelection = velocity == 50;
velocityBarIdx = ismember(find(scaleSelection), find(velocitySelection));
% Choose two colors
barColors = [0 0 1; 1 0 1];
% Color the vel=50 bars
cmap = barColors(velocityBarIdx+1,:);
bh.CData = cmap;
something like this
Adam Danz
Adam Danz on 19 Feb 2020
Edited: Adam Danz on 19 Feb 2020
See this answer for a demo on adding error bars to a bar plot.
The grouped bars can be achieved by the following code.
However, it is assumed that there is an equal number of values for each velocity group.
figure();
scaleSelection = strcmp(scale,'like'); % use strcmp(), not "==".
barData1 = meanVariantsVelocity(scaleSelection);
velocityGroups = findgroups(velocity(scaleSelection));
barMat(:,1) = barData1(velocityGroups==1);
barMat(:,2) = barData1(velocityGroups==2);
bar(barMat)
I cant add a title.
Do you know why?
I'm not sure what that means.
When you run title('myTitle') or title(axisHandle, 'MyTitle'), do you get an error message? Does the title not appear? You'll need to be more descriptive.

Sign in to comment.

More Answers (0)

Asked:

on 19 Feb 2020

Commented:

on 19 Feb 2020

Community Treasure Hunt

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

Start Hunting!