Change default background color for matplotlib plots

前端 未结 2 436
陌清茗
陌清茗 2021-01-11 11:54

I am using ipython with matplotlib. Is it possible to configure the default background color for matplotlib plots? The curent (white)

2条回答
  •  旧巷少年郎
    2021-01-11 12:39

    You can customise matplotlib in a variety of ways.

    If you're looking to customise across your entire computer then matplotlib uses the "matplotlibrc" configuration file as a default.

    If you wish to edit this to change the default axes facecolor (the technical term for the background) then you'll need to uncomment and adjust this line:

    #axes.facecolor : white # axes background color

    If you wish to set your background colour to #CCCCCC then you should change the line to:

    axes.facecolor : CCCCCC # axes background color

    N.B. if you re-install matplotlib this will be overwritten. To prevent this you can save it in "HOME/.matplotlib/matplotlibrc" as the example comments state.

    Should you wish to change it to a different colour temporarily then simply add the following at the top of your script:

    import matplotlib as mpl
    
    mpl.rcParams['axes.facecolor'] = '111111' # Or any suitable colour...
    

    If you should wish to modify an individual matplotlib.axes object then just use ax.set_axis_bgcolor('...').

提交回复
热议问题