I'm trying to figure out how to plot a range of y values as bars. For example if my x-axis is days of the month, my bars would represent the range between the max and min temperatures for that day. The bar would be the shaded portion between the min and max values for each day. Kind of like a boxplot without all the statistical information included. Can't figure out how to do this. Any suggestions?

 採用された回答

Star Strider
Star Strider 2018 年 8 月 22 日

7 投票

One approach:
Tmax = randi([25 30], 1, 10); % Create Data
Tmin = randi([15 20], 1, 10); % Create Data
days = 1:10; % Create Data
figure
plot([days; days], [Tmin; Tmax], 'LineWidth',5)
xlabel('Day')
ylabel('Temperature Range (°C)')
ylim([10 40])
xlim([0 11])
grid
Experiment to get the result you want.

10 件のコメント

Thomas Burbey
Thomas Burbey 2018 年 8 月 22 日
That's what I want. Thanks so much!!
Star Strider
Star Strider 2018 年 8 月 22 日
As always, my pleasure!
dpb
dpb 2018 年 8 月 22 日
Clearly the best solution -- good catch to not get caught by the red herring of "bar" that the rest of us fell into...the "too much information" bugaboo.
Star Strider
Star Strider 2018 年 8 月 22 日
Thank you!
I thought about errorbar first, and when I tested it, didn’t like the uneven look of the bars, so I went for (what I consider to be) the best alternative.
Meghashree Srikantaiah Manjesh
Meghashree Srikantaiah Manjesh 2020 年 1 月 20 日
You saved me @star strider. Thank you very much
Eduardo Almeida Benalcázar
Eduardo Almeida Benalcázar 2020 年 5 月 22 日
Simple and does the job.
Adusumilli Bala Surendra
Adusumilli Bala Surendra 2020 年 9 月 17 日
@star strider This is the most easier way. Thanks mate!
Star Strider
Star Strider 2020 年 9 月 17 日
Thank you!
My pleasure!
Brendan Görres
Brendan Görres 2020 年 10 月 16 日
What if i want the days to be a string? So for exaple instead of 1 it would give out 'Jan 1st'
Star Strider
Star Strider 2020 年 10 月 16 日
Brendan Görres — With this code, it would be necessary to plot against datenum numbers, then use datetick. (Note that I have not done that experiment.)
I am not certain how (or if) this code would work with datetime vectors, and I have not used it with them. See the documentation section on Plot Dates and Durations for details on plotting with them. If you want to do that, post it as a new Question.

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

その他の回答 (4 件)

dpb
dpb 2018 年 8 月 22 日
編集済み: dpb 2018 年 8 月 22 日

1 投票

OK, the bar option works pretty easily, actually, with some handle-diving for how things are organized.
% first make up some data...
x=1:30; % 30 days hath...
y=[randi(90,size(x)); % max, min temp distributions
randi(70,size(x))];
ix=diff(y)<0; % fixup to make sure all
y(:,ix)=flipud(y(:,ix)); % max are > min
To use bar to get scaled as between min/max for the second bar, the bar definitions are intervals [0 L] and [L H-L] so the sum or max of second bar in the stacked plot is L+(H-L) = H instead of H+L if plot L,H as normal.
hB=bar([y(1,:); diff(y)].', ...
'stacked','FaceColor','flat','EdgeColor','w');
hB(1).CData=ones(length(x),3); % set the color of bottom to background (white)
ends up with
In the end, this doesn't seem much more effort to fix up actual temperature min/max data by the difference than would be to compute the error values for the errorbar solution. It's a little more abstract in needing to set the color for the one bar so with Steven's observation of the new property for the error bar ends lengths being able to be set, that's probably the better solution.
BTW, the idea of simply
delete(hB(1))
to clear the first bar instead of rewriting the 'CData' doesn't work-- bar then turns the plot into a standard bar plot with zero baseline; doesn't keep the 'stacked' property any longer.

4 件のコメント

Thomas Burbey
Thomas Burbey 2018 年 8 月 22 日
Okay, can you explain what your CData look like? thanks.
dpb
dpb 2018 年 8 月 22 日
Just the RGB 3-vector of [1 1 1] for 'w' for each element of the CData array for the first bar object which is the bottom bar of the two stacked bars...there are two bar object handles, not 30; the two objects are the bottom and top pieces, not a two-segmented bar for each day if that makes sense?
If you have changed the graph background color from white, then you would retrieve it and use its RGB triplet instead.
OH!!! Just had idea -- wonder if one couldn't just delete the one set of handles instead...
I did also figure out how to do the scaling correctly; I'll amend the above ANSWER to correct that earlier oversight when get back home -- just looked in again after cleaning up to head to town but have got to get gone now...the idea is to subtract the low from the high so the second bar is the difference; then the sum is the total and the top bar will range over the min-max, not max-total as is by default.
dpb
dpb 2018 年 8 月 22 日
編集済み: dpb 2018 年 8 月 22 日
[Thomas Answer moved to comment...dpb]
I still get an error with the line: hB(1).CData =ones(length(x),3) as it doesn't know what CData is. I'm assuming this is trying to access the first column in the hB bar workspace. Is this a version issue? I'm running R2016b? Otherwise I get what you're doing. It seems to me that the bar function is simply too limited. I'd think this would be a fairly common practice to want to do what I'm asking here.
dpb
dpb 2018 年 8 月 22 日
編集済み: dpb 2018 年 8 月 22 日
As noted before, hB(1) is the bottom bar; hB(2) is the top; the bar objects aren't by column but by level for 'stacked'
On 'CData', indeed the bar object was redefined sometime after R2016b; I am on R2017b here. For R2016b, modify the above to
hB=bar([y(1,:); diff(y)].','stacked','EdgeColor','w');
hB(1).FaceColor='w'; % set the color of bottom to background (white)
But, it seems to me that while interesting to show what can manage to be done with enough creativity, that Star's simple plot solution is the best way to go, isn't it?
And, yes, I fully agree that bar is terribly limited in both what it can do "out of the box" easily and in subsequently trying to mung on it to do obviously needed/wanted things. I've complained for 20+ years there should be something like 'baseline' on a column basis.

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

Yuvaraj Venkataswamy
Yuvaraj Venkataswamy 2018 年 8 月 22 日

0 投票

1 件のコメント

Thomas Burbey
Thomas Burbey 2018 年 8 月 22 日
All the regular bar graphs start at the axis, there's no way to enforce a range where the bar doesn't extend to the axis, but rather the minimum value specified.

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

Yuvaraj Venkataswamy
Yuvaraj Venkataswamy 2018 年 8 月 22 日

0 投票

For example,
if true
x = 1:1:12;
y = [34 23 47 28 41 35 21 18 38 20 30 32];
bar(x,y)
end

2 件のコメント

Thomas Burbey
Thomas Burbey 2018 年 8 月 22 日
This isn't what I'm after. I want the lower value of the bar to be a minimum value I specify. This simply has the lower extent of the bar at the axis y=0.
Yuvaraj Venkataswamy
Yuvaraj Venkataswamy 2018 年 8 月 22 日
編集済み: Yuvaraj Venkataswamy 2018 年 8 月 22 日
You mean,
if true
ylim([min_value max_value])
end
For example, ylim([0 40]);

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

Steven Lord
Steven Lord 2018 年 8 月 22 日

0 投票

It sounds like you want the errorbar function.

3 件のコメント

dpb
dpb 2018 年 8 月 22 日
But afaik, Steven, there's no way to get rid of the end bars on the error bars with errorbar and they're also magnified when 'linewidth' is increased to draw the bar more like a bar or boxplot.
If OP is satisfied with that or the default then it's pretty simple, yes.
Steven Lord
Steven Lord 2018 年 8 月 22 日
For at least the first part of your comment, if you're using release R2016b or later you can use the CapSize property introduced in that release. I just checked in release R2018a and setting CapSize to 0 is allowed.
dpb
dpb 2018 年 8 月 22 日
Ah, in that case, probably simpler than the bar...wasn't aware of that modification/new property.

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

カテゴリ

ヘルプ センター および File ExchangeErrorbars についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by