如何在python中使用matplotlib创建曼哈顿图? [英] How to create a Manhattan plot with matplotlib in python?

查看:63
本文介绍了如何在python中使用matplotlib创建曼哈顿图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不幸的是,我自己还没有找到解决方案.如何使用例如 matplotlib/pandas 在 python 中创建

Unfortunately, I have not found a solution myself. How do I create a Manhattan plot within python using, e.g., matplotlib / pandas. The problem is that in these plots the x-axis is discrete.

from pandas import DataFrame
from scipy.stats import uniform
from scipy.stats import randint
import numpy as np

# some sample data
df = DataFrame({'gene' : ['gene-%i' % i for i in np.arange(1000)],
'pvalue' : uniform.rvs(size=1000),
'chromosome' : ['ch-%i' % i for i in randint.rvs(0,12,size=1000)]})

# -log_10(pvalue)
df['minuslog10pvalue'] = -np.log10(df.pvalue)
df = df.sort_values('chromosome')

# How to plot gene vs. -log10(pvalue) and colour it by chromosome?

解决方案

You can use something like this:

from pandas import DataFrame
from scipy.stats import uniform
from scipy.stats import randint
import numpy as np
import matplotlib.pyplot as plt

# some sample data
df = DataFrame({'gene' : ['gene-%i' % i for i in np.arange(10000)],
'pvalue' : uniform.rvs(size=10000),
'chromosome' : ['ch-%i' % i for i in randint.rvs(0,12,size=10000)]})

# -log_10(pvalue)
df['minuslog10pvalue'] = -np.log10(df.pvalue)
df.chromosome = df.chromosome.astype('category')
df.chromosome = df.chromosome.cat.set_categories(['ch-%i' % i for i in range(12)], ordered=True)
df = df.sort_values('chromosome')

# How to plot gene vs. -log10(pvalue) and colour it by chromosome?
df['ind'] = range(len(df))
df_grouped = df.groupby(('chromosome'))

fig = plt.figure()
ax = fig.add_subplot(111)
colors = ['red','green','blue', 'yellow']
x_labels = []
x_labels_pos = []
for num, (name, group) in enumerate(df_grouped):
    group.plot(kind='scatter', x='ind', y='minuslog10pvalue',color=colors[num % len(colors)], ax=ax)
    x_labels.append(name)
    x_labels_pos.append((group['ind'].iloc[-1] - (group['ind'].iloc[-1] - group['ind'].iloc[0])/2))
ax.set_xticks(x_labels_pos)
ax.set_xticklabels(x_labels)
ax.set_xlim([0, len(df)])
ax.set_ylim([0, 3.5])
ax.set_xlabel('Chromosome')

I just created an extra column of running index to have control on the x labels locations.

这篇关于如何在python中使用matplotlib创建曼哈顿图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆