bbox_to_anchor and loc in matplotlib

后端 未结 2 638
梦谈多话
梦谈多话 2020-12-24 06:00

I came across matplotlib code which customizes legend location using keywords loc and bbox_to_anchor. For example :

fi         


        
2条回答
  •  情话喂你
    2020-12-24 06:18

    When bbox_to_anchor and loc are used together, the loc argument will inform matplotlib which part of the bounding box of the legend should be placed at the arguments of bbox_to_anchor. For example (I've simplified the command a bit), the three options below will produce different locations for your legend,

     fig.legend([line1], ['series1'], bbox_to_anchor=[0.5, 0.5], loc='center')
     fig.legend([line1], ['series1'], bbox_to_anchor=[0.5, 0.5], loc='center left')
     fig.legend([line1], ['series1'], bbox_to_anchor=[0.5, 0.5], loc='center right')
    

    The first command will put the center of the bounding box at axes coordinates 0.5,0.5. The second will put the center left edge of the bounding box at the same coordinates (i.e. shift the legend to the right). Finally, the third option will put the center right edge of the bounding box at the coordinates (i.e. shift the legend to the left).

提交回复
热议问题