加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
HeatmapCalendar.m 4.56 KB
一键复制 编辑 原始数据 按行查看 历史
好玩的matlab 提交于 2023-11-04 02:25 . 第1次提交
classdef HeatmapCalendar
%--------------------------------------------------------------------------
% @Author: 好玩的Matlab
% @公众号:好玩的Matlab
% @Created: 11,02,2023
% @Email: 2377389590@qq.com
% 尊重劳动成果,转载请备注推文链接和公众号名,严禁商用。
%--------------------------------------------------------------------------
properties
Data
Year
Month
Colormap=hsv(255);
CMapNum=255;
end
methods
function obj = HeatmapCalendar(varargin)
disp(char([20844 20247 21495 58 22909 29609 30340 77 97 116 108 97 98]))
if nargin > 0
for i = 1:2:nargin
if strcmpi(varargin{i}, 'Data')
obj.Data = varargin{i+1};
elseif strcmpi(varargin{i}, 'Year')
obj.Year = varargin{i+1};
elseif strcmpi(varargin{i}, 'Month')
obj.Month = varargin{i+1};
elseif strcmpi(varargin{i}, 'Colormap')
obj.Colormap = varargin{i+1};
elseif strcmpi(varargin{i}, 'CMapNum')
obj.CMapNum = varargin{i+1};
end
end
end
if length(obj.Data)~=eomday(obj.Year,obj.Month)
error('数据和当月时间数据不一致')
end
obj.plot();
end
function plot(obj)
dayNames = {'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'};
monthNames = {'January', 'February', 'March', 'April', 'May', 'June', ...
'July', 'August', 'September', 'October', 'November', 'December'};
firstDay = weekday(datenum(obj.Year, obj.Month, 1));
daysInMonth = eomday(obj.Year,obj.Month);
hold on;
numWeeks = ceil((firstDay - 1 + daysInMonth) / 7)+1;
title([monthNames{obj.Month},' ',num2str(obj.Year)], ...
'HorizontalAlignment', 'center', 'FontSize', 16, 'FontWeight', 'bold','fontname','Times New Roman');
for i = 1:length(dayNames)
text((i-0.5)/7, 1-0.5/numWeeks, dayNames{i}, ...
'HorizontalAlignment', 'center', 'FontSize', 12, 'FontWeight', 'bold','fontname','Times New Roman');
end
normalizedData = (obj.Data - min(obj.Data)) / (max(obj.Data) - min(obj.Data));
cmap = colormap(obj.makeColorMap(obj.Colormap,obj.CMapNum));
obj.Colormap=cmap;
dayCounter = 1;
for i = 2:numWeeks+1
for j = 1:7
left = (j-1)/7;
bottom = 1 - i/numWeeks;
width = 1/7;
height = 1/numWeeks;
if dayCounter > daysInMonth
rectangle('Position', [left, bottom, width, height], 'EdgeColor', 'none', 'FaceColor', 'none');
elseif i > 1 && j >= firstDay || i > 2
colorIdx = max(1, ceil(normalizedData(dayCounter) * size(cmap, 1)));
cellColor = cmap(colorIdx, :);
rectangle('Position', [left, bottom, width, height], 'EdgeColor',[1,1,1]*0.95, 'FaceColor', cellColor,'LineWidth',2);
text(left + width/2, bottom + height/2, num2str(dayCounter),'Color',1-cellColor, ...
'HorizontalAlignment', 'center', 'FontSize', 14,'FontName','Times New Roman');
dayCounter = dayCounter + 1;
else
rectangle('Position', [left, bottom, width, height], 'EdgeColor', 'none', 'FaceColor', 'none');
end
end
end
cBar=colorbar;
colormap(cBar,obj.Colormap);
caxis([min(obj.Data) max(obj.Data)]);
cBar.LineWidth = 1.2;
cBar.TickLength = 0.015;
cBar.TickDirection = 'both';
cBar.FontSize=12;
cBar.FontName='Times New Roman';
axis([0 1 0 1]);
axis off;
pbaspect([1 1 1]);
hold off;
end
end
methods(Access=private)
function cMap=makeColorMap(obj,colorlist,num)
color.Num= num;
color.list=colorlist;
for col=1:size(color.list,2)
x=1:size(color.list,1);
xi=linspace(1,size(color.list,1),color.Num);
color.map(:,col)=interp1(x,color.list(:,col),xi);
end
cMap=color.map;
end
end
end
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化