I have created a heat map I want to give time series (1980-2019) monthly as x ticks I have daily rainfall data .
4 views (last 30 days)
Show older comments
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1076795/image.png)
clc; clear all; close all;
addpath 'D:\data\Heat_map\New folder'
addpath 'D:\data\Heat_map\New folder\';
cor_path='D:\data\Heat_map\'
Kendall_Tau = xlsread(sprintf('%s%s%s',fullfile(cor_path),'Heat_map_matrix_rainfall.xlsx'));
Z_Dependence1 = Kendall_Tau(1:end,4:end);
figure(1);
t1 = datetime(1980,1,1);
t2 = datetime(2019,12,1);
Tall = t1:caldays(1):t2;
Tall = t1:caldays(1):t2;
Tall = Tall';
Z = datevec(Tall);
Z(:, ~any(Z,1) ) = []; %columns
y = heatmap(Z_Dependence1,[], [])
mycolormap=customcolormap([0 1],{'#FFFFFF', '#00FF00'});
colorbar;
colormap(mycolormap);
if ismatrix(Z_Dependence1)==1
set(y, 'AlphaData', ~isnan(Z_Dependence1))
end
set(gca, 'Color', [0.8, 0.8, 0.8]);
set(gca,'FontName','Arial','FontSize',11);
if ismatrix(Z_Dependence1)==1
set(y, 'AlphaData', (Z_Dependence1==1005))
end
set(gca, 'Color', [1, 1, 1]);
xticks(1980:5:2019)
set(gca,'Ytick',[1:1:11],'yticklabel',({'TEHRI','GANGTOK', 'KAKIMPONG','DARJEELING','GUWAHATI','KOHIMA','SHILLONG','SILCHAR','IMPHAL','AGARTALA','AIZWAL'}))
H_Slope = heatmap(U1_Qpeak,[],[],[],'GridLines', 'None','colorbar', false,'fontsize',12);
Xlabel = [1889:10:1980];
serialDates = datenum(num2str(Xlabel(:)), 'yyyy');
Xlabel_str = datestr(serialDates,10);
set(gca,'Xtick',[1:1200:13],'xticklabel',({'1970-1974','1980-1990','1985-1987','1985-1989','1985-1990','1996-2004','2001-2003','2001-2007'...
'2005-2008','2008-2010','2010-2015','2010-2019','2012-2019'}),'Fontname','Arial','Fontsize',20)
% rotateXLabels(gca(), 45);
set(gca,'fontname','arial','fontsize',12);
serialDates = datenum(num2str(Xlabel(:)), 'yyyy');
Xlabel_str = datestr(serialDates);
set(gca,'Xtick',[1:5:33],'xticklabel',{Xlabel_str});
rotateXLabels(gca(), 45);
grad=colorGradient([1 0 0],[0.5 0.8 1],128);
surf(peaks)
colormap(grad);
[grad,im]=colorGradient([1 0 0],[0.5 0.8 1],128);
image(im); %display an image with the color gradient.
- I want to insert xticks as timeseries from 1980-2019 monthly and also i want to define color for values such as zero and NaN(the color should be different for both values)
0 Comments
Answers (1)
Aditya
on 5 Oct 2023
Hi Danish,
From your query what I understood is that you wanted to insert xticks as timeseries in your heatmap and wanted to change the color of values such as zero and NaN.
To insert xticks as a timeseries in your heatmap, you can use the following code snippet:
% Set x-axis tick locations and labels
startYear = 1980;
endYear = 2019;
numYears = endYear - startYear + 1;
monthsPerYear = 12;
numMonths = numYears * monthsPerYear;
% Generate the date labels for the x-axis
dates = datetime(startYear, 1, 1) + calmonths(0:numMonths-1);
dateLabels = cellstr(datestr(dates, 'yyyy-mm'));
% Set the x-axis tick locations and labels
h.XDisplayData = 1:numMonths;
h.XDisplayLabels = dateLabels;
To change the color of zero and NaN values, you can use the following code snippet:
% Set the colormap
map = colormap(heatmap(data));
map(1,:) = [1 0 0]; % changes the color of values that are zero
% Create the heatmap with modified colormap
figure;
h = heatmap(data, 'Colormap', map, 'MissingDataColor',[0.8 0.8 0.8]); % ‘MissingDataColor’ helps to color the NaN values
Hope this helps!
0 Comments
See Also
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!