Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用matplotlib绘制地物时删除空格_Python_Matplotlib_Tkinter_Canvas_Plot - Fatal编程技术网

Python 使用matplotlib绘制地物时删除空格

Python 使用matplotlib绘制地物时删除空格,python,matplotlib,tkinter,canvas,plot,Python,Matplotlib,Tkinter,Canvas,Plot,这让我快发疯了 在python中使用matplotlib, 我正在绘制一个简单的绘图(20个不同颜色的像素),如下所示: fig1, ax = plt.subplots() color_rgb_list = np.random.rand(n_colors, 3) ax.imshow(color_rgb_list[None, :], aspect='equal') for label in train_y: ax.text(label - 1.5, 1, str(label)) plt.t

这让我快发疯了

在python中使用matplotlib, 我正在绘制一个简单的绘图(20个不同颜色的像素),如下所示:

fig1, ax = plt.subplots()
color_rgb_list = np.random.rand(n_colors, 3)
ax.imshow(color_rgb_list[None, :], aspect='equal')
for label in train_y:
    ax.text(label - 1.5, 1, str(label))
plt.tight_layout()
plt.show()
# fig1.savefig("test.png", bbox_inches='tight')
然而,我得到的结果周围有很多空白。我发现在实际将图形保存到文件时可以使用
bbox\u inches='tight'
,但这对我没有帮助,因为我希望在绘制实际图像时删除空白。 (我将此应用程序用于一个应用程序,其中我希望使用
.pack()
tkinter在一个窗口中显示多个不同的绘图)

绘制/显示绘图时如何删除空白? 我基本上希望实现与
bbox\u inches='tight'
相同的功能,仅当使用
plt.show()
或使用tkinter在画布上绘制时


提前感谢。

由于绘制的是不规则大小的绘图,因此可以指定一个将相应地拉伸窗口的
figsize
**kwarg


fig1, ax = plt.subplots(figsize=(10, 2)) # figsize = (width, height)
color_rgb_list = np.random.rand(n_colors, 3)
ax.imshow(color_rgb_list[None, :], aspect='equal')
for label in train_y:
    ax.text(label - 1.5, 1, str(label))
plt.tight_layout()
plt.show()


看看玩它是否有帮助

我已经玩过了,不幸的是没有成功。。。但是谢谢。还有其他想法吗?我真的让它工作了,不知道为什么它以前不工作,非常感谢,我觉得有点像个白痴。再次感谢!