MATLAB |
您所在的位置:网站首页 › 饼状图绘制 › MATLAB |
居然又攒出了一期特殊绘图小合集文章,本来以为要攒很久的,本系列主要拿出来一些简单的,单独拿来写篇幅会很短的绘图小技巧做个合集,本期代码为了绘制好看大部分情况使用了以下这段简单小代码进行修饰: function defualtAxes ax=gca;hold on;box on ax.XGrid='on'; ax.YGrid='on'; ax.XMinorTick='on'; ax.YMinorTick='on'; ax.LineWidth=.8; ax.GridLineStyle='-.'; ax.FontName='Cambria'; ax.FontSize=12; end 1 连贯填充堆叠柱状图比如写了如下代码绘制了堆叠柱状图: X=randi([0,5],[6,4]); barHdl=bar(X,'stacked','BarWidth',.4);把代码改成这样就能绘制带连贯填充的柱状图: X=randi([0,5],[6,4]); % X=randi([-2,5],[6,4]); barHdl=bar(X,'stacked','BarWidth',.4); % 以下是生成连接的部分 hold on;axis tight yEndPoints=reshape([barHdl.YEndPoints]',length(barHdl(1).YData),[])'; zeros(1,length(barHdl(1).YData)); yEndPoints=[zeros(1,length(barHdl(1).YData));yEndPoints]; barWidth=barHdl(1).BarWidth; for i=1:length(barHdl) for j=1:length(barHdl(1).YData)-1 y1=min(yEndPoints(i,j),yEndPoints(i+1,j)); y2=max(yEndPoints(i,j),yEndPoints(i+1,j)); if y1*y2=0,1,'last'),j); y1=min(ty,yEndPoints(i+1,j)); y2=max(ty,yEndPoints(i+1,j)); end y3=min(yEndPoints(i,j+1),yEndPoints(i+1,j+1)); y4=max(yEndPoints(i,j+1),yEndPoints(i+1,j+1)); if y3*y4=0,1,'last'),j+1); y3=min(ty,yEndPoints(i+1,j+1)); y4=max(ty,yEndPoints(i+1,j+1)); end fill([j+.5.*barWidth,j+1-.5.*barWidth,j+1-.5.*barWidth,j+.5.*barWidth],... [y1,y3,y4,y2],barHdl(i).FaceColor,'FaceAlpha',.4,'EdgeColor','none'); end end本来不需要写这么长代码,但为了应对有负数的情况才写了这么长,比如数据有负数会是这样: X=randi([-2,5],[6,4]); barHdl=bar(X,'stacked','BarWidth',.4); 2 y轴分段填充折线图假设画了这样的图: X=0:.01:pi*5; Y=sin(X)+X./15; hold on plot(X,Y,'LineWidth',2) Uy=1.2;Ly=0;% Uy:上面的辅助线y值,Ly:下面的辅助线y值 yline(Uy,'LineWidth',1,'LineStyle','--') yline(Ly,'LineWidth',1,'LineStyle','--')填充颜色: fillColor=[114,146,184]./255;% 填充颜色 UY=Y;UY(UYLy)=Ly; fill(X,LY,fillColor,'EdgeColor','none','FaceAlpha',.9) 3 不同colormap同一坐标区域不同colormap,首先假如化了俩曲面: [X,Y,Z]=peaks(40); hold on;axis tight;grid on;view(3) surfHdl1=surf(X,Y,Z); surfHdl2=surf(X,Y,Z+10);设置配色: CM1=winter; CM2=pink; ZList1=linspace(min(min(surfHdl1.ZData)),max(max(surfHdl1.ZData)),size(CM1,1)); ZList2=linspace(min(min(surfHdl2.ZData)),max(max(surfHdl2.ZData)),size(CM2,1)); CMap1=cat(3,interp1(ZList1,CM1(:,1),surfHdl1.ZData),interp1(ZList1,CM1(:,2),surfHdl1.ZData),interp1(ZList1,CM1(:,3),surfHdl1.ZData)); CMap2=cat(3,interp1(ZList2,CM2(:,1),surfHdl2.ZData),interp1(ZList2,CM2(:,2),surfHdl2.ZData),interp1(ZList2,CM2(:,3),surfHdl2.ZData)); surfHdl1.CData=CMap1; surfHdl2.CData=CMap2; 4 柱状图绘制误差棒柱状图绘制误差棒很简单: Data=randi([20,35],[5,1]); err=rand([5,1]).*5; hold on barHdl=bar(Data,'BarWidth',.4); % 绘制误差棒 errorbar(barHdl.XEndPoints,barHdl.YEndPoints,err,... 'LineStyle','none','Color','k','LineWidth',.8)多组的就是加个循环: Data=randi([20,35],[5,2]); err=rand([5,2]).*5; hold on barHdl=bar(Data); % 绘制误差棒 for i=1:size(err,2) errorbar(barHdl(i).XEndPoints,Data(:,i),err(:,i),... 'LineStyle','none','Color','k','LineWidth',.8) end Data=randi([20,35],[5,2]); err=rand([5,2]).*5; hold on barHdl=bar(Data,'stacked','BarWidth',.4); % 绘制误差棒 for i=1:size(err,2) errorbar(barHdl(i).XEndPoints,barHdl(i).YEndPoints,err(:,i),... 'LineStyle','none','Color','k','LineWidth',.8) end 5 每个柱状图不同颜色 X=randi([2,15],[1,25])+rand([1,25]); barHdl=bar(X); % 与数据等长数量的颜色 ColorList=[0.2235 0.2314 0.4745 0.2235 0.2314 0.4745 0.3216 0.3294 0.6392 0.4196 0.4314 0.8118 0.6118 0.6196 0.8706 0.3882 0.4745 0.2235 0.5088 0.5951 0.2971 0.5490 0.6353 0.3216 0.7098 0.8118 0.4196 0.8078 0.8588 0.6118 0.5490 0.4275 0.1922 0.7412 0.6196 0.2235 0.8235 0.6745 0.2725 0.9059 0.7294 0.3216 0.9059 0.7961 0.5804 0.5176 0.2353 0.2235 0.6784 0.2863 0.2902 0.8392 0.3804 0.4196 0.8559 0.4324 0.4676 0.9059 0.5882 0.6118 0.4824 0.2549 0.4510 0.6471 0.3176 0.5804 0.8078 0.4275 0.7412 0.8706 0.6196 0.8392 0.8706 0.6196 0.8392]; barHdl.FaceColor='flat'; barHdl.CData=ColorList; 6 金字塔图 X1=[2,2.3,3.7,4.1,5.1,6,7,8]; X2=[1,1.7,1.9,3.8,4.7,5,7,8]; Label1={'A1','A2','A3','A4','A5','A6','A7','A8'}; Label2={'B1','B2','B3','B4','B5','B6','B7','B8'}; fig=figure(); ax1=axes('Parent',fig); % defualtAxes() hold on;axis tight;box off ax1.Position=[.1,.08,1/2-.1,.9]; ax1.YDir='reverse'; ax1.XDir='reverse'; ax1.YTickLabel=Label1; barh(ax1,X1,'FaceColor',[0,.447,.741]) ax2=axes('Parent',fig);hold on;axis tight % defualtAxes() hold on;axis tight;box off ax2.Position=[1/2,.08,1/2-.1,.9]; ax2.YDir='reverse'; ax2.YAxisLocation='right'; ax2.YTickLabel=Label2; barh(ax2,X2,'FaceColor',[.85,.325,.098]) |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |