Matplotlib figure has spines/ticks: how to remove?

Hi DQ community

I’m playing with the World Happiness Index 2015 dataset in a Jupyter Notebook and creating a plot with multiple subplots where each subplot is a scatter plot of Generosity vs Economy per region:

import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
data = pd.read_csv('World_happiness_2015.csv')
regions = data['Region'].unique()

fig, ax = plt.subplots(figsize=(30,12))
for sp in range(0,10):
    region_data = data[data['Region'] == regions[sp]]
    eco = region_data['Economy (GDP per Capita)']
    gen = region_data['Generosity']
    ax = fig.add_subplot(2,5,sp+1)
    ax.scatter(x=eco, y=gen)
    ax.set_title(regions[sp])
    ax.set_xlim(0, 1.75)
    ax.set_ylim(0, 0.8)
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)

The plot that is generated is:

The problem I’m having is that the figure appears to have spines, ticks and tick labels in addition to those for each subplot (not sure if this is what is actually going on). Any help on how to remove these (I want to keep them for each subplot only) would be appreciated!

Kevin

Managed to figure it out after consulting the Python Data Science Handbook by replacing this line of code:

ax = fig.add_subplot(2,5,sp+1)

with

ax = plt.subplot(2,5,sp+1)

Resulting plot:

1 Like