Matplotlib:如何删除网格线但保留主轴?

时间:2019-02-18 23:21:45

标签: python matplotlib

我正在绘制一些变形图,并希望删除与数据交叉的网格线,但保留主轴线。

我曾经使用sns.despine()完成此操作,但是我最近更新了matplotlib,它出现在行中不再有效的位置。我见过的大多数解决方案都删除了主轴和网格线。

例如,one question建议:

ax.grid(False)

这将同时删除网格线和主轴线。

second solution建议显式设置书脊颜色:

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

这对我不起作用。

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()

所需结果[仅适用于轴线]: enter image description here

在sns.despine无法正常工作的情况下,我有很多网格线[太多]:

enter image description here

如果我设置ax.grid(False),我得到的[行数]太少了:

enter image description here

0 个答案:

没有答案