这是我尝试将条形图的图例从矩形更改为方形:
import matplotlib.patches as patches
rect1 = patches.Rectangle((0,0),1,1,facecolor='#FF605E')
rect2 = patches.Rectangle((0,0),1,1,facecolor='#64B2DF')
plt.legend((rect1, rect2), ('2016', '2015'))
但是当我绘制这个时,我仍然会看到矩形而不是正方形:
有关如何做到这一点的任何建议?
我尝试了@ImportanceOfBeingErnest和@furas提供的两种解决方案,结果如下:
@ ImportanceOfBeingErnest的解决方案是最容易做到的:
plt.rcParams['legend.handlelength'] = 1
plt.rcParams['legend.handleheight'] = 1.125
结果如下:
我的最终代码如下所示:
plt.legend((df.columns[1], df.columns[0]), handlelength=1, handleheight=1) # the df.columns = the legend text
@ furas的解决方案产生了这个,我不知道为什么文本远离矩形,但我确信