MATLAB | 可视化 | 藏在风景里的k线图
Posted slandarer
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MATLAB | 可视化 | 藏在风景里的k线图相关的知识,希望对你有一定的参考价值。
一天一个气死老板的小技巧
老板:是谁教你这么画k线图的??
本期又是一个花里胡哨的数据可视化,前两天刷到了耐克的视觉设计师Gladys Orteza
绘制的k线图作品,把沉闷的股票图变成了精彩的风景,但是那些大部分是真的完全看不清,我这里挑选了几个能看清的k线图风格将其用MATLAB进行了实现。
效果
代码
代码中使用的数据
- SimulatedStock.mat
是MATLAB自带的示例数据,因此不需要下载,不过要绘制k线图,仍需安装Financial Toolbox
工具箱。
效果一 极光
% @author : slandarer
% gzh : slandarer随笔
load SimulatedStock.mat;
Data=TMW(end-200:end-50,:);
CHdl=candle(Data,'b');
title('Candlestick chart for TMW')
% 坐标区域基础修饰 =========================================================
fig=gcf;
set(fig,'Units','normalized','Position',[.1,.1,.7,.6])
ax=gca;hold on;axis tight
YLim=ax.YLim;
ax.YLim=ax.YLim+[-1.5,1].*diff(YLim);
YLim=ax.YLim;
XLim=ax.XLim;
ax.PlotBoxAspectRatio=[2,1,1];
ax.Color=[0,0,0];
maxY=YLim(1)+diff(YLim)*1.5/3.5;
minY=YLim(1)+diff(YLim)/4.5;
% 绘制背景 =================================================================
matSize=[300,300];
colorList=[1,1,2;16,20,49;33,42,101;37,64,119;24,99,104;24,99,104;12,148,86;1,1,2;1,1,2]./255;
YList=((0:(matSize(2)-1))./(matSize(2)-1))';
XList=ones(1,matSize(1));
% 线性插值
colorMat(:,:,1)=interp1(linspace(0,1,size(colorList,1)),colorList(:,1),YList)*XList;
colorMat(:,:,2)=interp1(linspace(0,1,size(colorList,1)),colorList(:,2),YList)*XList;
colorMat(:,:,3)=interp1(linspace(0,1,size(colorList,1)),colorList(:,3),YList)*XList;
[XMesh,YMesh]=meshgrid(linspace(XLim(1),XLim(2),300),linspace(YLim(2),YLim(1),300));
surf(XMesh,YMesh,zeros(size(XMesh)),colorMat,'EdgeColor','none');
matSize=[300,300];
colorList=[26,110,106;26,110,106;35,72,118;33,40,95;16,22,56;1,1,1;1,1,1]./255;
YList=((0:(matSize(2)-1))./(matSize(2)-1))';
XList=ones(1,matSize(1));
% 线性插值
colorMat(:,:,1)=interp1(linspace(0,1,size(colorList,1)),colorList(:,1),YList)*XList;
colorMat(:,:,2)=interp1(linspace(0,1,size(colorList,1)),colorList(:,2),YList)*XList;
colorMat(:,:,3)=interp1(linspace(0,1,size(colorList,1)),colorList(:,3),YList)*XList;
[XMesh,YMesh]=meshgrid(linspace(XLim(1),XLim(2),300),linspace(minY+(maxY-minY)/10,YLim(1),300));
surf(XMesh,YMesh,zeros(size(XMesh)),colorMat,'EdgeColor','none','FaceAlpha',.5);
% 绘制星星 =================================================================
Xs=diff(XLim).*rand([20,1])+XLim(1);
Ys=(YLim(2)-maxY).*rand([20,1])+maxY;
scatter(Xs,Ys,3,'filled','CData',[.8,.8,.8]);
Xs=diff(XLim).*rand([20,1])+XLim(1);
Ys=(YLim(2)-maxY).*rand([20,1])+maxY;
scatter(Xs,Ys,5,'filled','CData',[.6,.6,.6]);
% 修改蜡烛图配色 ===========================================================
rColor=[242,218,128]./255;
gColor=[9,28,48]./255;
% 获取竖直线数据
LineHdl=CHdl(1);
LineDataX=reshape(LineHdl.XData,3,[]);
LineDataY=reshape(LineHdl.YData,3,[]);
fill([LineDataX(1,1),LineDataX(1,:),LineDataX(1,end)],...
[minY,LineDataY(1,:),minY],[10,28,48]./270);
for i=2:length(CHdl)
if CHdl(i).FaceColor(1)==1
plot(LineDataX(:,i-1),LineDataY(:,i-1),'Color',rColor)
fill(CHdl(i).XData,CHdl(i).YData,rColor,'EdgeColor',rColor)
else
plot(LineDataX(:,i-1),LineDataY(:,i-1),'Color',gColor)
fill(CHdl(i).XData,CHdl(i).YData,gColor,'EdgeColor',gColor)
end
end
delete(LineHdl);
% 绘制柱状图 ===============================================================
YData=Data.Close-Data.Open;
absYData=abs(YData);
absYData1=absYData./(max(absYData)-min(absYData)).*(maxY-minY)+minY;
absYData2=-absYData./(max(absYData)-min(absYData)).*(maxY-minY)+minY;
fill([LineDataX(1,1),LineDataX(1,:),LineDataX(1,end)],...
[YLim(1)+diff(YLim)/4.5,absYData1(:).',YLim(1)+diff(YLim)/4.5],[.01,.01,.01]);
disp(char([64 97 117 116 104 111 114 32 58 32 115 108 97 110 100 97 114 101 114]))
bar(Data.Time(YData>0),absYData1(YData>0),'BaseValue',minY,'EdgeColor','none','FaceColor',[29,170,112]./255);
bar(Data.Time(YData<0),absYData1(YData<0),'BaseValue',minY,'EdgeColor','none','FaceColor',[23,105,103]./255);
bar(Data.Time(YData>0),absYData2(YData>0),'BaseValue',minY,'EdgeColor','none','FaceColor',[29,170,112]./255,'FaceAlpha',.6);
bar(Data.Time(YData<0),absYData2(YData<0),'BaseValue',minY,'EdgeColor','none','FaceColor',[23,105,103]./255,'FaceAlpha',.6);
xq=linspace(XLim(1),XLim(2),15);
yq=interp1(LineDataX(1,:),absYData1,xq,'spline');
scatter(xq,yq,15,'filled','CData',[128,169,90]./255)
效果二 暖调
% @author : slandarer
% gzh : slandarer随笔
load SimulatedStock.mat;
Data=TMW(end-200:end-50,:);
CHdl=candle(Data,'b');
title('Candlestick chart for TMW')
% 坐标区域基础修饰 ==MATLAB | 可视化 | 藏在风景里的k线图