【问题标题】:How to display an array with textbox in a figure?如何在图中显示带有文本框的数组?
【发布时间】:2019-04-18 18:02:18
【问题描述】:

我正在尝试使用根据该位置的值而变化的彩色文本框在 MATLAB 中将数组显示为图形。

到目前为止,我已经尝试使用 MATLAB Edit Plot Tool 来绘制这样一个图形,然后生成代码来看看它可能是什么样子。这是我想出的:

figure1=figure

annotation(figure1,'textbox',...
    [0.232125037302298 0.774079320113315 0.034810205908684 0.0410764872521246],...
    'String','HIT',...
    'FitBoxToText','off',...
    'BackgroundColor',[0.470588235294118 0.670588235294118 0.188235294117647]);

annotation(figure1,'textbox',...
    [0.27658937630558 0.774079320113315 0.034810205908684 0.0410764872521246],...
    'String',{'STAY'},...
    'FitBoxToText','off',...
    'BackgroundColor',[1 0 0]);

这里的结果看起来不太好。我想要一些简洁而不难写的东西。在视觉上,我想要这样的东西:

【问题讨论】:

    标签: arrays matlab textbox figure


    【解决方案1】:

    我找到了使用pcolor function 的可能解决方案。

    警告:我只用 Octave 测试过

    如果您想创建一个 (m x n) 表格,根据您的图片,4 色,您必须:

    • 创建一个大小为(m+1 x n+1) 的数组,在integers' in the1:4` 范围内根据所需的顺序设置它们
    • 调用pcolor绘制表格
    • 调整figure的大小
    • 根据所需的颜色创建自己的colormap
    • 设置“颜色图”
    • 使用text function 添加所需的文本
    • 设置坐标区的tickticklabel

    编辑以回复评论

    您可以在下文中找到建议解决方案的可能实现方式。

    代码创建了两个figure:

    • 在第一个中将绘制输入矩阵的值
    • 在第二个中,用户定义的字符串

    “颜色-值”的关联是通过用户自定义的颜色图进行的。

    由于矩阵 x 有 4 个不同的可能值(已定义为 x=randi([1 4],n_row+1,n_col+1);),因此颜色图必须由 4 RGB 条目组成,如下所示。

    cm=[1 0.3 0.3   % RED
        0.3 0.3 1   % BLUE
        0   1   0   % GREEN
        1   1   1]; % WHITE
    

    如果你想改变关联,你只需要改变颜色图的行顺序。

    代码中的cmets应该把上面的步骤搞清楚了。

    代码更新

    % Define a rnadom data set
    n_row=24;
    n_col=10;
    x=randi([1 4],n_row+1,n_col+1);
    
    for fig_idx=1:2
       % Open two FIGURE
       % In the first one wil be ploted the values of the input matrix
       % In the second one the user defined strings
       figure('position',[ 1057    210    606    686])
       % Plot the matrix
       s=pcolor(x);
       set(s,'edgecolor','w','linewidth',3)
       % Define the colormap
    
       %cm=[1 1 1
       %    0 1 0
       %    0.3 0.3 1
       %    1 0.3 0.3];
    
    
       cm=[1 0.3 0.3   % RED
           0.3 0.3 1   % BLUE
           0   1   0   % GREEN
           1   1   1]; % WHITE
    
       % Set the colormap
       colormap(cm);
       % Write the text according to the color
       [r,c]=find(x(1:end-1,1:end-1) == 1);
       for i=1:length(r)
          if(fig_idx == 1)
             ht=text(c(i)+.1,r(i)+.5,num2str(x(r(i),c(i))));
          else
             ht=text(c(i)+.1,r(i)+.5,'SUR');
          end
          set(ht,'fontweight','bold','fontsize',10);
       end
       % Write the text according to the color
       [r,c]=find(x(1:end-1,1:end-1) == 2);
       for i=1:length(r)
          if(fig_idx == 1)
             ht=text(c(i)+.1,r(i)+.5,num2str(x(r(i),c(i))));
          else
             ht=text(c(i)+.1,r(i)+.5,'DBL');
          end
          set(ht,'fontweight','bold','fontsize',10);
       end
       % Write the text according to the color
       [r,c]=find(x(1:end-1,1:end-1) == 3);
       for i=1:length(r)
          if(fig_idx == 1)
             ht=text(c(i)+.1,r(i)+.5,num2str(x(r(i),c(i))));
          else
             ht=text(c(i)+.1,r(i)+.5,'HIT');
          end
          set(ht,'fontweight','bold','fontsize',10);
       end
       % Write the text according to the color
       [r,c]=find(x(1:end-1,1:end-1) == 4);
       for i=1:length(r)
          if(fig_idx == 1)
             ht=text(c(i)+.1,r(i)+.5,num2str(x(r(i),c(i))));
          else
             ht=text(c(i)+.1,r(i)+.5,'STK');
          end
          set(ht,'fontweight','bold','fontsize',10);
       end
       % Create and set the X labels
       xt=.5:10.5;
       xtl={' ';'2';'3';'4';'5';'6';'7';'8';'9';'10';'A'};
       set(gca,'xtick',xt);
       set(gca,'xticklabel',xtl,'xaxislocation','top','fontweight','bold');
       % Create and set the X labels
       yt=.5:24.5;
       ytl={' ';'Soft20';'Soft19';'Soft18';'Soft17';'Soft16';'Soft15';'Soft14';'Soft13'; ...
            '20';'19';'18';'17';'16';'15';'14';'13';'12';'11';'10';'9';'8';'7';'6';'5'};
       set(gca,'ytick',yt);
       set(gca,'yticklabel',ytl,'fontweight','bold');
       title('Dealer''s Card')
    end
    

    输入矩阵中的值的表格

    带有用户定义字符串的表格

    【讨论】:

    • 谢谢,这很有帮助。但是,这里框中的颜色与数组 x 中的特定值无关。在 pcolor 的文档中,它说“每个面的颜色取决于其四个周围顶点之一的颜色。在四个顶点中,x-y 网格中最先出现的那个决定了面的颜色。”例如,我如何使用它来确保 1 = 红色、2= 蓝色、3= 绿色和 4= 白色?
    • 关联color - value - string 是通过colormap 执行的,特别是按照RGB 行的顺序。我更新了代码:现在它创建了两个图:在第一个中您可以直接找到关联color- value,第二个是关联color - string。您可以通过简单地更改颜色图中的行顺序来更改关联color - value - string
    • 您好,我根据您的回答启发了我的解决方案,所以我想我会过来​​给您竖起大拇指!
    • 好的,非常感谢。其实我只是看一下y轴就明白了一点:图中左上角的值与矩阵左下角的值相关。这就是为什么我认为有一个混淆。非常感谢这两个答案都符合我的期望!
    【解决方案2】:

    这是一个受 il_raffa 的 answer 启发的答案,但也有不少不同之处。没有好坏之分,只是喜好问题。

    主要区别是:

    • 它使用imagesc 而不是pcolor
    • 它使用第二个覆盖 axes 来精细控制网格 color/thickness/transparency 等...
    • value - label - color 之间的关联在一个表的开头设置。然后所有代码都会尊重这一点 表。

    是这样的:

    %% Random data
    n_row = 24;
    n_col = 10;
    vals = randi([1 4], n_row, n_col);
    
    %% Define labels and associated colors
    % this is your different labels and the color associated. There will be
    % associated to the values 1,2,3, etc ... in the order they appear in this
    % table:
    Categories = {
        'SUR' , [1 0 0] % red       <= Label and color associated to value 1
        'DBL' , [0 0 1] % blue      <= Label and color associated to value 2
        'HIT' , [0 1 0] % green     <= Label and color associated to value 3
        'STK' , [1 1 1] % white     <= you know what this is by now ;-)
        } ;
    
    % a few more settings
    BgColor  = 'w' ; % Background color for various elements
    strTitle = 'Dealer''s Card' ;
    
    %% Parse settings
    % get labels according to the "Categories" defined above
    labels = Categories(:,1) ;
    % build the colormap according to the "Categories" defined above
    cmap = cell2mat( Categories(:,2) ) ;
    
    %% Display
    hfig = figure('Name',strTitle,'Color',BgColor,...
                  'Toolbar','none','Menubar','none','NumberTitle','off') ;
    ax1 = axes ;
    
    imagesc(vals)     % Display each cell with an associated color
    colormap(cmap);   % Set the colormap
    grid(ax1,'off')   % Make sure there is no grid 
    
    % Build and place the texts objects
    textStrings = labels(vals) ;
    [xl,yl]     = meshgrid(1:n_col,1:n_row);
    hStrings    = text( xl(:), yl(:), textStrings(:), 'HorizontalAlignment','center');
    
    %% Modify text color if needed
    % (White text for the darker box colors)
    textColors = repmat(vals(:) <= 2 ,1,3);
    set(hStrings,{'Color'},num2cell(textColors,2));
    
    %% Set the axis labels
    xlabels = [ cellstr(num2str((2:10).')) ; {'A'} ] ;
    ylabels = [ cellstr(num2str((5:20).')) ; cellstr(reshape(sprintf('soft %2d',[13:20]),7,[]).') ] ;
    
    set(ax1,'XTick',        1:numel(xlabels), ...
            'XTickLabel',   xlabels, ...
            'YTick',        1:numel(ylabels), ...
            'YTickLabel',   ylabels, ... 
            'TickLength',   [0 0], ...
            'fontweight',   'bold' ,...
            'xaxislocation','top') ;
    
    title(strTitle)
    
    %% Prettify
    ax2 = axes ; % create new axe and retrieve handle
    % superpose the new axe on top, at the same position
    set(ax2,'Position', get(ax1,'Position') );
    % make it transparent (no color)
    set(ax2,'Color','none')
    % set the X and Y grid ticks and properties
    set(ax2,'XLim',ax1.XLim , 'XTick',[0 ax1.XTick+0.5],'XTickLabel','' ,... 
            'YLim',ax1.YLim , 'YTick',[0 ax1.YTick+0.5],'YTickLabel','' ,...
            'GridColor',BgColor,'GridAlpha',1,'Linewidth',2,...
            'XColor',BgColor,'YColor',BgColor) ;
    % Make sure the overlaid axes follow the underlying one
    resizeAxe2 = @(s,e) set(ax2,'Position', get(ax1,'Position') );
    hfig.SizeChangedFcn = resizeAxe2 ;
    

    它产生下图:

    当然,您可以将颜色替换为您喜欢的颜色。 我鼓励您使用ax2 的网格设置来获得不同的效果,您还可以使用text 对象的属性(使它们变为粗体、其他颜色等...)。玩得开心!

    【讨论】:

    • 伟大而优雅的答案! +1。它显示了您的专业人士和我的业余爱好者的反应之间的差异:-)。
    猜你喜欢
    • 2020-02-11
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-19
    • 2021-07-21
    • 1970-01-01
    相关资源
    最近更新 更多