フィルターのクリア

Bar chart legend and colour

24 ビュー (過去 30 日間)
Megan
Megan 2020 年 2 月 19 日
コメント済み: Adam Danz 2020 年 2 月 19 日
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 件のコメント
Megan
Megan 2020 年 2 月 19 日
Hi Adam I have created a plot in paint what I am trying to do:
Megan
Megan 2020 年 2 月 19 日
Sorry can you have a look at this data set?
It was the wrong one

サインインしてコメントする。

採用された回答

Adam Danz
Adam Danz 2020 年 2 月 19 日
編集済み: Adam Danz 2020 年 2 月 19 日
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 件のコメント
Megan
Megan 2020 年 2 月 19 日
I cant add a title.
Do you know why?
Adam Danz
Adam Danz 2020 年 2 月 19 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeBar Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by