How to bar plot legend with group by color ?
2 次查看(过去 30 天)
显示 更早的评论
Hi all ,I want plot bar graph and show legend with group by color.but now I not sure which I mistake.
when I use command "legend()" The result is as shown in the picture.
## Bar plot
T4 = DAtavi.Turbidity;
y4 = month(DAtavi.Date);
[Uy4,~,Ix] = unique(y4);
T4Max = accumarray(Ix, T4, [], @max);
bar(Uy4, T4Max)
name = {'Jan';'Feb';'Mar';'Apr';'May';'Jun';'Jul';'Aug';'Sep';'Oct';'Nov';'Dec'};
set(gca,'XTick',1:12,'xticklabel',name);
grid on;
hold on;
# Set color
set(bar(Uy4(1), T4Max(1)),'FaceColor','g');
set(bar(Uy4(2), T4Max(2)),'FaceColor','g');
set(bar(Uy4(3), T4Max(3)),'FaceColor','r');
set(bar(Uy4(4), T4Max(4)),'FaceColor','r');
set(bar(Uy4(5), T4Max(5)),'FaceColor','r');
set(bar(Uy4(6), T4Max(6)),'FaceColor','r');
set(bar(Uy4(7), T4Max(7)),'FaceColor','b');
set(bar(Uy4(8), T4Max(8)),'FaceColor','b');
set(bar(Uy4(9), T4Max(9)),'FaceColor','b');
set(bar(Uy4(10), T4Max(10)),'FaceColor','b');
set(bar(Uy4(11), T4Max(11)),'FaceColor','g');
set(bar(Uy4(12), T4Max(12)),'FaceColor','g');
legend()
0 个评论
采纳的回答
Ameer Hamza
2020-11-14
Here is a crude way to solve this problem
T4 = DAtavi.Turbidity;
y4 = month(DAtavi.Date);
[Uy4,~,Ix] = unique(y4);
T4Max = accumarray(Ix, T4, [], @max);
name = {'Jan';'Feb';'Mar';'Apr';'May';'Jun';'Jul';'Aug';'Sep';'Oct';'Nov';'Dec'};
ax = axes();
grid on;
hold on;
hb1 = bar(Uy4(1), T4Max(1), 'FaceColor','g');
bar(Uy4(2), T4Max(2), 'FaceColor','g')
hb2 = bar(Uy4(3), T4Max(3), 'FaceColor','r');
bar(Uy4(4), T4Max(4), 'FaceColor','r')
bar(Uy4(5), T4Max(5), 'FaceColor','r')
bar(Uy4(6), T4Max(6), 'FaceColor','r')
hb3 = bar(Uy4(7), T4Max(7) ,'FaceColor','b');
bar(Uy4(8), T4Max(8) ,'FaceColor','b')
bar(Uy4(9), T4Max(9) ,'FaceColor','b')
bar(Uy4(10), T4Max(10) ,'FaceColor','b')
hb4 = bar(Uy4(11), T4Max(11), 'FaceColor','g');
bar(Uy4(12), T4Max(12), 'FaceColor','g')
legend([hb1 hb2 hb3 hb4])
set(gca,'XTick',1:12,'xticklabel',name);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!