import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline

Read it in the data

df = pd.read_csv("../country-gdp-2014.csv")
df.head()
Country Continent GDP_per_capita life_expectancy Population
0 Afghanistan Asia 663 54.863 22856302
1 Albania Europe 4195 74.200 3071856
2 Algeria Africa 5098 68.963 30533827
3 Angola Africa 2446 45.234 13926373
4 Antigua and Barbuda N. America 12738 73.544 77656

Change the background color

You can change the background color with ax.set_facecolor, but it will only change the area inside of the plot. This is useful when you have multiple plots in the same figure (a.k.a. multiple charts in the same image) but most of the time is just a headache.

ax = df.plot(kind='scatter', x='GDP_per_capita', y='life_expectancy', color='white', alpha=0.5, linewidth=0)
ax.set_ylim((0, 85))
ax.set_xlim((0, 70000))
# Specify background color for the axis/plot
ax.set_facecolor("lightslategray")

png

Actually, really change all of the background color

In order to change the background color of everything, you need to create a figure all by itself and set the facecolor. On top of that, you need to specify the facecolor when you save, or else it will show up as white/transparent in Adobe Illustrator even though it looks right in the Python world.

# Specify facecolor when creating the figure
fig, ax = plt.subplots(facecolor='lightslategray')
df.plot(kind='scatter', x='GDP_per_capita', y='life_expectancy', alpha=0.5, color='white', linewidth=0, ax=ax)
ax.set_ylim((0, 85))
ax.set_xlim((0, 70000))
# Specify background color for the axis/plot
ax.set_facecolor("lightslategray")

# You also need to specify facecolor on export it won't look right in Illustrator
fig.savefig("output.pdf", facecolor=fig.get_facecolor())

png