首页 > 解决方案 > 如何在图中显示带有文本框的数组?

问题描述

我正在尝试使用根据该位置的值而变化的彩色文本框在 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]);

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

在此处输入图像描述

标签: arraysmatlabtextboxfigure

解决方案


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

警告:我只用 Octave 测试过

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

  • (m+1 x n+1)创建一个大小为1:4` 范围的数组,integers' in the根据所需的顺序设置它们
  • 调用pcolor绘制表格
  • 调整大小figure
  • colormap根据所需的颜色创建自己的
  • 设置“颜色图”
  • 使用text 函数添加所需的文本
  • 设置轴的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

如果您想更改关联,只需更改颜色图行的顺序。

代码中的注释应阐明上述步骤。

代码更新

% 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

包含输入矩阵中的值的表

在此处输入图像描述

包含用户定义字符串的表

在此处输入图像描述


推荐阅读