简体   繁体   中英

Changing the background color of plot on Jupyter using matplotlib

I am using jupyter notebook for simple plotting tasks as the following.

%matplotlib inline

plt.rcParams["figure.figsize"] = (12, 8)
plt.style.use("bmh")

I am getting a plot of the following form. How can remove the background silver color (we can keep the grid but I can also turn them off using plt. grid(False) ) to white and change the border color of the plot to black?

在此处输入图片说明

The "bmh" style sets the axes facecolor and edgecolor like

axes.facecolor: eeeeee
axes.edgecolor: bcbcbc

You can set them back after setting the style,

import matplotlib.pyplot as plt

plt.style.use("bmh")
plt.rcParams.update({"figure.figsize" : (12, 8),
                     "axes.facecolor" : "white",
                     "axes.edgecolor":  "black"})

I would recommend the following way. The following answer is based on this and this posts

%matplotlib inline

plt.rcParams["figure.figsize"] = (8, 6)
plt.style.use("bmh")

plt.hist(np.random.normal(0,1, 10000), bins=100)
plt.gca().set_facecolor("white")
plt.setp(ax.spines.values(), color='k') # Change the frame border to black ('k')

An alternative way using ax object could be

%matplotlib inline

plt.rcParams["figure.figsize"] = (8, 6)
plt.style.use("bmh")

fig, ax = plt.subplots()

ax.hist(np.random.normal(0,1, 10000), bins=100);
ax.set_facecolor("white")
plt.setp(ax.spines.values(), color='k')

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM