python中的matshow,带有在正方形中间打勾的范围

时间:2015-04-03 20:19:50

标签: python matplotlib

以下代码生成一个矩阵图,其中每个方格在中间用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()

enter image description here

如果我想将数字更改为01.95之间或基本上[0,39]*0.05,则标签会缩小到轴的开头。如果我尝试在matshow中使用范围,那么标签就不会指向正方形的中间位置!如何使这个浮点索引指向正方形的中间?

1 个答案:

答案 0 :(得分:2)

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