因为设置了plt.tight_layout()
,这会让子图挤在一起,需要先设置legend,再用plt.tight_layout()
https://www.cnpython.com/qa/277015
if len(a)>0:
a.plot(ax=ax, legend=False, color=colors)
# 腾出放图例的空间
ncol = 3; rcol = math.ceil(len(k)/ncol)
# 如果图例太长了(rcol>9),那么将其放置到该行子图的最右边空白处
if rcol < 9:
y_max = a.max().max()*(1+0.11*rcol)
ax.set_ylim(0, y_max)
plt.legend(ncol=ncol, loc='upper center', )
elif n == 2:
ax2 = ax
ax.set_title(col+title_add)
plt.tight_layout()
ax2.legend(ncol=3, loc=2, bbox_to_anchor=(1.05,1.0)) # 图例放右边 # 增加标签...说明哪些指标在用该图例...