简体   繁体   English

python中的matshow,其范围在方格的中间打勾

[英]matshow in python with extents that make ticks in the middle of squares

The following code generates a matrix plot that every square of it indexed in the middle with a number from 1 to 39: 以下代码生成一个矩阵图,该矩阵图的每个正方形在中间用1到39的数字索引:

import numpy as np
from matplotlib import pyplot as plt
a=np.random.uniform(0,1,1600).reshape((40,40))
fig, ax = plt.subplots(1,1)
ax.matshow(a, vmin = 0, vmax = 1, interpolation = 'none')
label_list=np.arange(0,40,5)
label_list=np.append(label_list,39)
ax.set_xticks(label_list)
ax.set_yticks(label_list)
plt.show()

在此处输入图片说明

When I want to change the numbers to be between 0 and 1.95 or basically [0,39]*0.05 the labels shrink to the beginning of axes. 当我想将数字更改为介于01.95之间或基本上是[0,39]*0.05 ,标签会缩小到轴的起点。 If I try to use extent in matshow then the labels don't point to the middle of squares! 如果我尝试在matshow使用范围,则标签不会指向正方形的中间! How can I make this float indices to point to the middle of squares? 我怎样才能使这个浮动索引指向正方形的中间?

import numpy as np
from matplotlib import pyplot as plt

a=np.random.uniform(0,1,1600).reshape((40,40))
fig, ax = plt.subplots(1,1)
ax.matshow(a, vmin = 0, vmax = 1, interpolation = 'none')
tick_list = np.append(np.arange(0,40,5), 39)
label_list=map(lambda x: str(0.05*x), tick_list)
ax.set_xticks(tick_list)
ax.set_xticklabels(label_list)
ax.set_yticks(tick_list)
ax.set_yticklabels(label_list)
plt.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM