You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Matlab各子图设置对应θ_(1/2)值的自定义图例?

给Matlab子图添加对应θ_(1/2)值的图例

问题描述

我希望为每个子图添加对应图例,图例内容为θ_(1/2)=5、θ_(1/2)=10、θ_(1/2)=15……θ_(1/2)=60,请问如何修改每个子图图例中的数值?以下是我的Matlab代码:

Q = [16,32,64,128,256,512,1024];

VEC_Beam_5 = [113.2659   69.7204   51.8739   34.5033   21.6538   10.9459    5.9488];   % Coverage area of Theta = 5
VEC_Beam_10 = [117.5491   76.1452   53.3016   38.3105   23.0815   13.5634    6.9007];  % Coverage area of Theta = 10
VEC_Beam_15 = [117.5491   76.1452   53.3016   38.3105   23.0815   13.5634    6.9007];  % Coverage area of Theta = 15
VEC_Beam_20 = [117.7870   76.1452   53.3016   38.5485   23.0815   13.5634    6.9007];  % Coverage area of Theta = 20
VEC_Beam_25 = [117.7870   76.6211   54.2534   38.5485   23.0815   13.5634    6.9007];  % Coverage area of Theta = 25
VEC_Beam_30 = [117.7870   76.6211   54.2534   38.5485   23.0815   13.5634    6.9007];  % Coverage area of Theta = 30
VEC_Beam_35 = [117.7870   76.6211   54.2534   38.5485   23.0815   13.5634    6.9007];  % Coverage area of Theta = 35
VEC_Beam_40 = [117.7870   76.6211   54.2534   38.5485   23.0815   13.5634    6.9007];  % Coverage area of Theta = 40
VEC_Beam_45 = [117.7870   76.6211   54.2534   38.7864   23.0815   13.5634    6.9007];  % Coverage area of Theta = 45
VEC_Beam_50 = [117.7870   76.6211   54.2534   38.7864   23.0815   13.5634    6.9007];  % Coverage area of Theta = 50
VEC_Beam_55 = [117.7870   76.6211   54.2534   38.7864   23.0815   13.5634    6.9007];  % Coverage area of Theta = 55
VEC_Beam_60 = [117.7870   76.6211   54.2534   38.7864   23.0815   13.5634    6.9007];  % Coverage area of Theta = 60

VEC_Beam = [VEC_Beam_5;VEC_Beam_10;VEC_Beam_15;VEC_Beam_20;VEC_Beam_25;VEC_Beam_30;VEC_Beam_35;VEC_Beam_40;VEC_Beam_45;VEC_Beam_50;VEC_Beam_55;VEC_Beam_60];
figure

cmap = jet(12);

for k1 = 1:12
    subplot(2,6,k1);
    plot(log2(Q),VEC_Beam(k1,:), 'Color', cmap(k1, :),'LineWidth',2, 'Marker','O');
    xticks(log2(Q))
    xticklabels(string(Q))
    xlabel('Q');
    ylabel('Coverage area');
    grid on;
end

sgtitle('QAM 16');

解决方案

通过两步即可实现需求:

  1. 生成包含所有θ值的数组,统一管理数值;
  2. 在子图循环中动态生成图例文本,调用legend()函数添加。

修改后的完整代码如下:

Q = [16,32,64,128,256,512,1024];

VEC_Beam_5 = [113.2659   69.7204   51.8739   34.5033   21.6538   10.9459    5.9488];   % Coverage area of Theta = 5
VEC_Beam_10 = [117.5491   76.1452   53.3016   38.3105   23.0815   13.5634    6.9007];  % Coverage area of Theta = 10
VEC_Beam_15 = [117.5491   76.1452   53.3016   38.3105   23.0815   13.5634    6.9007];  % Coverage area of Theta = 15
VEC_Beam_20 = [117.7870   76.1452   53.3016   38.5485   23.0815   13.5634    6.9007];  % Coverage area of Theta = 20
VEC_Beam_25 = [117.7870   76.6211   54.2534   38.5485   23.0815   13.5634    6.9007];  % Coverage area of Theta = 25
VEC_Beam_30 = [117.7870   76.6211   54.2534   38.5485   23.0815   13.5634    6.9007];  % Coverage area of Theta = 30
VEC_Beam_35 = [117.7870   76.6211   54.2534   38.5485   23.0815   13.5634    6.9007];  % Coverage area of Theta = 35
VEC_Beam_40 = [117.7870   76.6211   54.2534   38.5485   23.0815   13.5634    6.9007];  % Coverage area of Theta = 40
VEC_Beam_45 = [117.7870   76.6211   54.2534   38.7864   23.0815   13.5634    6.9007];  % Coverage area of Theta = 45
VEC_Beam_50 = [117.7870   76.6211   54.2534   38.7864   23.0815   13.5634    6.9007];  % Coverage area of Theta = 50
VEC_Beam_55 = [117.7870   76.6211   54.2534   38.7864   23.0815   13.5634    6.9007];  % Coverage area of Theta = 55
VEC_Beam_60 = [117.7870   76.6211   54.2534   38.7864   23.0815   13.5634    6.9007];  % Coverage area of Theta = 60

VEC_Beam = [VEC_Beam_5;VEC_Beam_10;VEC_Beam_15;VEC_Beam_20;VEC_Beam_25;VEC_Beam_30;VEC_Beam_35;VEC_Beam_40;VEC_Beam_45;VEC_Beam_50;VEC_Beam_55;VEC_Beam_60];
figure

cmap = jet(12);
% 生成所有θ值的数组,步长5,从5到60
theta_vals = 5:5:60;

for k1 = 1:12
    subplot(2,6,k1);
    plot(log2(Q),VEC_Beam(k1,:), 'Color', cmap(k1, :),'LineWidth',2, 'Marker','O');
    xticks(log2(Q))
    xticklabels(string(Q))
    xlabel('Q');
    ylabel('Coverage area');
    grid on;
    % 动态生成带LaTeX格式的图例文本
    legend_text = ['$\theta_{1/2} = ', num2str(theta_vals(k1)), '$'];
    legend(legend_text, 'Interpreter', 'latex');
    % 可选:设置图例自动选择最优位置,避免遮挡曲线
    % legend(legend_text, 'Interpreter', 'latex', 'Location', 'best');
end

sgtitle('QAM 16');

关键说明

  • theta_vals = 5:5:60:一次性生成所有需要的θ值,避免手动逐个输入,减少出错概率;
  • legend_text = ['$\theta_{1/2} = ', num2str(theta_vals(k1)), '$']:通过字符串拼接生成带LaTeX格式的文本,其中$\theta_{1/2}$会渲染成下标形式的θ符号;
  • legend(legend_text, 'Interpreter', 'latex'):指定使用LaTeX解释器,确保下标和符号正确显示。

内容的提问来源于stack exchange,提问作者SH_IQ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 17:10:29