How can I gradient the legend color in MATLAB?
19 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Scott MacKenzie
le 6 Mai 2021
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?
Réponses (1)
DGM
le 8 Mai 2021
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 commentaires
Voir également
Catégories
En savoir plus sur Bar Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!