Matplotlib figure facecolor (background color)

前端 未结 4 701
無奈伤痛
無奈伤痛 2020-11-27 13:05

Can someone please explain why the code below does not work when setting the facecolor of the figure?

import matplotlib.pyplot as plt

# create figure instan         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 13:52

    It's because savefig overrides the facecolor for the background of the figure.

    (This is deliberate, actually... The assumption is that you'd probably want to control the background color of the saved figure with the facecolor kwarg to savefig. It's a confusing and inconsistent default, though!)

    The easiest workaround is just to do fig.savefig('whatever.png', facecolor=fig.get_facecolor(), edgecolor='none') (I'm specifying the edgecolor here because the default edgecolor for the actual figure is white, which will give you a white border around the saved figure)

    Hope that helps!

提交回复
热议问题