Matlab 曲線繪制之線型和顏色 示例


估計很多人會碰到,當繪制的曲線特別多的時候,需要用不同的顏色和線型區分開這些曲線。根據STC論文,自己整理了一個顏色和線型的例子,供大家直接使用,直接引用PlotStyle這個數據結構就可以了。

示例1

 1 PlotStyle={   
 2     struct('Color',[1,0,0],'LineStyle','-'),...
 3     struct('Color',[0,1,0],'LineStyle','-'),...
 4     struct('Color',[0,0,1],'LineStyle','-'),...
 5     struct('Color',[0,0,0],'LineStyle','-'),...%    struct('Color',[1,1,0],'LineStyle','-'),...%yellow
 6     struct('Color',[1,0,1],'LineStyle','-'),...%pink
 7     struct('Color',[0,1,1],'LineStyle','-'),...
 8     struct('Color',[0.5,0.5,0.5],'LineStyle','-'),...%gray-25%
 9     struct('Color',[136,0,21]/255,'LineStyle','-'),...%dark red
10     struct('Color',[255,127,39]/255,'LineStyle','-'),...%orange
11     struct('Color',[0,162,232]/255,'LineStyle','-'),...%Turquoise
12     struct('Color',[163,73,164]/255,'LineStyle','-'),...%purple    %%%%%%%%%%%%%%%%%%%%
13     struct('Color',[1,0,0],'LineStyle','--'),...
14     struct('Color',[0,1,0],'LineStyle','--'),...
15     struct('Color',[0,0,1],'LineStyle','--'),...
16     struct('Color',[0,0,0],'LineStyle','--'),...%    struct('Color',[1,1,0],'LineStyle','--'),...%yellow
17     struct('Color',[1,0,1],'LineStyle','--'),...%pink
18     struct('Color',[0,1,1],'LineStyle','--'),...
19     struct('Color',[0.5,0.5,0.5],'LineStyle','--'),...%gray-25%
20     struct('Color',[136,0,21]/255,'LineStyle','--'),...%dark red
21     struct('Color',[255,127,39]/255,'LineStyle','--'),...%orange
22     struct('Color',[0,162,232]/255,'LineStyle','--'),...%Turquoise
23     struct('Color',[163,73,164]/255,'LineStyle','--'),...%purple    %%%%%%%%%%%%%%%%%%%
24     struct('Color',[1,0,0],'LineStyle','-.'),...
25     struct('Color',[0,1,0],'LineStyle','-.'),...
26     struct('Color',[0,0,1],'LineStyle','-.'),...
27     struct('Color',[0,0,0],'LineStyle','-.'),...%    struct('Color',[1,1,0],'LineStyle',':'),...%yellow
28     struct('Color',[1,0,1],'LineStyle','-.'),...%pink
29     struct('Color',[0,1,1],'LineStyle','-.'),...
30     struct('Color',[0.5,0.5,0.5],'LineStyle','-.'),...%gray-25%
31     struct('Color',[136,0,21]/255,'LineStyle','-.'),...%dark red
32     struct('Color',[255,127,39]/255,'LineStyle','-.'),...%orange
33     struct('Color',[0,162,232]/255,'LineStyle','-.'),...%Turquoise
34     struct('Color',[163,73,164]/255,'LineStyle','-.'),...%purple
35   };
36 
37 x = 0.1:.1:10;
38 len = length(PlotStyle)
39 for i = 1:len
40    y = x.*log(x)+i*2;
41    hold on;
42    plot(x,y,'Color',PlotStyle{1,i}.Color,'LineStyle',PlotStyle{1,i}.LineStyle,'lineWidth', 2);%call method
43 end
44 title('多種顏色和線性的示例','FontSize',20,'FontWeight','bold','Color','k');
45 xlabel('x軸','FontSize',15,'FontWeight','bold','Color','k');
46 ylabel('y軸','FontSize',15,'FontWeight','bold','Color','k');
47 saveas(1, 'result', 'jpg');

效果如下圖所示:

 

示例2

PlotStyle={   
    struct('Color',[123,0,150]/255,'LineStyle','-'),...
    struct('Color',[1,0,160]/255,'LineStyle','--'),...
    struct('Color',[0,137,4]/255,'LineStyle','-.'),...
    struct('Color',[160,101,5]/255,'LineStyle',':'),...%    struct('Color',[1,1,0],'LineStyle','-'),...%yellow
    struct('Color',[150,0,5]/255,'LineStyle','-')
  };

x = 0:.1:10;
len = length(PlotStyle)
for i = 1:len
%    y = x.*log(x)+i*2;
   y = x.*sin(x)+i*2;
   hold on;
   plot(x,y,'Color',PlotStyle{1,i}.Color,'LineStyle',PlotStyle{1,i}.LineStyle,'lineWidth', 2);
end
title('多種顏色和線性的示例','FontSize',20,'FontWeight','bold','Color','k');
xlabel('x軸','FontSize',15,'FontWeight','bold','Color','k');
ylabel('y軸','FontSize',15,'FontWeight','bold','Color','k');
saveas(1, 'result2', 'jpg');

 效果如下圖所示:

  

示例3

clc;
close all;
%%數據生成
ya = [];
yb = [];
yc = [];
xa = [];
xb = [];
xc = [];
for i = 0:10
    value0 = i*2+rand();
    valuea = i*2+rand();
    xa = [xa value0];
    ya = [ya 2*valuea.^(0.6)];
    valueb = i*2+rand();
    xb = [xb valueb];
    yb = [yb 2*valuea.^(0.7)];
    valuec = i*2+rand();
    xc = [xc valuec];
    yc = [yc 2*valuea.^(0.5)];
end
xa = xa - min(xa);
ya = ya - min(ya);
xb = xb - min(xb);
yb = yb - min(yb);
xc = xc - min(xc);
yc = yc - min(yc);
figure(1);
hold on;
axis equal  %強制坐標軸尺度相同,這個放在前面或者后面效果會不一樣
xlim([0 22]);%x軸范圍
ylim([0 14]);%y軸范圍

%%繪制曲線
plot(xa,ya,'ms:','LineWidth',4,'MarkerSize',15,'Color', [1 0 0]);
x = xa(end);y = ya(end);
text(x-0.5,y+0.5,['(x_n,y_n)'],...
    'FontSize',20, 'Color',[0.0 0.0 0.0], 'FontWeight','bold');
plot(xb(1:end-3),yb(1:end-3),'ro:','LineWidth',4,'MarkerSize',15,'Color', [0 0 1]);
x = xb(end-3);y = yb(end-3);
text(x-0.5,y+0.5,['(x_m,y_m)'],...
    'FontSize',20, 'Color',[0.0 0.0 0.0], 'FontWeight','bold');
plot(xc(1:end-2),yc(1:end-2),'r<:','LineWidth',4,'MarkerSize',15,'Color', [0 1 0]);
x = xc(end-2);y = yc(end-2);
text(x-0.5,y+0.5,['(x_k,y_k)'],...
    'FontSize',20, 'Color',[0.0 0.0 0.0], 'FontWeight','bold');

%坐標原點
x = xc(1);y = yc(1);
text(x+0.5,y+0.5,['(x_0,y_0)'],...
    'FontSize',20, 'Color',[0.0 0.0 0.0], 'FontWeight','bold');
title('轉歸過程','FontSize',25,'FontWeight','bold','FontName','微軟雅黑');

%%坐標軸標題設置
xlabel('體質辨識x','Color','k','FontSize',20,'FontWeight','bold','FontName','微軟雅黑');%橫軸標題
ylabel('疾病預報y','Color','k','FontSize',20,'FontWeight','bold','FontName','微軟雅黑');%縱軸標題

%設置legend
hleg = legend('體檢者1','體檢者2','體檢者3','Location','NorthWest');%本身不能設置字體的大小,需要通過set進行設置
set(hleg,'FontSize', 20, 'FontAngle','italic','FontWeight','bold',...
    'Color',[1,1,1]);%Color為設置坐標的背景顏色

%%坐標軸刻度設置
set(gca, 'ytick',ya,'yticklabel',{' ',' ',' ',' ',' ',' ','','','',''},'xtick',x,...
    'xticklabel',{'','','','','','','','','',''}, 'color',[1 1 1],...
    'FontSize',12,'FontName','微軟雅黑'); %xtick就是你要在哪些地方顯示刻度,xticklabel就是顯示什么內容

scrsz = get(0,'ScreenSize');
set(gcf,'Position',scrsz);

%%保存圖像
set(1, 'InvertHardCopy', 'off');%設置的背景色有效,如果為on則圖形不保存背景色,maltab 默認為 on
filename = 'test';
saveas(1,filename,'jpeg');

效果如下圖所示:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM