How to adjust the (absolute) width of bars to 1 unit in a bar plot?

8 ビュー (過去 30 日間)
Sim
Sim 2023 年 11 月 1 日
コメント済み: Sim 2023 年 11 月 1 日
How to adjust the (absolute) width of bars to 1 unit in a bar plot?
In the following example, the bars are larger than 1 unit:
clear all;clc;close all;
a=[2 6 15 63];
b=[23 4 2 1];
bar(a,b,'FaceColor',[0.5 0.5 0.5],'FaceAlpha',0.2)
xlim([0 Inf])
I mean, even though we set the relative bar width (bar(___,width)) equal to 1, we do not get 1 unit large columns. I guess because we are talking about the relative bar width and not about the absolute bar width.
bar(a,b,1,'FaceColor',[0.5 0.5 0.5],'FaceAlpha',0.2)
xlim([0 Inf])
For example, I would expect that the column centered at tick 2 spans between 1.5 and 2.5.
  6 件のコメント
Mann Baidi
Mann Baidi 2023 年 11 月 1 日
Update the EdgeColor by
x.EdgeColor=[1 1 1]; % for white edges [0 0 0] for black edges
Sim
Sim 2023 年 11 月 1 日
great, thanks a lot! :-)

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

採用された回答

Dyuman Joshi
Dyuman Joshi 2023 年 11 月 1 日
"I would expect that the column centered at tick 2 spans between 1.5 and 2.5, i.e. 1 unit "
That will be quarter of the width available for a bar -
%Modified values to get a better visual
a=[2 6 15];
b=[23 4 2];
bar(a,b,0.25,'FaceColor',[0.5 0.5 0.5],'FaceAlpha',0.2)
xlim([0 Inf])
vec = [a-0.5;a;a+0.5];
%Adding ticks for visual verification
xticks(vec(:))
ax=gca;
ax.FontSize = 9;
ax.XTickLabelRotation = 90;
  4 件のコメント
Sim
Sim 2023 年 11 月 1 日
Thanks a lot @Dyuman Joshi for your comments! Yes, I would like to have a general solution..... I guess it would be the following one, right?
bar(a,b,1,'FaceColor',[0.5 0.5 0.5],'FaceAlpha',0.2)
xticks(unique(vec))
Sim
Sim 2023 年 11 月 1 日
@Mann Baidi and @Dyuman Joshi, many thanks for your solutions, which are both nice :-)

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

その他の回答 (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