How do I change the colors in a legend on a bar plot when I change the colors of the bars?

56 ビュー (過去 30 日間)
I am having some problems with adapting a legend to a bar plot.
I have changed the colors of the bars, but when creating the legend I just have the option of putting one. When I put more entries I get the message
'Ignoring extra legend entries'
I have found in some other posts a way of putting more extra legends, but the problem is that they all have the same color and I have not found the way to change that!
Here is an example of what I mean:
data = rand(1,12);
def2=3:5;
def3=6:9;
def4=10:12;
index = ones(1,length(data));
index(def2)=2;
index(def3)=3;
index(def4)=4;
bar_h=bar(data);
bar_child=get(bar_h,'Children');
mycolor=[0 1 0;0 0 1;1 0 0;0 1 1];
set(bar_child, 'CData',index);
colormap(mycolor);
In this way I have the bars in the different colors I want. Now I would like to have a legend with four entries specifying the four different colors and the legend (for example) 'a','b','c','d'...
What I have found in other posts is:
ch=repmat(bar_child,1,4);
le={'a','b','c','d'};
legend(ch,le);
But I just get the same color in all the entries!
  2 件のコメント
Todd Flanagan
Todd Flanagan 2011 年 1 月 20 日
Ana, I moved your reply to Walter into a comment to his answer.
Ana
Ana 2011 年 1 月 22 日
Thanks Todd, it was my first time in Matlab answers and after writing that as an answer I did realize that it should be a comment on the previous answer not an answer itself but I didn't know it could be changed. Thanks!

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

採用された回答

Walter Roberson
Walter Roberson 2011 年 1 月 20 日
You cannot do what you want using legend when you use bar() with a row or column vector. Internals of how legends are handled are described in MATLAB documentation.
We can see through examination of your code that only a single barseries object is being produced and it will have only a single patch object as its child. The documentation referenced earlier shows that only a single legend slot is available for this situation. You can get multiple lines for the legend but there is not enough control information available to assign them different colors.
What you should be doing instead is using multiple bar() calls, one per group. You can specify the y values to be associated with the entries so that they will all appear on one graph, and you can legend() the groupings.
  2 件のコメント
Todd Flanagan
Todd Flanagan 2011 年 1 月 20 日
Ana says, "Thank you very much for the answer, I still don't understand very well how I have to associate the y values for all the bar plots to appear in one graph but I will have a look at it tonight and post again if I have any further question. I suppose that once I am able to plot various bar plots in the same graph I would be able to put a legend with more entries, one for each group and color.
Thanks!"
Ana
Ana 2011 年 1 月 22 日
Using different bar() calls in this way:
bar(def1,data(def1),'g')
hold on
bar(def2,data(def2),'b')
bar(def3,data(def3),'r')
bar(def4,data(def4),'c')
legend('a','b','c','d')
I have managed to do what I wanted! I am not sure that this is the best way, but it is much more than I have achieved until now! Thank you very much!

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

その他の回答 (2 件)

Doug Hull
Doug Hull 2011 年 1 月 20 日
The ability to change the color in each bar of a plot is not built into MATLAB.
As a workaround, you can return handles to the patch objects that define the bars, and change their colors using Handle Graphics. If you are using MATLAB 7.0 (R14) or later, you will need to use the 'v6' option to the BAR function to obtain handles to patch objects. In earlier versions, this is not necessary.
Y=[ 1 2 3 ; 4 5 6 ; 3 4 5];
h = bar('v6',Y);
set(h(1),'facecolor','red') % use color name
set(h(2),'facecolor',[0 1 0]) % or use RGB triple
set(h(3),'facecolor','b') % or use a color defined in the help for PLOT
Note: This would not have any effect on the legend colors. Hence you would need to change the legend colors similarly. You can get the handle of the legend and look for the children.
  3 件のコメント
Ana
Ana 2011 年 1 月 22 日
One of the problems I have is that not all the groups have the same size, so I cannot arrange them in a matrix,...
Frank Schumann
Frank Schumann 2023 年 6 月 1 日
Mathworks, could you update your bar plot to make this possible in the future? It seems such an essential function to colour individual bars and put them on a legend.
These workaround, as happy as I am to find them, are so time consuming and error prone.

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


Haris
Haris 2012 年 4 月 6 日
I have found a way to display the bars with different colors. Let say that you have 4 measurements a, b, c and d that you want to display with different color each:
a = 1;
b = 2;
c = 3;
d = 4;
if you represent these values as
a2 = [ a 0 0 0 ];
b2 = [ 0 b 0 0 ];
c2 = [ 0 0 c 0 ];
d2 = [ 0 0 0 d ];
then you can display them with different colors:
figure;
set(gcf,'Color',[1,1,1]);
hold on;
bar(a2);
bar(b2,'g');
bar(c2,'r');
bar(d2,'y');
legend('a', 'b', 'c', 'd');

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by