Set colors for specific entries of heat map

41 views (last 30 days)
Hi all,
I have constructed an heatmap based on a diagonal 50x50 matrix (that I attached to the csv file below).
My code looks as follows:
T1 = readtable('/Users/federiconutarelli/Desktop/Second_work/pivot_own.csv');
T1 = removevars(T1, 'Var1');
T1(1,:) = [];
T1_mat = T1{:,:};
T1_mat(T1_mat==0) = NaN;
ax = gca;
xvalues = {'ACETYLCYSTEINE' 'ACETYLSALICYLIC ACID' 'ACICLOVIR' 'ALPRAZOLAM' 'AMLODIPINE' 'AMOXICILLIN' 'BETAMETHASONE' 'BISOPROLOL' 'BUDESONIDE' 'CARBAMAZEPINE' 'CARBIDOPA + LEVODOPA' 'CETIRIZINE' 'CICLOSPORIN' 'CLINDAMYCIN' 'CLOBETASOL' 'DESMOPRESSIN' 'DICLOFENAC' 'EPINEPHRINE' 'ESTRADIOL' 'FINASTERIDE' 'FLUCONAZOLE' 'FLUOXETINE' 'FUROSEMIDE' 'GOSERELIN' 'HYDROCORTISONE' 'IBUPROFEN' 'INSULIN HUMAN ISOPHANE' 'IRON FERROUS' 'LACTULOSE' 'LEVOTHYROXINE SODIUM' 'LOPERAMIDE' 'MAGNESIUM' 'MESALAZINE' 'METHYLPREDNISOLONE' 'METOPROLOL' 'METRONIDAZOLE' 'NICOTINE' 'OCTREOTIDE' 'OMEPRAZOLE' 'ONDANSETRON' 'PARACETAMOL' 'POTASSIUM' 'RISPERIDONE' 'SALBUTAMOL' 'SPIRONOLACTONE' 'SULFASALAZINE' 'TESTOSTERONE' 'TOBRAMYCIN' 'TRIPTORELIN' 'VALPROIC ACID'};
yvalues = {'ACETYLCYSTEINE' 'ACETYLSALICYLIC ACID' 'ACICLOVIR' 'ALPRAZOLAM' 'AMLODIPINE' 'AMOXICILLIN' 'BETAMETHASONE' 'BISOPROLOL' 'BUDESONIDE' 'CARBAMAZEPINE' 'CARBIDOPA + LEVODOPA' 'CETIRIZINE' 'CICLOSPORIN' 'CLINDAMYCIN' 'CLOBETASOL' 'DESMOPRESSIN' 'DICLOFENAC' 'EPINEPHRINE' 'ESTRADIOL' 'FINASTERIDE' 'FLUCONAZOLE' 'FLUOXETINE' 'FUROSEMIDE' 'GOSERELIN' 'HYDROCORTISONE' 'IBUPROFEN' 'INSULIN HUMAN ISOPHANE' 'IRON FERROUS' 'LACTULOSE' 'LEVOTHYROXINE SODIUM' 'LOPERAMIDE' 'MAGNESIUM' 'MESALAZINE' 'METHYLPREDNISOLONE' 'METOPROLOL' 'METRONIDAZOLE' 'NICOTINE' 'OCTREOTIDE' 'OMEPRAZOLE' 'ONDANSETRON' 'PARACETAMOL' 'POTASSIUM' 'RISPERIDONE' 'SALBUTAMOL' 'SPIRONOLACTONE' 'SULFASALAZINE' 'TESTOSTERONE' 'TOBRAMYCIN' 'TRIPTORELIN' 'VALPROIC ACID'};
h = heatmap(xvalues,yvalues,T1_mat);
h.Title = 'Own Elasticities';
h.XLabel = 'Molecules';
h.YLabel = 'Molecules';
%colormap(flipud(hot))
Now what I would like to do is to color some of the entries on the main diagonal according to shades of red and make a separate legend with a writing "Significant entries colored according to shades of red"
Is it possible to do so?
Thank you,
Federico

Answers (1)

Salman Ahmed
Salman Ahmed on 14 Oct 2021
Hi,
From my understanding you wish to highlight specific cells in a heatmap but this is not a feature present currently with heatmap. Some of the workarounds that you can proceed with are:
  1. Use imagesc to achieve a similar map. Refer link for more details.
  2. Use a custom colormap. You can mark the cells to be highlighted at the extreme limit and assign a different color in the your custom map. Note, you can create one using colormapeditor. Have a look at a sample modified code, where I have highlighted the (1,1) cell:
T1 = readtable('pivot_own.csv');
T1 = removevars(T1, 'Var1');
T1(1,:) = [];
T1_mat = T1{:,:};
T1_mat(T1_mat==0) = NaN;
T1_mat(1,1) = -1.5; % Marking cell at extreme
ax = gca;
xvalues = {'ACETYLCYSTEINE' 'ACETYLSALICYLIC ACID' 'ACICLOVIR' 'ALPRAZOLAM' 'AMLODIPINE' 'AMOXICILLIN' 'BETAMETHASONE' 'BISOPROLOL' 'BUDESONIDE' 'CARBAMAZEPINE' 'CARBIDOPA + LEVODOPA' 'CETIRIZINE' 'CICLOSPORIN' 'CLINDAMYCIN' 'CLOBETASOL' 'DESMOPRESSIN' 'DICLOFENAC' 'EPINEPHRINE' 'ESTRADIOL' 'FINASTERIDE' 'FLUCONAZOLE' 'FLUOXETINE' 'FUROSEMIDE' 'GOSERELIN' 'HYDROCORTISONE' 'IBUPROFEN' 'INSULIN HUMAN ISOPHANE' 'IRON FERROUS' 'LACTULOSE' 'LEVOTHYROXINE SODIUM' 'LOPERAMIDE' 'MAGNESIUM' 'MESALAZINE' 'METHYLPREDNISOLONE' 'METOPROLOL' 'METRONIDAZOLE' 'NICOTINE' 'OCTREOTIDE' 'OMEPRAZOLE' 'ONDANSETRON' 'PARACETAMOL' 'POTASSIUM' 'RISPERIDONE' 'SALBUTAMOL' 'SPIRONOLACTONE' 'SULFASALAZINE' 'TESTOSTERONE' 'TOBRAMYCIN' 'TRIPTORELIN' 'VALPROIC ACID'};
yvalues = {'ACETYLCYSTEINE' 'ACETYLSALICYLIC ACID' 'ACICLOVIR' 'ALPRAZOLAM' 'AMLODIPINE' 'AMOXICILLIN' 'BETAMETHASONE' 'BISOPROLOL' 'BUDESONIDE' 'CARBAMAZEPINE' 'CARBIDOPA + LEVODOPA' 'CETIRIZINE' 'CICLOSPORIN' 'CLINDAMYCIN' 'CLOBETASOL' 'DESMOPRESSIN' 'DICLOFENAC' 'EPINEPHRINE' 'ESTRADIOL' 'FINASTERIDE' 'FLUCONAZOLE' 'FLUOXETINE' 'FUROSEMIDE' 'GOSERELIN' 'HYDROCORTISONE' 'IBUPROFEN' 'INSULIN HUMAN ISOPHANE' 'IRON FERROUS' 'LACTULOSE' 'LEVOTHYROXINE SODIUM' 'LOPERAMIDE' 'MAGNESIUM' 'MESALAZINE' 'METHYLPREDNISOLONE' 'METOPROLOL' 'METRONIDAZOLE' 'NICOTINE' 'OCTREOTIDE' 'OMEPRAZOLE' 'ONDANSETRON' 'PARACETAMOL' 'POTASSIUM' 'RISPERIDONE' 'SALBUTAMOL' 'SPIRONOLACTONE' 'SULFASALAZINE' 'TESTOSTERONE' 'TOBRAMYCIN' 'TRIPTORELIN' 'VALPROIC ACID'};
h = heatmap(xvalues,yvalues,T1_mat);
h.Colormap = CustomColormap; % Add your custom map here.
h.Title = 'Own Elasticities';
h.XLabel = 'Molecules';
h.YLabel = 'Molecules';
Note you can add text to indicate the extreme value indicates highlighted. Hope it helps.

Tags

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!