我無法弄清楚這段代碼是做什么的,因為如果我洗掉它,熱圖中沒有任何變化,而且我不知道“data.shape[1] 1)-.5”和“.shape”是什么[]”一般都在做
# 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)
我無法理解這段代碼
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()
uj5u.com熱心網友回復:
data.shape是陣列的![為什么 np.arange(data.shape[1] 1)-.5 用于繪制熱圖?](https://img.uj5u.com/2021/12/20/34b170be07a2436a8e1e8f3764021ec3.png)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/386439.html
標籤:Python 麻木的 matplotlib
上一篇:如何從位掩碼繪制多邊形補丁
