import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
# Define the matrix
matrix = np.array([
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 1, 1, 0, 1],
[0, 0, 0, 0, 0, 2, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 2, 0],
[0, 0, 0, 0, 0, 0, 0, 1],
[0, 1, 0, 1, 2, 0, 0, 0],
[1, 2, 0, 3, 1, 0, 0, 2],
[0, 0, 0, 4, 6, 3, 0, 2],
[0, 1, 5, 13, 7, 4, 1, 0],
[0, 1, 4, 9, 30, 11, 2, 0],
[0, 5, 1, 44, 15, 30, 3, 0],
[0, 0, 18, 37, 13, 5, 1, 0]
])
# Create the heatmap
plt.figure(figsize=(10, 8))
sns.heatmap(matrix, annot=True, fmt="d", cmap="YlGnBu", cbar=True)
# Display the heatmap
plt.title('Heatmap of the Matrix')
plt.show()
# Create a custom colormap that directly transitions from blue to red without
# white
blue_red_cmap = mcolors.LinearSegmentedColormap.from_list('BlueRed', ['blue','white', 'red'])
# Create a custom scale for the x-axis
x_labels = [-50, -35, -20, -5, 5, 20, 35, 50] # Assuming symmetric scale around 0
y_labels = np.linspace(196, 0, matrix.shape[0])
# Plot the heatmap with the custom blue-red colormap and specified adjustments
plt.figure(figsize=(10, 10)) # Ensuring equal width and height
plt.imshow(matrix, cmap=blue_red_cmap, aspect='equal', vmin=0, vmax=np.max(matrix))
# Set the custom ticks and labels
plt.xticks(ticks=np.arange(len(x_labels)), labels=x_labels)
plt.yticks(ticks=np.arange(len(y_labels)), labels=y_labels)
# Adding the values inside the cells
for i in range(matrix.shape[0]):
for j in range(matrix.shape[1]):
plt.text(j, i, f'{matrix[i, j]:.1f}', ha='center', va='center', color='white')
plt.colorbar(label='CFU/(100cm²)')
plt.xlabel('Distance (cm)')
plt.ylabel('Distance (cm)')
plt.show()
Click Run or press shift + ENTER to run code