Home > Enterprise >  Why is np.arange(data.shape[1] 1)-.5 used to plot a heatmap?
Why is np.arange(data.shape[1] 1)-.5 used to plot a heatmap?

Time:12-20

I can't figure out what this piece of code does, because if I delete it, nothing changes in the heatmap, and I don't know what "data.shape[1] 1)-.5" and ".shape[]" are doing in general

# Now enter the following code snippet to define the heatmap function
# map. First you need to prepare the chart:
def heatmap(data, row_labels, col_labels, ax=None, cbar_kw={},
        cbarlabel='',
        **kwargs):
    if not ax:
        ax = plt.gca()
    im = ax.imshow(data, **kwargs)

# Define the colorbar as a colorbar, as specified in the following code snippet:
    cbar = ax.figure.colorbar(im, ax=ax, **cbar_kw)
    cbar.ax.set_ylabel(cbarlabel, rotation=-90, va='bottom')

# Show all checkboxes and mark them with the corresponding entries in the list:
    ax.set_xticks(np.arange(data.shape[1]))
    ax.set_yticks(np.arange(data.shape[0]))
    ax.set_xticklabels(col_labels)
    ax.set_yticklabels(row_labels)

# Adjust the horizontal axes so that the marks appear on top of the graph:
    ax.tick_params(top=True, bottom=False, labeltop=True, labelbottom=False)

# Rotate the check marks and set their alignment:
    plt.setp(ax.get_xticklabels(), rotation=-30, ha='right', rotation_mode='anchor')

# Turn off the frame and create a white grid for the graph, 
# as specified in the following code snippet:
    for edge, spine in ax.spines.items():
        spine.set_visible(False)

I can't understand this piece of code

    ax.set_xticks(np.arange(data.shape[1] 1)-.5, minor=True)
    ax.set_yticks(np.arange(data.shape[0] 1)-.5, minor=True)
    ax.grid(which='minor', color='w', linestyle='-', linewidth=3)
    ax.tick_params(which='minor', bottom=False, left=False)

# Return the heat map:
    return im, cbar

import numpy as np
import matplotlib.pyplot as plt
data = np.array([
    [30, 20, 10],
    [10, 40, 15],
    [12, 10, 20],
])
im, cbar = heatmap(data, ['Class-1','Class-2','Class-3'], ['A','B','C'],
                   cmap='YlGn', cbarlabel='Number of Students')
plt.show()

CodePudding user response:

data.shape is the sns.heatmap

  • Related