How can I gradient the legend color in MATLAB?
35 次查看(过去 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?
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.
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 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!