Search code examples
matplotlibjupyter-notebookfigureaxes

Enable white frames on matplotlib figure/ Disable transparent frames


I'm running ipython notebook on a dark theme. When I build a chart on this, the chart is white, but the frame is transparent (hence dark), hiding the ticks which are also dark. Is there a way to make the frame not transparent/ white?

The ticks are barely visible due to the black background.

The ticks are barely visible due to black background.

How do I solve this? Thanks!

Edit: This is not about changing the colors of axis, ticks/labels, I'm thinking of adding a white background frame, not changing the colors of ticks - it'll be ugly if I just change the color of the ticks because the figure is white


Solution

  • The figure shown in jupyter with the %matplotlib inline backend (which is often the default) is created via saving it through savefig to a png that is then displayed. savefig has an argument facecolor which sets the color of the figure background. This can be set to white, e.g. fig.savefig("name.png", facecolor="w").

    The options for saving can be adapted in the jupyter configuration. To achieve a white background one can set

    %config InlineBackend.print_figure_kwargs={'facecolor' : "w"}
    

    in a cell prior to showing the plot.

    If that is to be used for every notebook, it can also be added to the ipython configuration file

    c = get_config()
    c.InlineBackend.print_figure_kwargs={'facecolor' : "w"}