Plotting bar graphs with different colors

29 vues (au cours des 30 derniers jours)
Nitin Arora
Nitin Arora le 22 Nov 2021
Modifié(e) : dpb le 27 Nov 2021
I have a 2D array A with 10 rows and 2 columns. In the first coloum data is in sorted form and in the 2nd column indexed are stored from 1 to 10 in random order.
0 1
55.49483 6
56.04042 2
56.13312 3
57.2423 7
57.26352 4
60.63905 8
63.84518 10
64.23292 9
66.96066 5
I want to plot a bar graph in a way if A(:,2)== rang from 1 to 5 then bar graph of red color else blue color.

Réponse acceptée

dpb
dpb le 22 Nov 2021
Modifié(e) : dpb le 22 Nov 2021
I've railed over bar interface for 20+ years -- this is harder than it should be even though there is an example in the doc, it's pretty heavy lifting for the newbie...the bar colors should be addressable properties without all the gyrations of color maps, CData arrays, etc., etc....
That aside, with a little knowledge (well maybe quite a bit) about how handle graphics works along with the more arcane usage of set, the end code can be pretty short...
c=([[1 0 0];[0 0 1]]); % red, blue rgb triplets
hB=bar(A(:,1),'FaceColor','flat'); % create base bar, save handle
set(hB,{'CData'},{c((A(:,2)>5)+1,:)}) % set CData triplet based on second column of A
You can modify the colormap rgb triplets to use other than the full bright hues to not be quite so garish, just adjust the values in the c array to suit. This, of course, is not such an easy thing to know just what would be a great set of numbers to use just looking, either....
The other example that shows setting bar colors to colormap indices, while it does use a grouped bar plot style, doesn't make it explicit that that form works only for grouped bars or for all colors in a bar series. Behind the scenes, if you turn the 'CData' triplet into an integer, that becomes the data content for the entire bar handle--you then lose the accessiblity to the array of 'CData' rgb triplets by individual bar within the series. Consequently, using the rgb triplet is the only way to accomplish this particular goal.
It is arcane at best...
  2 commentaires
Star Strider
Star Strider le 22 Nov 2021
There may also be a legend involved (that added requriement surprised me as well), so the code needs to accommodate that potential added requirement.
dpb
dpb le 22 Nov 2021
Modifié(e) : dpb le 27 Nov 2021
Indeed. The limitations in legend that it only has entries for something that has a handle in the axis precludes using only one bar series if it is required to have a legend -- which is certainly also a perfectly reasonable expectation/need.
It can be done with another one of the handle graphics "tricks" -- use a set of objects with NaN for data so that the handles exist and can be maniuplated, but the data aren't visible. Again, not for the faint of heart and illustrative of the compexity within HG in general and bar in particular.
B=[randi(10,10,1) randi(2,10,1)];
hB=bar(B(:,1),'FaceColor','flat');
c=[[1 0 0];[0 0 1]];
set(hB,{'CData'},{c((B(:,2)>1)+1,:)})
% same as above to here...now add the dummy grouped bar for the legend
hold on
hBX=bar(nan(2)); % bar will create 2 groups, not visible
set(hBX,{'FaceColor'},{'r';'b'}) % set their colors to match
hLg=legend(hBX,'Level 1','Level 2', ...
'location','eastoutside'); % add the legend to those two
Above yielded
It would be possible to only add the second single bar and put the legend on hB and hBX, but then one has to ensure get the colors associated as wanted because the legend will pick the first bar color for only one handle and it could be either.
All in all, I still contend the bar user interface is an abominable thing to have to use for anything much beyond the ordinary...but many of those things are, like this, perfectly reasonable expectations and shouldn't be nearly so obtuse.
It makes for entertaining Answers forum exercises, but is a complete waste of real users' time in accomplishing their work.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by