Matthew Beckler's Home Page

Home :: Blog :: General :: Software :: Hardware :: Microcontrollers :: Artsy

Java Heat Map

A Heat Map is a common visualization method for viewing the values of a two-dimensional map. There are numerous ways to do three-dimensional graphing and charting, but in my humble opinion, at times it can be difficult to see all the essential details without being able to rotate the 3D chart. I like heat maps for visualizing this kind of data, as the top-down, color-based view doesn't hide any information.

I (along with the help of a few others) have created an easy-to-use Java class that draws heat maps in a JPanel. It should be easy to add into another project of your own. The range of X- and Y-coordinates are only used when drawing the labels on the axis, and do not affect the range of data that is plotted. All data in the array is plotted. You can turn on and off the title, both axis' labels, and the legend. There are a number of color gradients available, and you can always create your own by specifying an array of Color objects.

Data is provided to the HeatMap code in the form of a 2-dimensional array of doubles. The code is configurable to accept data with the y=0 row at either the top or bottom of the display, depending on if you prefer the computer graphics coordinate system, or the standard mathematical coordinate system.

A simple GUI application has been created (HeatMapDemo) that demonstrates the features of the HeatMap, and can be used to play around with most of the different options for the HeatMap. This demo program is included with the source code.

HeatMapDemo Screenshot

A simple class (HeatMapFrame) has been created to demonstrate you can easily add a HeatMap to your own Java Swing application:

import javax.swing.*;
import java.awt.*;

class HeatMapFrame extends JFrame
    HeatMap panel;

    public HeatMapFrame() throws Exception

        super("Heat Map Frame");
        double[][] data = HeatMap.generateSinCosData(100);
        boolean useGraphicsYAxis = true;

        // you can use a pre-defined gradient:

        panel = new HeatMap(data, useGraphicsYAxis, Gradient.GRADIENT_BLUE_TO_RED);

        // or you can also make a custom gradient:

        Color[] gradientColors = new Color[]{,,Color.yellow};
        Color[] customGradient = Gradient.createMultiGradient(gradientColors, 500);
        // set miscellaneous settings


        panel.setTitle("Height (m)");

        panel.setXAxisTitle("X-Distance (m)");

        panel.setYAxisTitle("Y-Distance (m)");

        panel.setCoordinateBounds(0, 6.28, 0, 6.28);



    // this function will be run from the EDT

    private static void createAndShowGUI() throws Exception

        HeatMapFrame hmf = new HeatMapFrame();


    public static void main(String[] args)
        SwingUtilities.invokeLater(new Runnable()
            public void run()
                catch (Exception e)

Javadoc: I have prepared some javadoc API for your reference.

Download: Click below to download the tar.gz or .zip package. The package contains the source code files and your very own copy of the GPL. If you are using this code, I'd appreciate it if you could let me know, I'm always curious to see who's using my software. Thanks!

Homepage Made with Vim! Validate HTML Email Me! Made with Inkscape! Validate CSS

Copyright © 2004 - 2017, Matthew L. Beckler, CC BY-SA 3.0
Last modified: 2016-01-04 11:12:13 PM (EST)