Hello, I want to make a bar graph in matlab but the bars only showing for an interval in y for some x. For example. For example for x=1 I would like to create a bar that starts in y=3 and ends in y=5. For x=2 I would like to create a bar that starts from y=5 and ends in y=15.
Can you help me? Here's a picture to explain better what I'm looking for
Thanks In Advance,

 採用された回答

Star Strider
Star Strider 2014 年 11 月 14 日
編集済み: Star Strider 2014 年 11 月 14 日

0 投票

The rectangle function can come to your rescue:
w = 0.5; % Rectangle Width
figure(1)
axis([0 4 0 20])
rectangle('Position',[1-w/2 1 0.5 4], 'LineWidth',2)
rectangle('Position',[2-w/2 5 0.5 10], 'LineWidth',2)
rectangle('Position',[3-w/2 10 0.5 5], 'LineWidth',2)
produces:

6 件のコメント

Star Strider
Star Strider 2014 年 11 月 14 日
Rui’s Answer moved here...
But for example I can also have for x=1 two intervals. One from y=3 to y=5 and another from y=8 to y=12. Is it possible to do a graph like that in matlab?
Star Strider
Star Strider 2014 年 11 月 14 日
Yes. Add a rectangle call, for instance:
rectangle('Position',[1-w/2 8 0.5 4], 'LineWidth',2)
producing this plot:
Rui
Rui 2014 年 11 月 14 日
編集済み: Rui 2014 年 11 月 14 日
Yes, that could definetely do what I want. And in the x axis can I put names from a cell array instead of numbers?
Thank you so much for your help
Star Strider
Star Strider 2014 年 11 月 14 日
My pleasure!
The rectangles are already aligned with the y-axis.
Relabeling the x-axis is relatively straightforward. You have to define where the 'XTick' values are (for example {1 2 3}, and supply a matching array of 'XTickLabels'. See the documentation for Axis Properties for details. MATLAB will place them correctly. This varies by version, with significantly different and new options in R2014b, so see the documentation for your version of MATLAB for the correct syntax.
—
(The most sincere expression of thanks here on MATLAB Answers is to Accept the answer that most closely solves your problem.)
Rui
Rui 2014 年 11 月 14 日
Thank you so much! You helped me a lot!
Best Regards
Star Strider
Star Strider 2014 年 11 月 14 日
My pleasure!

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

その他の回答 (1 件)

カテゴリ

ヘルプ センター および File Exchange で Discrete Data Plots についてさらに検索

質問済み:

Rui
2014 年 11 月 14 日

コメント済み:

2014 年 11 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by