Draw grid/mosaic diagrams in Python

Preface

Draw a grid in Python and fill in colors at the corresponding coordinates
Reference blog.


def mplot_intf(t, data):
plt.rcParams["figure.figsize"] = (t, len(data))
plt.rcParams["xtick.major.size"] = 0
plt.rcParams["ytick.major.size"] = 0
plt.rcParams["xtick.minor.size"] = 0
plt.rcParams["ytick.minor.size"] = 0
fig, ax = plt.subplots()
ax.set_xlim([0, t])
ax.set_ylim([0, len(data)])
ax.set_xticks(range(t + 1))
ax.set_yticks(range(len(data) + 1))
ax.grid(which="both", color="grey", linewidth=1)
for i in range(t):
  for j in range(len(data)):
  	 #fill rectangle x:[ abscissa ] y1:[ ordinate ] y2:[ ordinate ] the color range is( x1,y2) (x2,y 2) to( x1,y1)(x2,y1)
       ax.fill_between(x=[i - 1, i], y1=j, y2=j + 1, color="black")  
       #fill lower triangle 
       ax.fill_between(x=[i - 1, i], y1=[j, j+1], y2=[j, j], color="green")  
plt.xlabel('Time Slot')
plt.ylabel('data')
plt.show()

Effect:

Common color codes:

Related articles