I have array called CV which has a value that can be 0 to 1 or bigger than one. CV is an array that has 35 values. I want to make a heatmap to show the value of each CV. I wasn the zero to be very ligth green as it increases to 1 becomes darker green and after one becomes red.
I was thinking I can use h = heatmap(xvalues,yvalues,cdata) line but I dont know what would be corresponding x and y and how I can design colors?
Any help would be highly appreciated.
I attached the EXCEL and sample image of it.

3 comentarios

Cris LaPierre
Cris LaPierre el 3 de Oct. de 2019
Can you attach a sample data set for us to play around with?
Zeynab Mousavikhamene
Zeynab Mousavikhamene el 3 de Oct. de 2019
Editada: Zeynab Mousavikhamene el 3 de Oct. de 2019
I attached excel of a sample to the question.
Cris LaPierre
Cris LaPierre el 3 de Oct. de 2019
How do you envision your heatmap being arranged? These are usually grids. Your data would be a single column.

Iniciar sesión para comentar.

 Respuesta aceptada

Cris LaPierre
Cris LaPierre el 3 de Oct. de 2019
Editada: Cris LaPierre el 3 de Oct. de 2019

0 votos

Here's a sample of how I might do this. Note that I'm using some randomly generated numbers. The 5th column is integers.
I define a colormap of light green to dark green (credit to this page for getting me started), as well as red. By using the ColorLimit name-value pair, I can force any value greater than 1 to be red.
This was more guess and check than actual reasoning, so no guarantees it will work in all scenarios. However, it should be close enough to give you some ideas.
% Create a 7x5 matrix of random numbers where 28 are <1 and 7 are >=1
CV = reshape([rand(28,1); randi(15,7,1)],7,5)
% Define values of green and red
gr = [linspace(.75,0,10)' ones(10,1) linspace(.75,0,10)';...
zeros(10,1) linspace(1,.5,10)' zeros(10,1)];
red = [1 0 0];
% Create the colormap
cmap = [gr; red];
% Generate the heatmap
heatmap(CV,"Colormap",cmap,"ColorLimits",[0 1.1])

6 comentarios

Zeynab Mousavikhamene
Zeynab Mousavikhamene el 3 de Oct. de 2019
Editada: Zeynab Mousavikhamene el 3 de Oct. de 2019
Thanks @ Cris LaPierre. Why did you use random number?
Can you please explain what you mean in this comment: "Create a 7x5 matrix of random numbers where 28 are <1 and 7 are >=1"? whati s 28? what is 7? why 7*5 matrix?
Cris LaPierre
Cris LaPierre el 3 de Oct. de 2019
I used random numbers when testing my solution because you had not yet posted your data. I created 35 values arranged into a grid of 7 rows and 5 columns.
You can replace that line of code with your actual data. The rest of the code should work with your data.
Zeynab Mousavikhamene
Zeynab Mousavikhamene el 3 de Oct. de 2019
Editada: Zeynab Mousavikhamene el 3 de Oct. de 2019
@Cris LaPierre Oh you couldnt see the excel file? I already uploaded that. They are sample of real data.
Cris LaPierre
Cris LaPierre el 3 de Oct. de 2019
I can now. They weren't there when you first posted your question.
Zeynab Mousavikhamene
Zeynab Mousavikhamene el 3 de Oct. de 2019
@Cris LaPierre was great start thank you. Any source to learn making great heatmaps? I checked matlab: https://www.mathworks.com/help/matlab/ref/heatmap.html description of heatmap but did not like it
Cris LaPierre
Cris LaPierre el 3 de Oct. de 2019
That's where I would have pointed you. You could also look into pcolor, surf, and other 3D plots. When rotated, they can look like heatmaps as well, and might provide additional settings you could take advantage of to create a plot that looks like a heatmap.

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by