mdave1701
mdave1701

Reputation: 37

How can axes be despined in Matplotlib?

I am trying to make the below grid of plots a little bit cleaner. I don't want the tick marks on the left side and the bottom to overlap. I have tried to despine the axes by trying the below code, but it doesn't seem to work. Anyone have any suggestions?

fig, ax = plt.subplots(figsize=(15,10))
cols = ['x6', 'x7', 'x16', 'x17']
subset = df[cols]
normed_df = (subset-subset.min())/(subset.max()-subset.min())
style.use('seaborn-darkgrid')
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)
for sp in range(4):
    ax = fig.add_subplot(2,2, sp+1)
    ax.hist(normed_df[cols[sp]], density=True)
    normed_df[cols[sp]].plot.kde(ax=ax)
ax.tick_params(bottom="off", top="off", left="off", right="off")

After running the above code, I am getting the following plots, however, the ticks are still overlapping.enter image description here

Upvotes: 0

Views: 365

Answers (2)

Derek Eden
Derek Eden

Reputation: 4618

either do what @Arne suggested:

fig, ax = plt.subplots(rows, cols) #makes a grid of subplots

or make your first two lines this:

fig, ax = plt.subplots(figsize=(15,10))
ax.axis('off')

this will remove the axis around the entire subplot before adding your additional subplots

Upvotes: 1

Arne
Arne

Reputation: 10545

When you call plt.subplots() without specifying a grid, it creates those axes across the whole figure whose tick marks and labels interfere with your subplot tick labels in the final plot. So change your first line of code to this:

fig, ax = plt.subplots(2, 2, figsize=(15,10))

Upvotes: 1

Related Questions