Matlab中的高级绘图(图例操作) [英] advanced plotting (legend manipulation) in Matlab

查看:125
本文介绍了Matlab中的高级绘图(图例操作)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在图上添加图例,以使其与图片中的图样相同(减少线的长度并减少不必要的空格)

I want to add a legend to my plot so that it look as in the picture (reduce the length of the line and suppress unnecessary spaces)

x = randn(6,20);

figure(2)
hax = gca;

plot(x(1,:),'--k','linewidth',1.5);
hold on;
plot(x(2,:),'b','linewidth',1.5);
hold on;
plot(x(3,:),'g','linewidth',1.5);
hold on;
plot(x(4,:),'r','linewidth',1.5);
hold on;
plot(x(5,:),'c','linewidth',1.5);
hold on;
plot(x(6,:),':r','linewidth',1.5);


ylabel('states','fontsize',14); xlabel('time(s)','fontsize',14);
legend('True','SCKS(h1)','SCKS(h2)','SCKS(h3)','SCKS(h4)','DEM',14);
legendshrink(0.8,[]);
%Fig_legend = legend('Taylor','Euler','LLscheme','LLscheme1');
%set(Fig_legend,'FontSize',7)

grid(hax,'on')
axis(hax,'tight')
set(hax,'box','on','Layer','top');
set(hax,'tickdir','out')

有人可以帮助我吗?

推荐答案

如MatLab中所述

As reported in the MatLab online documentation Starting in R2014b, colorbars and legends are no longer axes objects.

这限制了为实现目标而在legend上工作的可能性.

This limits the possibilites to work on the legend in order to achieve your target.

可能的解决方案是按照以下步骤创建自己的基于轴的图例:

A possible solution could be to create a your own axes based legend following these steps:

  • 使用以下语法[lgd,icons,plots,txt] = legend(___)创建调用legend函数的图例(注意,尽管不推荐使用此语法,但是我们将在下一步中删除图例,因此不会有问题)
  • 获取图例的位置
  • 标识图例中的项数

  • create the legend calling the legend function with the following syntax [lgd,icons,plots,txt] = legend(___) (notice, this syntax is not recommended nevertheless, we are going to delete the legend in the next steps, so it will not be a problem)
  • get the position of the legend
  • identify the number of items in the legend

  • 图中的每一行图例中都有三项(linemarkertext)
  • 在图形中有六行的情况下,图例手柄将按以下顺序包含18对象:

  • for each line in the graph there are three items in the legend (the line, the marker and the text)
  • in the case of six line in a graph the legend handle will contains 18 objects in the following order:

  • 6个处理text
  • 的句柄
  • 对于6行中的每行
    • 该行的1个句柄
    • 标记的1个句柄
    • 6 x handles to text
    • for each of the 6 lines
      • 1 handle to the line
      • 1 handle to the marker
      • 行数据:XDataYDataColor,"LineStyle"
      • 文本数据:stringposition
      • line data: XData, YData, Color, 'LineStyle`
      • text data: string, position
      • 您可以使用text项的Extent属性来标识较长的项的长度
      • 然后在其中添加相应行的长度
      • you can use the Extent property of the text items to identify the length of the longer one
      • then add to it the length of the corresponding line

      这是一个可能的实现(基于您的代码):

      This is a possible implementation (based on you code):

      x = randn(6,20);
      
      figure(2)
      hax = gca;
      
      plot(x(1,:),'--k','linewidth',1.5);
      hold on;
      plot(x(2,:),'b','linewidth',1.5);
      % hold on;
      plot(x(3,:),'g','linewidth',1.5);
      % hold on;
      plot(x(4,:),'r','linewidth',1.5);
      % hold on;
      plot(x(5,:),'c','linewidth',1.5);
      % hold on;
      plot(x(6,:),':r','linewidth',1.5);
      
      
      ylabel('states','fontsize',14); xlabel('time(s)','fontsize',10);
      %legend('True','SCKS(h1)','SCKS(h2)','SCKS(h3)','SCKS(h4)','DEM',14);
      %
      % New call "legend"
      %
      [leg_h,leg_item_h,~,~]=legend('True','SCKS(h1)','SCKS(h2)','SCKS(h3)','SCKS(h4)','DEM',14);
      %
      % legendshrink(0.8,[]);
      %Fig_legend = legend('Taylor','Euler','LLscheme','LLscheme1');
      %set(Fig_legend,'FontSize',7)
      
      grid(hax,'on')
      % axis(hax,'tight')
      set(hax,'box','on','Layer','top');
      set(hax,'tickdir','out')
      
      %%%%%%%%%%%%%%%%%%%%%%%%%%%%
      % GENERATION OF THE LEGEND %
      %%%%%%%%%%%%%%%%%%%%%%%%%%%%
      % Define a scale factor fot the lines
      line_scale_factor=1.4;
      % Define a scale factor fot the lines
      text_scale_factor=1.35;
      % Get the "Position" of the legend
      orig_leg_pos=get(leg_h,'position')
      % Get the number on objects in the legend
      n_obj=length(leg_item_h);
      % Extract the "Line" object
      line_obj=leg_item_h(n_obj/3+1:2:n_obj);
      % Get the "LineStyle" of each "Line" in the legend
      l_style=get(line_obj,'LineStyle')
      % Get the "Color" of each "Line" in the legend
      l_col=cell2mat(get(line_obj,'color'))
      % Get the "XData" and "YData" of the "Lines" in the legend
      leg_x_data=cell2mat(get(line_obj,'xdata'))
      leg_y_data=cell2mat(get(line_obj,'ydata'))
      % Get the handle of the "Text" of the items in the legend
      leg_t=leg_item_h(1:n_obj/3)
      % Get the "Text" of the items in the legend
      str=get(leg_t,'string')
      % Get the "Position" of each "Text" item in the legend
      tx=cell2mat(get(leg_t,'position'))
      % Delete the original legend
      delete(leg_h)
      % Create an axes with the same position and size of the original legend
      ax=axes('position',orig_leg_pos,'xlim',[0 1],'ylim',[0 1], ...
         'xtick',[],'ytick',[],'box','on')
      hold on
      % Add the legend items to the axes
      for i=1:n_obj/3
         % Add the lines with the original settings (color, style, ...)
         plot([leg_x_data(i,1) leg_x_data(i,2)/line_scale_factor],leg_y_data(i,:),'color',l_col(i,:), ...
                                'linestyle',l_style{i}, ...
                                'linewidth',1.4)
         % Add the text
         th(i,:)=text(leg_x_data(i,2)/text_scale_factor,tx(i,2),0,str{i},'fontsize',9, ...
                 'unit','normalized')
      end
      % Get the maximun extent of the lagend "Text"
      tx_max_ext=max(reshape([th(:).Extent],4,6)');
      % Evaluate the axis scaling factor
      tx_r_1=tx_max_ext(3)+leg_x_data(i,2)/line_scale_factor
      % Get the axes position
      axp=ax.Position
      % Resize the axes width
      ax.Position=[axp(1) axp(2) axp(3)*tx_r_1 axp(4)]
      

      这篇关于Matlab中的高级绘图(图例操作)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆