How can I gradient the legend color in MATLAB?
조회 수: 19 (최근 30일)
이전 댓글 표시
I have a 3D-bar MATLAB code which represents data for 3-folds, as shown in the attached figure.
How can I gradient the legend color, ranging from #FFFF00 to #F68C00 and from #00FEEF to #3092E3, as shown in the big red box?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/588111/image.png)
댓글 수: 2
Scott MacKenzie
2021년 5월 6일
It would help if you attached the code that generated this chart. Also, do you only want the color gradient in the legend or do you also want the color of the bars in the chart to follow the gradient?
답변 (1개)
DGM
2021년 5월 8일
I don't know that there's a good way to do this, but there's probably a better way than the garbage I came up with.
%hl = legend('','','','', 'Actual','Predicted'); %Taking the color of the center fold
pleg = [0.8559 0.8220 0.08 0.1030];
txpos = 0.45;
pt1 = [pleg(1)+pleg(3)*txpos pleg(2)-pleg(4)*0.12 0.1 0.1];
pt2 = [pleg(1)+pleg(3)*txpos pleg(2)-pleg(4)*0.52 0.1 0.1];
a1 = annotation('rectangle',pleg,'facecolor','w');
t1 = annotation('textbox',pt1,'string','Actual','edgecolor','none','fitboxtotext','on');
t2 = annotation('textbox',pt2,'string','Predicted','edgecolor','none','fitboxtotext','on');
bh = 0.025;
bw = 0.008;
a11 = annotation('rectangle',[pleg(1)+pleg(3)*0.1 pleg(2)+pleg(4)*0.17 bw bh],'facecolor','#FFFF00','edgecolor','none');
a12 = annotation('rectangle',[pleg(1)+pleg(3)*0.1+bw pleg(2)+pleg(4)*0.17 bw bh],'facecolor','#FBC600','edgecolor','none');
a13 = annotation('rectangle',[pleg(1)+pleg(3)*0.1+2*bw pleg(2)+pleg(4)*0.17 bw bh],'facecolor','#F68C00','edgecolor','none');
a21 = annotation('rectangle',[pleg(1)+pleg(3)*0.1 pleg(2)+pleg(4)*0.60 bw bh],'facecolor','#00FEEF','edgecolor','none');
a22 = annotation('rectangle',[pleg(1)+pleg(3)*0.1+bw pleg(2)+pleg(4)*0.60 bw bh],'facecolor','#18C8E9','edgecolor','none');
a23 = annotation('rectangle',[pleg(1)+pleg(3)*0.1+2*bw pleg(2)+pleg(4)*0.60 bw bh],'facecolor','#3092E3','edgecolor','none');
This kind of works, but it's fragile.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/611375/image.png)
If you ask me, 3D bar charts are a universally poor way to visualize anything, especially small differences between sets.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Bar Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!