
PYTHON — Creating Colored Matrix in Python
Digital design is like painting, except the paint never dries. — Neville Brody

PYTHON — Summary of Numpy arange in Python
# Creating a Colored Matrix in Python
One of the most visually appealing types of visualizations is a heatmap. Although matplotlib does not have a direct method for creating heatmaps, we can still generate a grid of colored squares from a two-dimensional ndarray. To get started, we need to import the make_axes_locatable function from mpl_toolkits.
from mpl_toolkits.axes_grid1.axes_divider import make_axes_locatableWe’ll create two separate two-dimensional ndarrays, similar to matrices, to demonstrate the process of creating a colored matrix.
import matplotlib.pyplot as plt
import numpy as np
# Create two 10x10 arrays
x = np.diag(np.arange(2, 12))[::-1]
x2 = np.arange(100).reshape(10, 10)
# Disable tick marks and numeric labels
nolabels = {i: j for i, j in zip('xy', ['top', 'right'])}
nolabels.update({i: False for i in ['top', 'bottom', 'left', 'right']})
# Plot the data
with plt.rc_context({'axes.grid': False}):
fig, (ax1, ax2) = plt.subplots(1, 2)
cax = ax1.matshow(x, cmap='viridis')
ax2.matshow(x2, cmap='RdYlGn_r')
for ax in [ax1, ax2]:
ax.tick_params(axis='both', which='both', **nolabels)
for i, j in zip(*x.nonzero()):
ax1.text(j, i, x[i, j], color='white', ha='center', va='center')
divider = make_axes_locatable(ax2)
cax = divider.append_axes("right", size="5%", pad=0.1)
plt.colorbar(cax=cax)
fig.suptitle('Heatmaps with Axes.matshow', fontsize=16)
plt.show()In this example, we first create two 10x10 arrays, x and x2, to represent the data that we want to visualize. We then use the matplotlib and numpy libraries to plot these arrays as color-coded matrices using the matshow function.
Additionally, we disable the tick marks and labels on the axes to create a cleaner visualization, and we add text labels to the diagonal color strips to provide more context to the visualization. Lastly, we include a color bar to indicate the color mapping of the data values.
The end result is a grid of colored squares representing the provided data, and the code provides a clear and detailed explanation of each step involved in creating the colored matrix.







