漂亮,美观的图表之Matlab强势回归~~~~走你9

首先介绍一个函数 sparkline , 是一种描述类型的信息图,特点是小尺寸,高数据,并
 提供一个简明的趋势。

代码简介:

function sparkline(xdata,ydata,labels1,labels2)

%SPARKLINE(XDATA,YDATA,LABELS1,LABELS2) creates a graph with sparklines
% XDATA and YDATA are cell arrays of vectors of x and corresponding y
% values. LABELS1 give the labels you want corresponding to each sparkline
% to be located at the start of the line. LABELS2 give the labels you want 
% corresponding to each sparkline to be located at the end of the line. 

% No borders necessary - span the axes out to total available space
% make the plots by bumping up each sparkline with an arbitrary unit of
% separation. Here unitOfSep=1;
unitOfSep=1;

figure; axes('position',[0 0 1 .9]);hold on;
endPt = -1;
startPt = 1e100;
for i = 1:length(xdata)
    % Plot SparkLines
    plot(xdata{i}, ydata{i}+ (i-1)*+unitOfSep,'k'); 
    
    maxp{i} = find(ydata{i}==max(ydata{i})); 
    minp{i} = find(ydata{i}==min(ydata{i}));

    plot(xdata{i}(maxp{i}),ydata{i}(maxp{i})+ (i-1)*+unitOfSep,'bo','MarkerFaceColor','b');
    plot(xdata{i}(minp{i}),ydata{i}(minp{i})+ (i-1)*+unitOfSep,'ro','MarkerFaceColor','r');
    text(xdata{i}(end), mean(ydata{i})+ (i-1)*+unitOfSep,labels1{i},'HorizontalAlignment','right');
    text(xdata{i}(1), mean(ydata{i})+ (i-1)*+unitOfSep,labels2{i},'HorizontalAlignment','left');
    
    endPt = max([xdata{i}(1) endPt]);
    startPt= min([xdata{i}(end) startPt]);
end
text(startPt+2, i*unitOfSep+.7,'SparkLines with Stock Prices (1/1/2011 to 12/31/2011)','fontsize',14);
set(gca,'visible','off','ylim',[0+unitOfSep/2 i*unitOfSep+unitOfSep/2],...
    'yticklabel',[],'xlim',[startPt-.15*(endPt-startPt) endPt+.15*(endPt-startPt)],...
    'xticklabel',[],'TickLength',[0 0]);
set(gcf,'Color',[1 1 1],'Paperpositionmode','auto');


主要的部分代码:

stocks = {'AAPL','GOOG','MSFT','SLB','YHOO','S&P','GE'};
rangeMIN = datenum('1/1/2011');
rangeMAX = datenum('12/31/2011');

%% Preprocess
for i = 1:length(dt)    
    % Data Processing
    % convert date to a numeric format
    cc={dateD{i}{2:end,1}};
    dateD{i} = datenum({dateD{i}{2:end,1}});
    % find dates in range
    idx = find(dateD{i} >= rangeMIN & dateD{i} <= rangeMAX);
    dt{i} = dt{i}(idx); 
    % extract data in range
    dateD{i} = dateD{i}(idx);
    % normalize
    dtn{i} = dt{i}./max(dt{i});
    clear idx
    labels2{i} = num2str(dt{i}(end));    
end

%% Call spark lines 
sparkline(dateD,dtn,stocks,labels2);


 

效果图:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值