Enable white frames on matplotlib figure/ Disable transparent frames
Asked Answered
S

2

16

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

Shading answered 17/9, 2018 at 15:46 Comment(4)
@Yuca I don't think so, it's not changing the colors of axis, ticks/labels, I'm thinking of adding a white background frame so I don't have to change them - it'll be ugly if I just change the color of the ticks because the figure is white.Shading
Possible duplicate of https://mcmap.net/q/87091/-how-to-change-plot-background-color/9754169Czardas
sorry for the wrong link, this should be the correct one :) (you need to modify the figure's patch property)Czardas
@Czardas Thanks this solves it!Shading
E
17

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"}
Eras answered 17/9, 2018 at 21:51 Comment(1)
Thank you! I had tried setting matplotlibrc, it does not work. It turns out to be an IPython problem!Fpc
A
-1

To have a transparent frame just add the following line after loading matplotlib:

import matplotlib.pyplot as plt
plt.rcParams['axes.facecolor'] = 'none'

This did the trick for me!

Accouplement answered 16/4, 2021 at 14:2 Comment(1)
The question was about how to make the frame "not transparent"Indohittite

© 2022 - 2024 — McMap. All rights reserved.