Python 在 matplotlib imshow 中调整网格线和刻度线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38973868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:42:35  来源:igfitidea点击:

Adjusting gridlines and ticks in matplotlib imshow

pythonmatplotlibimshow

提问by Joe Bathelt

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

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

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

a) 删除刻度但将标签保留在同一位置和
b) 在像素边界之间添加网格线?

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 enter image description here

没有网格线且在错误位置有刻度线的图像 在此处输入图片说明

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

Image with gridlines in the wrong location:

网格线位置错误的图像:

enter image description here

在此处输入图片说明

采纳答案by Serenity

Try to shift axes ticks:

尝试移动轴刻度:

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

enter image description here

在此处输入图片说明

回答by Joe Bathelt

Code for solution as suggested by Serenity:

Serenity 建议的解决方案代码:

plt.figure()
im = plt.imshow(np.reshape(np.random.rand(100), newshape=(10,10)),
                interpolation='none', vmin=0, vmax=1, aspect='equal');

ax = plt.gca();

# Major ticks
ax.set_xticks(np.arange(0, 10, 1));
ax.set_yticks(np.arange(0, 10, 1));

# Labels for major ticks
ax.set_xticklabels(np.arange(1, 11, 1));
ax.set_yticklabels(np.arange(1, 11, 1));

# Minor ticks
ax.set_xticks(np.arange(-.5, 10, 1), minor=True);
ax.set_yticks(np.arange(-.5, 10, 1), minor=True);

# Gridlines based on minor ticks
ax.grid(which='minor', color='w', linestyle='-', linewidth=2)

Resulting image: enter image description here

结果图像: 在此处输入图片说明

回答by Georgy

One can find it easier to use plt.pcoloror plt.pcolormesh:

人们可以发现它更易于使用plt.pcolorplt.pcolormesh

data = np.random.rand(10, 10)
plt.pcolormesh(data, edgecolors='k', linewidth=2)
ax = plt.gca()
ax.set_aspect('equal')

enter image description here

在此处输入图片说明

Though, there are some differences among them and plt.imshow, the most obvious being that the image is swapped by the Y-axis (you can reversed it back easily by adding ax.invert_yaxis()though). For further discussion see here: When to use imshow over pcolormesh?

但是,它们之间存在一些差异plt.imshow,最明显的是图像由 Y 轴交换(ax.invert_yaxis()尽管添加,您可以轻松地将其反转回来)。有关进一步讨论,请参见此处:何时在 pcolormesh 上使用 imshow?

回答by drammock

You can shift the pixels by passing the extentargument to imshow. extentis a 4-element list of scalars (left, right, bottom, top):

您可以通过将extent参数传递给 来移动像素imshowextent是标量的 4 元素列表(左、右、下、上):

foo = np.random.rand(35).reshape(5, 7)
# This keeps the default orientation (origin at top left):
extent = (0, foo.shape[1], foo.shape[0], 0)
_, ax = plt.subplots()
ax.imshow(foo, extent=extent)
ax.grid(color='w', linewidth=2)
ax.set_frame_on(False)

enter image description here

在此处输入图片说明