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
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?
Amal AbdelGawad
Amal AbdelGawad 2021-5-8
Thank you for your response.
I only want the color gradient in the legend, ranging from #FFFF00 to #F68C00 and from #00FEEF to #3092E3.
This is a miniature version of the code:
clear all; close all; clc;
A_F1 = [1:1:5]'; A_F2 = [2:2:10]'; A_F3 = [3:3:16]'; %Actual data of Fold 1, 2, and 3
P_F1 = [2:1:6]'; P_F2 = [3:2:11]'; P_F3 = [4:3:18]'; %Predicted data of Fold 1, 2, and 3
Z = zeros(length(A_F1(:)),2); % 2 XTicks to be hided (to separate the folds visually)
figure;
b = bar3([A_F1,P_F1, ...
Z,A_F2,P_F2, ...
Z,A_F3,P_F3], 0.8); %"0.8" to separate the bars visually
b(1).FaceColor='#FFFF00'; b(5).FaceColor='#FBC600'; b(9).FaceColor='#F68C00'; %Yellow gradient for Actual
b(2).FaceColor='#00FEEF'; b(6).FaceColor='#18C8E9'; b(10).FaceColor='#3092E3'; %Blue gradient for Predicted
for i=3:1:4 b(i).FaceColor='none'; b(i).LineStyle='none'; end %The 1st 2 XTicks to be hided (btn. Fold 1 & 2)
for i=7:1:8 b(i).FaceColor='none'; b(i).LineStyle='none'; end %The 2nd 2 XTicks to be hided (btn. Fold 2 & 3
set(gca,'XTick', [1,5,9]');
set(gca,'XTickLabels', ['1','2','3']');
xlabel ('Fold No.','fontweight','b');
ylabel ('Instant No.','fontweight','b');
zlabel ('Data','fontweight','b');
legend('','','','', 'Actual','Predicted'); %Taking the color of the center fold
hold off;
It is producing this chart:

请先登录,再进行评论。

回答(1 个)

DGM
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.

类别

Help CenterFile Exchange 中查找有关 Data Distribution Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by