MATLAB draw Gantt chart

Line 1—11, the start time and end time of the machine work piece
16—19 lines, set the color as
many colors as you want: aa=unidrnd(255,20,3) Put the generated matrix into the color

%machine job time1 time2 
a=[0 0 0 5;
1 0 5 49;
2 0 49 145;
0 1 5 22;
1 1 49 135;
2 1 145 177;
0 2 22 94;
1 2 135 189;
2 2 189 225;
   ]; 
figure(1);clf; 
w=0.6;       %横条宽度 
set(gcf,'color','w'); 

color=[192,90,42;
    66,212,203;
    130,150,80;
   ];
%matlab中仅可以用字符表示8种颜色,超过8种就不可以了,
%现在用rgb数组可以表示任意多的颜色,注意这里生成的255,28行patch中除了255
% 生成颜色
% aa=unidrnd(255,20,3)

for ii=1:size(a,1) 
    x=a(ii,[3 3 4 4]); 
    y=a(ii,1)+[-w/2 w/2 w/2 -w/2]+0.9; 
    p=patch('xdata',x,'ydata',y,'facecolor',[color(a(ii,2)+1,1)/255,color(a(ii,2)+1,2)/255,color(a(ii,2)+1,3)/255],'edgecolor','k'); 
    text(a(ii,3)+1,a(ii,1)+0.9,num2str(a(ii,2)),'FontSize',13); 
end 

xlabel({'Makespan'},'FontSize',14,'FontWeight','bold');
ylabel({'Machine'},'FontSize',14,'FontWeight','bold');

axis([0 250 0 4]); 
% 设置横纵坐标大小
set(gca,'FontSize',12)
set(gca,'Box','on'); %右上边框
set(gca,'YTick',0:4); 
set(gca,'YTickLabel',{'';num2str((1:3)','M%d');''});  

Effect picture:
Insert picture description here

Guess you like

Origin blog.csdn.net/root_zhb/article/details/114155938