'Plot 2d array using matplotlib based on fixed value-based color regions

I'd like to plot a 2d array using matplotlib based on fixed value-based color regions.

For example, fixed color regions are 0 < value < 5 = red, 5 < value < 30 = blue, Above 30 = white

My example array is[[ 40 35 50] [ 60 70 80]]

If I correctly code, the plot should be all white. However, the values of color regions are always relative, not fixed. How can I fix the color region values so that the values are colored based on the fixed color regions?

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
import seaborn as sns

my_colors = ['#FF0000', '#1124D8', '#FFFFFF']
my_cmap = ListedColormap(sns.color_palette(my_colors).as_hex())

exmaple = np.array((40, 35, 50, 60, 70, 80))
exmaple = exmaple.reshape(2,3)

plt.contourf(exmaple, cmap=my_cmap, levels=3)
plt.colorbar() 

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source