Matlab legend 出错

img


画图中进行程序运行时提示转义字符无效,更新legend时出错

  • 你看下这篇博客吧, 应该有用👉 :matlab legend换行 多个legend 阵列排布
  • 除此之外, 这篇博客: 【Matlab】根据excel画折线图和柱状图中的 添加图例(legend) 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • legendlegend('legend1', 'legend2', 'legend3')

    %% prepare data in excel
    data_file = 'data.xlsx'
    
    %% read data from a certain sheet
    data1 = xlsread(data_file, 1); % first sheet
    data2 = xlsread(data_file, 2); % second sheet
    
    %% plot
    % plot1 = plot(data1(1:end,1:end),'DisplayName','data(1:end,1:end)');
    
    %% bar
    bar2 = bar(data2(1:end,1:end),'DisplayName','data(1:end,1:end)');
    
    %% x label
    xlabel('Event log');
    % xlim([2.5 5.5])
    % set(gca,'XTick',[2.5:0.5:5.5])
    set(gca,'Xticklabel',{'\it{uci\_labour}','\it{uci\_weekends}','\it{102\_labour}','\it{102\_weekends}','\it{104\_labour}','\it{104\_weekends}','\it{110\_labour}','\it{110\_weekends}'}); %设置横坐标
    set(gca,'XTickLabelRotation',45); 
    
    
    %% y label
    ylabel('Percentage');
    ylim([0 1.0]);
    set(gca,'YTick',[0:0.5:1.0]);
    set(gca,'Yticklabel',{'Percentage(0%)','Percentage(50%)','Percentage(100%)'});
    set(gca,'YTickLabelRotation',45); 
    
    %% plot with markers
    % set(plot1(1),'marker','+');
    % set(plot1(2),'marker','*');
    % set(plot1(3),'marker','o');
    
    %% plot with different specific colors
    % set(plot1(1),'color',[255/255 247/255 235/255])
    % set(plot1(2),'color',[237/255 177/255 32/255])
    % set(plot1(3),'color',[153/255 51/255 0/255])
    
    %% bar with different colors
    set(bar2(1),'facecolor',[0.87 0.92 0.98])
    set(bar2(2),'facecolor',[0.73 0.83 0.96])
    set(bar2(3),'facecolor',[0.39 0.47 0.64])
    
    %% legend
    lege = legend('Second', 'Minute', 'Hour');
    

    在这里插入图片描述