python绘制网格并在相应的坐标填充颜色
参考博客
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)):
#填充矩形 x:[横坐标] y1:[纵坐标] y2:[纵坐标] 填色范围为(x1,y2) (x2,y2) 到(x1,y1)(x2,y1)
ax.fill_between(x=[i - 1, i], y1=j, y2=j + 1, color="black")
#填充下三角
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()
效果:
常用颜色代码: