我正在开发一个 MATLAB 项目,其中有两组数据:
1-“无反射”:表示基线覆盖区域值。 2- “With Reflection”:表示具有附加反射效果的覆盖区域值。
我将这两个数据集绘制为分组条形图,我想突出显示反射引起的差异。具体来说,我需要每个条中与差异(反射效果)相对应的部分在“无反射”条顶部以不同的颜色突出显示。
示例: 例如,如果:
我想要:
1- 绘制一个代表 120 m² 的条形(“无反射”的基值)。
2- 以红色突出显示蓝色条顶部的差异 (160 - 120 = 40 m²)(突出显示反射效果)。
我想将其应用到右侧图像上的所有条形上。请问有什么帮助吗?
这是我的代码:
close all;
clear variables;
clc;
QAM = [16, 32, 64, 128, 256, 512, 1024];
%% m_CAP modulation
% Beamsteering without reflection
VEC_Beam_no_reflection_mCAP = [
117.5491 75.1933 53.5396 38.3105 23.0815 13.5634 6.9007; % Theta = 10
118.2629 76.1452 54.0155 39.0244 23.0815 13.5634 6.9007; % Theta = 30
118.2629 76.3831 54.0155 39.5003 22.8435 13.5634 6.9007; % Theta = 45
118.2629 76.6211 54.2534 39.5003 22.8435 13.5634 6.9007 % Theta = 60
];
% Beamsteering with reflection
VEC_Beam_reflection_mCAP = [
121.3563 77.0970 54.7293 39.7383 22.8435 13.5634 6.9007; % Theta = 10
141.1065 88.7567 61.6300 42.5937 24.2713 14.5152 7.8525; % Theta = 30
153.7180 95.6573 67.8168 45.6871 26.6508 14.5152 8.8043; % Theta = 45
160.3807 100.6544 69.7204 50.4462 28.7924 16.4188 8.8043 % Theta = 60
];
%% DCO-OFDM modulation
% Beamsteering without reflection
VEC_Beam_no_reflection_OFDM = [
91.3742 59.7264 42.1178 28.7924 16.4188 8.8043 4.9970; % Theta = 10
95.4194 58.2986 34.7412 25.9369 16.4188 10.7079 4.9970; % Theta = 30
89.4706 59.4884 44.4973 24.7472 16.4188 8.8043 4.9970; % Theta = 45
83.5217 69.4825 40.2142 28.7924 16.4188 10.7079 4.9970 % Theta = 60
];
% Beamsteering with reflection
VEC_Beam_reflection_OFDM = [
94.7055 61.8679 42.1178 28.7924 16.4188 8.8043 4.9970; % Theta = 10
113.9798 69.7204 42.1178 28.7924 16.4188 10.7079 4.9970; % Theta = 30
115.6454 74.9554 55.6811 28.7924 16.4188 9.7561 4.9970; % Theta = 45
114.4557 88.7567 50.6841 34.5033 21.1779 11.6597 4.9970 % Theta = 60
];
%% Combine Data for Bar Chart
angles = [10, 30, 45, 60];
data_no_reflection = [VEC_Beam_no_reflection_mCAP; VEC_Beam_no_reflection_OFDM];
data_with_reflection = [VEC_Beam_reflection_mCAP; VEC_Beam_reflection_OFDM];
% Generate gradient colors for m-CAP (blue) and DCO-OFDM (green)
mCAP_blue_gradient = [linspace(0.8, 0.1, 4)', linspace(0.9, 0.2, 4)', linspace(1.0, 0.3, 4)']; % Light to dark blue
DCO_green_gradient = [linspace(0.7, 0.1, 4)', linspace(0.9, 0.4, 4)', linspace(0.6, 0.2, 4)']; % Light to dark green
% Combine colors
custom_colors = [mCAP_blue_gradient; DCO_green_gradient];
% Plot for No Reflection
figure('Position', [100, 100, 800, 600]);
b1 = bar(data_no_reflection', 'grouped');
for k = 1:length(b1)
b1(k).FaceColor = 'flat';
b1(k).CData = repmat(custom_colors(k, :), size(b1(k).CData, 1), 1);
end
xticks(1:length(QAM));
xticklabels(string(QAM));
xlabel('QAM Order, M');
ylabel('Coverage Area (m²)');
title({'Coverage Area Without Reflection', 'm-CAP and DCO-OFDM'});
legend(["m-CAP \Phi_{1/2}=10°", "m-CAP \Phi_{1/2}=30°", "m-CAP \Phi_{1/2}=45°", "m-CAP \Phi_{1/2}=60°", ...
"DCO-OFDM \Phi_{1/2}=10°", "DCO-OFDM \Phi_{1/2}=30°", "DCO-OFDM \Phi_{1/2}=45°", "DCO-OFDM \Phi_{1/2}=60°"], ...
'Location', 'Best');
grid on;
% Plot for With Reflection
figure('Position', [100, 100, 800, 600]);
b2 = bar(data_with_reflection', 'grouped');
for k = 1:length(b2)
b2(k).FaceColor = 'flat';
b2(k).CData = repmat(custom_colors(k, :), size(b2(k).CData, 1), 1);
end
xticks(1:length(QAM));
xticklabels(string(QAM));
xlabel('QAM Order, M');
ylabel('Coverage Area (m²)');
title({'Coverage Area With Reflection', 'm-CAP and DCO-OFDM'});
legend(["m-CAP \Phi_{1/2}=10°", "m-CAP \Phi_{1/2}=30°", "m-CAP \Phi_{1/2}=45°", "m-CAP \Phi_{1/2}=60°", ...
"DCO-OFDM \Phi_{1/2}=10°", "DCO-OFDM \Phi_{1/2}=30°", "DCO-OFDM \Phi_{1/2}=45°", "DCO-OFDM \Phi_{1/2}=60°"], ...
'Location', 'Best');
grid on;
如果您没有创建分组条形图,您可以将它们制作为堆叠条形图,其中堆栈的顶部部分等于两者之间的差值。
由于您使用的是分组图表,一种简单的方法是首先绘制一组
axes
中较高的条形(整个条形为红色),然后将其他条形绘制在没有标签的新 axes
上,并且透明背景。
您需要确保它们具有相同的轴限制,以便条形图正确对齐。您可以使用
linkaxes
或仅设置 ylim
来完成此操作。请注意,您只需要对第二个轴执行诸如标题、xticks 和图例之类的操作。
从你的例子来看,它看起来像这样:
% Plot for With Reflection
yl = [0,180];
f = figure('Position', [100, 100, 800, 600]);
ax = axes(f);
b2 = bar(ax, data_with_reflection', 'grouped');
for k = 1:length(b2)
b2(k).FaceColor = 'flat';
b2(k).CData = repmat([1,0,0], size(b2(k).CData, 1), 1);
end
grid on;
ylim(yl);
% Plot for No Reflection
ax = axes(f);
b1 = bar(ax, data_no_reflection', 'grouped');
for k = 1:length(b1)
b1(k).FaceColor = 'flat';
b1(k).CData = repmat(custom_colors(k, :), size(b1(k).CData, 1), 1);
end
xticks(1:length(QAM));
xticklabels(string(QAM));
title({'Coverage Area With Reflection', 'm-CAP and DCO-OFDM'});
legend(["m-CAP \Phi_{1/2}=10°", "m-CAP \Phi_{1/2}=30°", "m-CAP \Phi_{1/2}=45°", "m-CAP \Phi_{1/2}=60°", ...
"DCO-OFDM \Phi_{1/2}=10°", "DCO-OFDM \Phi_{1/2}=30°", "DCO-OFDM \Phi_{1/2}=45°", "DCO-OFDM \Phi_{1/2}=60°"], ...
'Location', 'Best');
set(ax,'color','none','box','off','GridLineStyle','none','XTickLabel',{},'YTickLabel',{},'XTick',[],'YTick',[]);
ylim(yl);
输出: