Install these with pip install before importing for plotting purposes below.
import numpy as np
import pandas as pd
import os.path
import matplotlib.pyplot as plt
plt.rcParams['pdf.fonttype'] = 42 #embed fonts into pdf figure
import seaborn as sns
path = os.path.join('/home','genewalk','qki') #separate (sub)folders with a comma
filename = 'genewalk_results.csv'
GW = pd.read_csv(os.path.join(path,filename))
stat = 'gene_padj'
GW['mlog10padj'] = -np.log10(GW[stat]) #calculate -log10 values
GW['mlog10padj_err'] = -np.log10(GW['cilow_'+stat]) - GW['mlog10padj'] #calculate -log10 error bars
alpha_FDR_plot = 0.05
GENES = ['MAL','PLP1']
for gene in GENES:
dplot = GW[GW['hgnc_symbol']==gene]
dplot = dplot.sort_values(by=[stat,'sim'],ascending=[True,False])
sns.set(style="whitegrid", color_codes=True)
f, ax = plt.subplots(figsize=(2,0.25*len(dplot)))#units: inch
ax = sns.barplot(x='mlog10padj',
y="go_name",
xerr=dplot['mlog10padj_err'],
data=dplot,
color="b",
error_kw=dict(elinewidth=1,ecolor=[0.7,0.7,0.7],capsize=1))
plt.axvline(x=-np.log10(alpha_FDR_plot), color='r', linestyle='--')
font_sz = 12
plt.xticks(range(4), size=font_sz)
plt.xlim(0, max(dplot['mlog10padj']+dplot['mlog10padj_err'])+0.3)
plt.xlabel('-log10('+stat+')',size=font_sz)
ax.yaxis.set_label_position('right')
plt.ylabel('GO annotations', size=0.8*font_sz,
rotation=270, labelpad=15)
plt.title(gene)
filename = 'barplot_'+gene+'_x_mlog10'+stat+'_y_GO.'
plt.savefig(os.path.join(path,filename+'pdf'),bbox_inches="tight",transparent=True)
plt.savefig(os.path.join(path,filename+'png'),bbox_inches="tight",transparent=True)