- Matplotlib 3.0 Cookbook
- Srinivasa Rao Poladi
- 111字
- 2021-08-13 15:16:00
How to do it...
The following code block defines the function and makes a call to the function to plot the Hinton diagram:
- Read the weight matrix data from an Excel file:
matrix = np.asarray((pd.read_excel('weight_matrix.xlsx')))
- Instantiate the figure and axes:
fig, ax = plt.subplots()
- Set up the parameters for the axes:
ax.patch.set_facecolor('gray')
ax.set_aspect('equal', 'box')
ax.xaxis.set_major_locator(plt.NullLocator())
ax.yaxis.set_major_locator(plt.NullLocator())
- Plot the Hinton diagram:
max_weight = 2 ** np.ceil(np.log(np.abs(matrix).max()) / np.log(2))
for (x, y), w in np.ndenumerate(matrix):
color = 'white' if w > 0 else 'black'
size = np.sqrt(np.abs(w) / max_weight)
rect = plt.Rectangle([x - size / 2, y - size / 2], size, size, facecolor=color, edgecolor=color)
ax.add_patch(rect)
ax.autoscale_view()
- Display it on the screen:
plt.show()