在子图环境中使用时,matplotlib.pyplot.imshow的边距 [英] Margins of matplotlib.pyplot.imshow when used in subplot environment
问题描述
我正在尝试使用 imshow 在图形的不同图中绘制多个数组的值.
I am trying to plot the values of several arrays in separate plots of a figure using imshow.
当我只绘制一张图像,并使用具有正确范围的 plt.imshow() 命令时,该图完美呈现.但是,当我尝试使用plt.subplot()在同一幅图中创建该图像的多个图时,这些图中的每一个都以不正确的x轴设置结尾,并且有白色边距.我尝试使用 set_xlim() 命令更正 x 轴范围,但没有效果(我也不明白).
When I plot one image only, and use the plt.imshow() command with the correct extents, the figure comes out perfectly. However, when I try to create multiple plots of this image in the same figure, using plt.subplot(), each of these plots ends up with incorrect x-axis settings, and there are white margins. I tried correcting the x-axis range with the set_xlim() command, but it has no effect (which I also don't understand).
下面是最小的工作示例-我们将不胜感激!
The minimal working sample is below - any help would be appreciated!
from matplotlib import pyplot as plt
import numpy as n
image = n.array([[ 1., 2., 2., 5.],
[ 1., 0., 0., 3.],
[ 1., 2., 0., 2.],
[ 4., 2., 3., 2.]])
xextent, yextent= n.shape(image)
fig, ax = plt.subplots(2,sharex=True, sharey=True)
im0 = ax[0].imshow(image, extent=(0,xextent,yextent,0),interpolation='nearest');
ax[0].set_xlim([0,4])
im1 = ax[1].imshow(image, extent=(0,xextent,yextent,0),interpolation='nearest');
ax[1].set_xlim([0,4])
plt.show()
推荐答案
所以选项是:
-
删除
sharex
/sharey
关键字-似乎与子图环境中的imshow冲突.(由 xnx 推荐)
Remove the
sharex
/sharey
keywords - seems to clash with imshow in the subplot environment. (Suggested by xnx)
结合适当的设置使用 plt.subplots_adjust
和 plt.gcf().tight_layout()
组合(由mwormser )
Use plt.subplots_adjust
with appropriate settings, in combination with plt.gcf().tight_layout()
(Suggested by mwormser)
在子图环境中使用 pcolormesh 而不是 imshow.
Use pcolormesh instead of imshow in the subplot environment.
这篇关于在子图环境中使用时,matplotlib.pyplot.imshow的边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!