Heat map using pictures
I made a heat map calendar a few months ago and it inspired me to write this article. The heat map calendar changes the background color of each cell unlike the technique used here where I change the brightness of each picture based on numbers from a data set.
The drop-down list below allows you to change statistics, it contains three items: Population, Area and Population/area.
The map changes automatically when you change the value in the drop-down list, the workbook contains Event code that is run when the value in the drop-down list changes.
Instructions
Step 1 - Find and copy a map
- I found a map at the Wikimedia commons website.
- Press with right mouse button on on the image you want to use and press with left mouse button on "Copy".
- Paste it to your favorite image editing software.
Step 2 - Copy each state/country/territory
This step, step 3 and 4 are somewhat tedious if you have many images to copy.
- Select the "Magic Wand" tool. I made this in paint.net.
- Tolerance : 0%.
- Press with mouse on a state.
- Copy the selected area (Ctrl + c).
Step 3 - Paste to Excel
- Start Excel.
- Paste the picture to a worksheet.
- Select the picture and name it using the name box located almost at the top left corner, see image below.
The name is important, we will use it in a macro to change the brightness.
Step 4 - Select a transparent color
- Double press with left mouse button on picture. This takes you to the "Format" tab on the ribbon.
- Press with left mouse button on "Color" button.
- Press with left mouse button on "Set Transparent Color".
- Press with left mouse button on a "white" area on the picture.
Repeat step 2 - 4 and copy all states to Excel.
Step 5 - Organize pictures
Press and hold on a state and then drag to the location you want. You will se something like the image above when all states have been copied to the worksheet.
Step 6 - Create a group (optional)
Make a group of all pictures and you will be able to resize the entire map in one single step.
- Select all pictures. Tip! Use the "Selection pane" on the "Format" tab.
- Press with right mouse button on on a picture.
- Press with left mouse button on Group | Group.
- Size handles appear when you select the group.
- Press and hold on one of the handles then drag with the mouse to resize the group.
Step 7 - Create an Excel defined Table
I geted data from wikipedia and created the Excel Table above. An Excel Table has a few great features, one of them is that they use structured references which makes them really easy to reference and work with.
Here is how to convert a data set to an Excel Table:
- Press with mouse on any cell in your data set.
- Press shortcut keys CTRL + T and a dialog box appears.
- Press with left mouse button on checkbox if you data set contains header names.
- Press with left mouse button on OK.
The data set changes it appearance and is now formatted differently, this tells you that you now have an Excel Table.
Table column Column1 contains a single value. The brightness property is ranging from 0 to 1. 0 - darkest and 1 brightest. It is hard to see pictures with a brightness ranging from 0.5 to 1 so I am using values from 0 to 0.5.
We need to create values between 0 (zero) and 0.5 based on the data in one state compared to the maximum and minimum value in the same column, the formulas are shown below.
Formula in table column %:
Excel Tables uses structured references which are different than regular cell references. [@Population] is a reference to a value in column Population on the same row as the formula. [Population] is a reference to all values in column Population.
The formula above calculates a value between 0 and 0.5 based on the ratio of a given state's population compared to the largest population of the values in column "Population". The formulas below calculate a ratio using the values in column "Area sq km" and "Pop / km".
Formula in table column %2:
Formula in table column %3:
What we now have done is indexing values, I have made a post about indexing data before:
Compare your stock portfolio with S&P500 in excel
Step 8 - Create a drop down list
A drop-down list allows you to control which values the user can choose from, they are easy to create:
- Select cell D26.
- Go to tab "Data" on the ribbon.
- Press with left mouse button on "Data Validation" button.
- Select "List".
- Source: United states of America,Population, Area, Population / area.
- Press with left mouse button on OK.
There is also a hidden formula in cell F26. The following formula calculates a value that the event code below uses.
These steps demonstrate how to hide the value in cell F26.
- Select cell F26.
- Press CTRL + 1 to open the "Format Cells" dialog box.
- Select Custom, see image below.
- Type ;;;
- Press OK button.
The value in cell F26 is still there but you can't see it. The worksheet looks cleaner with this setup.
Step 9 - Change picture brightness for each picture
- Copy code below. (CTRL + c)
- Press with right mouse button on on sheet name, see image above.
- Press with left mouse button on "View Code".
- Paste code below to code window. (CTRL + v)
- Exit VB Editor and return to Excel.
'Event code is rund when a cell is changed in worksheet "Map". Private Sub Worksheet_Change(ByVal Target As Range) 'Dimension variables and declare data types Dim r As Single, c As Integer, Arr() As Variant 'Check if cell's address is D26 If Target.Address = "$D$26" Then 'Populate variable Arr with values from Excel Table "Table1" in worksheet "Table" Arr = Worksheets("Table").Range("Table1").Value 'Iterate through values in a given column based on the hidden value in cell F26 For r = LBound(Arr, 1) To UBound(Arr, 1) 'The With ... End With statement allows you to write shorter code by referring to an object only once instead of using it with each property. With ActiveSheet.Shapes.Range(Arr(r, 1)).PictureFormat 'Set brightness from value in array variable based on row and column number .Brightness = Arr(r, Range("F26")) End With Next r End If End Sub
If you like maps, check out this post: Use a map in an Excel chart
Recommended links
- Create a Map chart in Excel
- How to make a killer map using Excel in under 5 minutes with PowerMap plugin
Charts category
Table of Contents How to add lines between stacked columns/bars (Excel charts) Use slicers to quickly filter chart data How […]
Table of Contents How to create an interactive Excel chart How to filter chart data How to build an interactive […]
What's on this page Custom data labels Improve your X Y Scatter Chart with custom data labels How to apply custom […]
Maps category
More than 1300 Excel formulasExcel categories
7 Responses to “Heat map using pictures”
Leave a Reply
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Contact Oscar
You can contact me through this contact form
Helo,
Thanks for this great tutorial. I would really like to change the colors of the pictures on the map, not just the brightness.
It looks like "PictureFormat" object only supports Brightness and Contrast values.
I have been playing with your code a bit and added the following code before "Next r":
With ActiveSheet.Shapes.Range(Arr(r, 1)).Fill
.ForeColor.RGB = RGB(255, 1, 1)
End With
This will change the color of each image BUT for some reason it will only change the transparent background of the image, not the actual shape.
If I select a picture and chose "Recolor" from the menu it works corretly on the non-transparent portion of the image but apparently
there is no VBA code for this action.
Any ideas?
thanks
Frank,
Any ideas?
No, but if you discover a workaround I would be happy if you let me know.
Perhaps the following website is helpful:
https://www.clearlyandsimply.com/clearly_and_simply/2009/06/choropleth-maps-with-excel.html
thanks for the links
This here is exactly what I was looking for:
https://www.clearlyandsimply.com/clearly_and_simply/2012/09/faster-choropleth-maps-with-microsoft-excel.html
Hi,
Thanks for the tut but I'm stuck on step 8. I wasn't able to copy and paste in the formulas for column % even after replacing 'population' with my column title of interest. But I was able to get around this by just inputting an alternative formula:
=1-((C6)/MAX(C6:C36)*0.5+0.5)
To get brightness values.
Now I'm up to to step 10 and the macros is failing due to 'the index into the specified collection is out of bounds'. Debugging specifically highlights the line:
With ActiveSheet.Shapes.Range(Arr(r, 1)).PictureFormat
Any help would be much appreciated.
Thanks in advance
Ignore that post - sorted
Hi,
One issue I had (but easily fixed) was giving my table of data the name of "Table1" which corresponds with the code provided. This may be helpful to check/add to the Step by Step.
However, now I am currently stuck in the same place that Steve had trouble with.
Debugging specifically highlights "With ActiveSheet.Shapes.Range(Arr(r, 1)).PictureFormat" but I cannot figure out why.
Any advice? Thanks!
I'm stuck where Steve and Elizabeth hit a snag. Debugging specifically highlights "With ActiveSheet.Shapes.Range(Arr(r, 1)).PictureFormat" but I cannot figure out why. Any suggestions?
I'm admittedly a bit of a newby in the VBA space, but I'm trying to learn. I've tried to faithfully recreate the formate that Oscar initially posted, but the code is failing for some reason.
Thank you!