我正在绘制一些群图,并且想删除与数据交叉的网格线,但保留主轴线。
我曾经通过使用 sns.despine() 来完成此操作,但我最近更新了 matplotlib,它出现在某处,不再起作用。我见过的大多数解决方案都删除了主轴和网格线。
例如,推荐一个问题:
ax.grid(False)
Run Code Online (Sandbox Code Playgroud)
这会删除网格线和主轴线。
第二种解决方案建议明确设置书脊颜色:
for spine in ['left','right','top','bottom']:
ax1.spines[spine].set_color('k')
Run Code Online (Sandbox Code Playgroud)
这对我不起作用。
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()
Run Code Online (Sandbox Code Playgroud)
由于 sns.despine 不起作用,我有很多网格线[太多]:
如果我设置 ax.grid(False) 我得到的 [Nolines] 太少:
归档时间: |
|
查看次数: |
3822 次 |
最近记录: |