3
我正在用matplotlib繪製幾十個子圖。在圖的底部,在最後一排地塊和圖例之間,出現空白區域。當我添加更多子圖時,空白區域變大。任何想法如何擺脫這個空虛的空間?matplotlib:繪製多個子圖時出現在圖上的空白區域
這裏的工作代碼:
import textwrap
import matplotlib.pyplot as plt
from collections import OrderedDict
rlen = 31 # number of plots
figsize = (11, 3) # matrix of subplots
fig = plt.figure(figsize=(figsize[1]*4, figsize[0]*4))
plots = []
for f_ind in range(rlen):
plots.append(fig.add_subplot(figsize[0], figsize[1], f_ind))
fig.subplots_adjust(wspace=0.5, hspace=0.5)
for ax in plots:
atitle = 'Aaa bbb ccc ' * 10
ax.set_title('\n'.join(textwrap.wrap(atitle, 45)), fontsize=10)
ax.plot(range(10), range(10), 'o', color='red', label='LABEL_1')
revlist = list(reversed(range(10)))
ax.plot(revlist, range(10), 'o', color='blue', label='LABEL_2')
ax.set_xlabel('Train set size', fontsize=9)
ax.set_ylabel('Accuracy (%)', fontsize=9)
handles, labels = plt.gca().get_legend_handles_labels()
by_label = OrderedDict(zip(labels, handles))
lgd = fig.legend(by_label.values(), by_label.keys(), loc='lower center', ncol=4)
fig.savefig('ZZZ.png', dpi=fig.dpi, bbox_extra_artists=(lgd,), bbox_inches='tight')
您還可以查看示例結果here。謝謝!
這很完美,謝謝!最適合底部= 1./rlen –