フィルターのクリア

How to draw a line in a bar chart in complete x area

2 ビュー (過去 30 日間)
Richard
Richard 2024 年 3 月 31 日
編集済み: Voss 2024 年 4 月 1 日
Is there a way to make the horizontal line start from the very left and go to the very right? It's only going from the first x-marker to the last x-marker.
my code:
cat=categorical({'a','b','c'})
data = [37.6 24.5 14.6]';
errhigh = [2.1 4.4 0.4];
errlow = [4.4 2.4 2.3];
bar(cat,data)
hold on
er = errorbar(cat,data,errlow,errhigh);
er.Color = [0 0 0];
er.LineStyle = 'none';
hold off
line(xlim,[20 20])
output diagramm:
Thanks.

回答 (2 件)

Voss
Voss 2024 年 3 月 31 日
cats=categorical({'a','b','c'})
cats = 1x3 categorical array
a b c
data = [37.6 24.5 14.6]';
errhigh = [2.1 4.4 0.4];
errlow = [4.4 2.4 2.3];
bar(cats,data)
hold on
er = errorbar(cats,data,errlow,errhigh);
er.Color = [0 0 0];
er.LineStyle = 'none';
hold off
% force graphics to update so XLim is accurate
drawnow()
% prevent auto-updating of XLim when new line is made
set(gca(),'XLimMode','manual')
% make the new line
line([0 numel(cats)+1],[20 20])
I changed the variable cat to cats, since cat is the name of an important built-in function.
  2 件のコメント
Richard
Richard 2024 年 3 月 31 日
I tried your code. It still gives me the line only from the middle of the bars.
Is there a setting or something for this?
Voss
Voss 2024 年 3 月 31 日
編集済み: Voss 2024 年 4 月 1 日
The line goes all the way across when I run it on my desktop (R2023b). See below:

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


Star Strider
Star Strider 2024 年 3 月 31 日
The easiest option is to use the yline function —
cat=categorical({'a','b','c'})
data = [37.6 24.5 14.6]';
errhigh = [2.1 4.4 0.4];
errlow = [4.4 2.4 2.3];
bar(cat,data)
hold on
er = errorbar(cat,data,errlow,errhigh);
er.Color = [0 0 0];
er.LineStyle = 'none';
hold off
yline(20) % Use 'yline'
The Run feature is not working for some reason just now, ot it woudl be possible to demonstrate this here.
.
  2 件のコメント
Richard
Richard 2024 年 3 月 31 日
Thank you. Is there a different method? I want to make the line only appear in certain areas of x-values.
Star Strider
Star Strider 2024 年 3 月 31 日
My pleasure!
You can do something like this —
cat=categorical({'a','b','c'})
data = [37.6 24.5 14.6]';
errhigh = [2.1 4.4 0.4];
errlow = [4.4 2.4 2.3];
bar(cat,data)
hold on
er = errorbar(cat,data,errlow,errhigh);
er.Color = [0 0 0];
er.LineStyle = 'none';
% plot([0 1], [1 1]*20) % Added
plot([0 1; 2 3; 4 5].', ones(3,2).'*20) % All-In-One
hold off
however the placement requires a bit of experimentation, since numeric values of the ‘x’ axis aren’t precisely defined here. (The return from an xlim call is [a c] for example.) If you want them all on the same y-value, you can combine them as in the ‘All-In-One’ plot call, otherwise, it might be easier to use separate plot calls, similar to the commented-out version.
.

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

カテゴリ

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

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by