0

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?

enter image description here

2 Answers2

1

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')

enter image description here

Sheldore
  • 37,862
  • 7
  • 57
  • 71
  • thank you so much. But how about without creating a new figure handler (` fig, ax = plt.subplots()`? Is there any way to set the border color to black with `plt`? –  Jul 05 '19 at 15:53
1

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"})
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Concerning your (now deleted) comment about the legends, I would reading [this](https://stackoverflow.com/questions/37864735/matplotlib-and-ipython-notebook-displaying-exactly-the-figure-that-will-be-save) and [this](https://stackoverflow.com/a/43439132/4124317) – ImportanceOfBeingErnest Jul 05 '19 at 16:24
  • I sorted it out and that's why I deleted it but thank you! –  Jul 05 '19 at 18:11