6

I'm plotting some swarmplots, and would like to remove the gridlines crossing the data but keep the major axis lines.

I used to accomplish this by using sns.despine(), but I recently updated matplotlib and it appears somewhere along the line this no longer works. Most solutions I have seen remove BOTH major axes and gridlines.

For instance, one question recommended:

ax.grid(False)

which removes both gridlines and major axis lines.

A second solution suggested setting spine colors explicitly:

for spine in ['left','right','top','bottom']:
    ax1.spines[spine].set_color('k')

This does not work for me.

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns

%matplotlib inline

df = pd.read_csv('fig3full_tidy.csv', comment = '#')
df.head()

plt.style.use('dark_background')
fig, ax = plt.subplots()

# ax.grid(False)

barwidth = 0.2
temps = [15, 20, 25]
types = ['sc', 'sk', 'direct']
i = 1
for g in types:
    for t in temps:
        ys = df.loc[(df['Temperature'] == t) & (df['Genotype'] == g)].Fluorescence
        xs = np.random.normal(i, 0.05, len(ys))
        if g == 'sc':
            c = '#f08073'
        elif g == 'sk':
            c= '#2b8cbe'
        elif g == 'direct':
            c = '#23ba23'
        plt.plot(xs, ys, 'o', color= c, alpha= .7, zorder = 1)
        plt.hlines(ys.mean(), color = 'white', xmin = i- barwidth, xmax = i+ barwidth, lw = 1, zorder = 2)
        plt.errorbar(i, ys.mean(), yerr = ys.sem(), fmt = '-', ecolor = 'white', capsize = 0, elinewidth  = 1, barsabove = True, zorder = 2)
        i += 1
# plt.axvline(3.5, color = 'white', lw = 1)        
labels= ['', '15', '20', '25', '15', '20', '25', '15', '20', '25']
plt.xlabel('Temperature ($^\circ$C)', fontsize = 14, fontname = 'Arial')  
plt.xticks([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], labels, fontsize = 14)
sns.despine()


plt.ylabel('Fluorescence', fontsize = 14, fontname = 'Arial')
plt.yticks([0, 10000, 20000, 30000, 40000])

# plt.savefig('fig3.2_dark.pdf', bbox_inches='tight')
plt.show()
plt.close()

Desired results [Axis lines only]: enter image description here

With sns.despine not working, I have lots of gridlines [Too many]:

enter image description here

If I set ax.grid(False) I get too few [No lines] :

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Jon
  • 61
  • 1
  • 2

0 Answers0