从 https://www.cnblogs.com/nju2014/p/5633768.html 中习得:
使用
plt.xticks([0, 1, 2, 3, 5, 4, 6, 7, 8, 9, 10, 11])时,并不会将4,5 的刻度正常标注。
解决办法如下:
plt.xticks([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
[r'$0$', r'$1$', r'$2$', r'$3$', r'$5$', r'$4$', r'$6$', r'$7$', r'$8$', r'$9$', r'$10$', r'$11$'])
将坐标刻度0,1,2,3,4,5,6,7,8,9,10,11,标注为0,1,2,3,5,4,6,7,8,9,10,11
matplotlib.colors.Normalize控制colormap对应的数值
绘制一个11*11的邻接矩阵图
def drawMat(adj_mat):
norm = colors.Normalize(vmin=-1, vmax=1)
cmp = plt.cm.bwr # coolwarm
cbar = plt.matshow(adj_mat, cmap=cmp) # Greens
cbar.set_norm(norm)
plt.xticks([])
plt.yticks([])
plt.xticks([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
[r'$0$', r'$1$', r'$2$', r'$3$', r'$5$', r'$4$', r'$6$', r'$7$', r'$8$', r'$9$', r'$10$', r'$11$'])
plt.yticks([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
[r'$0$', r'$1$', r'$2$', r'$3$', r'$5$', r'$4$', r'$6$', r'$7$', r'$8$', r'$9$', r'$10$', r'$11$'])
# Add colorbar, make sure to specify tick locations to match desired ticklabels
cbar = plt.colorbar(cbar, ticks=[0, 1])
cbar.ax.set_yticklabels(['0', '1']) # vertically oriented colorbar
def rect(pos):
r = plt.Rectangle(pos - 0.5, 1, 1, facecolor="none", edgecolor="w", linewidth=0.5)
plt.gca().add_patch(r)
x, y = np.meshgrid(np.arange(adj_mat.shape[1]), np.arange(adj_mat.shape[1]))
m = np.c_[x[adj_mat.astype(bool)], y[adj_mat.astype(bool)]]
m_ = np.c_[x[~adj_mat.astype(bool)], y[~adj_mat.astype(bool)]]
for pos in m:
rect(pos)
for pos in m_:
rect(pos)
plt.show()