Visualizing heat maps

Let's look at how to visualize heat maps in this recipe. This is a pictorial representation of data where two groups are associated point by point. The individual values that are contained in a matrix are represented as color values in the plot.

How to do it…

  1. Create a new Python file, and import the following packages:
    import numpy as np
    import matplotlib.pyplot as plt
  2. Define the two groups:
    # Define the two groups 
    group1 = ['France', 'Italy', 'Spain', 'Portugal', 'Germany'] 
    group2 = ['Japan', 'China', 'Brazil', 'Russia', 'Australia']
  3. Generate a random 2D matrix:
    # Generate some random values
    data = np.random.rand(5, 5)
  4. Create a figure:
    # Create a figure
    fig, ax = plt.subplots()
  5. Create the heat map:
    # Create the heat map heatmap = ax.pcolor(data, ...

Get Python Machine Learning Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.