Change default background color for matplotlib plots

前端 未结 2 440
陌清茗
陌清茗 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:40

    You need to set both the axes and figure background colors:

    f = plt.figure(facecolor=".6")
    ax = f.add_subplot(111, axisbg=".6")
    ax.plot([0, 1, 2], [1, 0, 2])
    

    enter image description here

    There is additionally a distinction between the facecolor for the interactive plot and what gets saved; you also have to pass facecolor to f.savefig if you want a uniform background on the resulting file.

    You can change the defaults with the following fields in the rcParams dictionary:

    import matplotlib as mpl
    mpl.rcParams["figure.facecolor"]
    mpl.rcParams["axes.facecolor"]
    mpl.rcParams["savefig.facecolor"]
    

    Note that this works a little unexpectedly in the IPython notebook with an inline backend, where the "saved" version of the figure you see below the cell is not controlled by the figure parameter, but by the savefig paramter.

提交回复
热议问题