bar graph : How to show each bar with different base value ?
12 views (last 30 days)
Show older comments
Hi,
I would like to show use bar graph. But each bar has different base line. So, I wrote the following codes. However, it gives error. Could you please help me, how to do this with matlab
my codes are;
figure(1);
x=[1 2 3 4]; y1=[928,1417; 2166,3625; 2278,3401; 1,2]; y2=[197,271; 141,212; 34,150; 1,2];
b1 = bar(x,y1, 'facecolor', 'y'); hold on; b2 = bar(x,y2, 'facecolor', 'b');
b1(1).baseValue=928; % <== it gives error here. How to solve this problem ? b1(2).baseValue=2166; b1(3).baseValue=2278;
0 Comments
Answers (3)
dpb
on 14 Jun 2016
Apparently you've got an earlier release than R2014b, maybe? The ".dot" notation for properties wasn't implemented prior, use set instead--
set(b(1),'basevalue',928); % works fine here (R2012b)
However, the rest will have troubles--
b1(2).baseValue=2166;
the baseline is only a single value for each bar series; hence setting b1(2) to something different than b1(1) will simply replace the first with the second value.
And,
b1(3).baseValue=2278;
there are only two b1 handles so the above will error on a bounds error.
The only way to have a different baseline per bar will be to have only a single bar per bar plot or make the patch objects directly from primitives.
0 Comments
Brad Allen
on 20 Oct 2016
Edited: Brad Allen
on 20 Oct 2016
I was just having this same issue, also couldn't figure it out. It would seem that no matter which way I changed the basevalue (either separate bar charts or using set() or barhandle.BaseValue), it would always change to the last one I set. For example:
data = [1,2,3;4,5,6]';
hbar = bar(data);
set(hbar(1), 'BaseValue', 2);
set(hbar(2), 'BaseValue', 5);
% This results in the BaseValue for both sets = 5
% same happens if you do this
hbar1 = bar(data(:,1),'BaseValue',2);
hold on
hbar2 = bar(data(:,2),'BaseValue',5);
% or if you do this
hbar1 = bar(data(:,1));
hold on
hbar2 = bar(data(:,2));
hbar1.BaseValue = 2;
hbar2.BaseValue = 5;
% It always results in a BaseValue of 5 for both datasets...
It's strange because if you set foo = get(barhandle.BaseValue), you will get a cell array that contains the same basevalue in each cell, but Matlab (R2014b) returns an error if you try to assign a cell array to barhandle.Basevalue.
0 Comments
See Also
Categories
Find more on Line Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!