matplotlib 绘图坐标刻度标注

从 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()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值