调整Matplotlib imshow中的网格线和刻度线 [英] Adjusting gridlines and ticks in matplotlib imshow

查看:344
本文介绍了调整Matplotlib imshow中的网格线和刻度线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制值矩阵,并希望添加网格线以使值之间的边界更加清晰.不幸的是,imshow 决定在每个体素的中间定位刻度线.可以吗

I'm trying to plot a matrix of values and would like to add gridlines to make the boundary between values clearer. Unfortunately, imshow decided to locate the tick marks in the middle of each voxel. Is it possible to

a)删除刻度线,但将标签留在相同的位置,然后
b) 在像素边界之间添加网格线?

a) remove the ticks but leave the label in the same location and
b) add gridlines between the pixel boundaries?

import matplotlib.pyplot as plt
import numpy as np

im = plt.imshow(np.reshape(np.random.rand(100), newshape=(10,10)),
                    interpolation='none', vmin=0, vmax=1, aspect='equal');
ax = plt.gca();
ax.set_xticks(np.arange(0, 10, 1));
ax.set_yticks(np.arange(0, 10, 1));
ax.set_xticklabels(np.arange(1, 11, 1));
ax.set_yticklabels(np.arange(1, 11, 1));

没有网格线并且在正确的位置带有刻度线的图像

Image without the gridline and with tick marks in the wrong location

ax.grid(color='w', linestyle='-', linewidth=2)

网格线位置错误的图像:

Image with gridlines in the wrong location:

推荐答案

尝试移动坐标轴刻度:

ax = plt.gca()
ax.set_xticks(np.arange(-.5, 10, 1))
ax.set_yticks(np.arange(-.5, 10, 1))
ax.set_xticklabels(np.arange(1, 12, 1))
ax.set_yticklabels(np.arange(1, 12, 1))

这篇关于调整Matplotlib imshow中的网格线和刻度线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆