Matplotlib figure facecolor (background color)

前端 未结 4 686
無奈伤痛
無奈伤痛 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:44

    savefig has its own parameter for facecolor. I think an even easier way than the accepted answer is to set them globally just once, instead of putting facecolor=fig.get_facecolor() every time:

    plt.rcParams['axes.facecolor']='red'
    plt.rcParams['savefig.facecolor']='red'
    
    0 讨论(0)
  • 2020-11-27 13:45

    If you want to change background color, try this:

    plt.rcParams['figure.facecolor'] = 'white'
    
    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2020-11-27 14:00

    I had to use the transparent keyword to get the color I chose with my initial

    fig=figure(facecolor='black')
    

    like this:

    savefig('figname.png', facecolor=fig.get_facecolor(), transparent=True)
    
    0 讨论(0)
提交回复
热议问题