我正在努力处理matplotlib中的图边距。我使用下面的代码来生成我的图表:
plt.imshow(g)
c = plt.colorbar()
c.set_label("Number of Slabs")
plt.savefig("OutputToUse.png")
然而,我得到的输出数字在图的两边都有大量的空白。我已经搜索了谷歌并阅读了matplotlib文档,但我似乎找不到如何减少这一点。
我正在努力处理matplotlib中的图边距。我使用下面的代码来生成我的图表:
plt.imshow(g)
c = plt.colorbar()
c.set_label("Number of Slabs")
plt.savefig("OutputToUse.png")
然而,我得到的输出数字在图的两边都有大量的空白。我已经搜索了谷歌并阅读了matplotlib文档,但我似乎找不到如何减少这一点。
当前回答
你可以使用subplots_adjust()函数来调整matplotlib图的间距:
import matplotlib.pyplot as plt
plt.plot(whatever)
plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1)
这既适用于屏幕上的图形,也适用于保存到文件中的图形,即使在一个图形上没有多个图形,也可以调用这个函数。
这些数字是图形尺寸的分数,并且需要调整以允许图形标签。
其他回答
plt.savefig("circle.png", bbox_inches='tight',pad_inches=-1)
# import pyplot
import matplotlib.pyplot as plt
# your code to plot the figure
# set tight margins
plt.margins(0.015, tight=True)
只需使用ax = fig.add_axes([左,下,宽,高]) 如果你想要精确控制图形布局。如。
left = 0.05
bottom = 0.05
width = 0.9
height = 0.9
ax = fig.add_axes([left, bottom, width, height])
受到sammy回答的启发:
margins = { # vvv margin in inches
"left" : 1.5 / figsize[0],
"bottom" : 0.8 / figsize[1],
"right" : 1 - 0.3 / figsize[0],
"top" : 1 - 1 / figsize[1]
}
fig.subplots_adjust(**margins)
其中figsize是在fig = pyplot.figure(figsize=…)中使用的元组。
你可以使用subplots_adjust()函数来调整matplotlib图的间距:
import matplotlib.pyplot as plt
plt.plot(whatever)
plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1)
这既适用于屏幕上的图形,也适用于保存到文件中的图形,即使在一个图形上没有多个图形,也可以调用这个函数。
这些数字是图形尺寸的分数,并且需要调整以允许图形标签。