How to change the background color of df.plot() in Python Pandas?

Question:

I want to specify the color of the area surrounding a plot created using the df.plot() in Pandas/Python.

Using .set_facecolor as in the code below only changes the area inside the axes (see image), I want to change the color outside too.

import pandas as pd
import numpy as np

df = pd.DataFrame(components, columns=['PC1','PC2']
df.plot('PC1','PC2','scatter').set_facecolor('green')

Replacing the last line with these two lines produces the same graph.

ax = df.plot('PC1','PC2','scatter')
ax.set_facecolor('green')

enter image description here
setfacecolor example

Asked By: Spencer J Rothfuss

||

Answers:

IIUC, you can use fig.set_facecolor:

fig, ax = plt.subplots()
df.plot('PC1','PC2','scatter', ax=ax).set_facecolor('green')
fig.set_facecolor('green')
plt.show()

Output:

enter image description here

Answered By: Corralien