# Bar() very thin, does not respond to width argument

20 ビュー (過去 30 日間)
Maciej 2024 年 5 月 18 日 18:24
コメント済み: Stephen23 2024 年 5 月 18 日 19:08
I have a problem with Bar() function, look at the screenshot, the bars are very thin. Note that there are no leftover trash from previous work, this is fresh example:

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

### 採用された回答

Stephen23 2024 年 5 月 18 日 18:41

"I have a problem with Bar() function"
No, you have a problem with ZEROS() function. In particular you have only specified ZEROS(10) which creates a 10x10 matrix. You then fill the first 10 of those 100 elements (i.e. the first column) with some random numbers... but 90% of your data are zero, i.e. all remaining columns are zeros. You can read the BAR() documentation yourself to know what it does with a matrix input: in short you told it to plot 10 bars in each bin, but 9 of those bars are zero... BAR is showing you exactly what you told it to plot.
What you should have done is preallocated with e.g. ZEROS(1,10) to give a vector.
M = zeros(10,10); % what you did -> 10x10 matrix
M(:,1) = rand(10,1);
bar(M,0.9)
M = zeros(10,1); % what you should have done -> 10 element vector
M(:,1) = rand(10,1);
bar(M,0.9)
Two important steps when debugging your own code:
• look at the data. Do not rely on what you have in your head (your computer does not care about what you think/believe it is doing), you have to actually check the data yourself.
• read the documentation for every operator you use.
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Stephen23 2024 年 5 月 18 日 19:08
"... does not respond to width argument"
Yes, it does. Because you told BAR() to plot ten bars in each bin it makes the difference slightly harder to discern, but the width option certainly works as documented:
M = zeros(10,10); % what you did -> 10x10 matrix
M(:,1) = rand(10,1);
nexttile
bar(M,1.0)
nexttile
bar(M,0.1)
So contrary to what you stated in the title, the width option is clearly working exactly as documented.

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

### カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

R2024a

### Community Treasure Hunt

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

Start Hunting!

Translated by