pandas 如何在python中使用matplotlib创建曼哈顿图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37463184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:17:46  来源:igfitidea点击:

How to create a Manhattan plot with matplotlib in python?

pythonpandasmatplotlibplotgenetics

提问by Thomas M?bius

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

不幸的是,我自己还没有找到解决方案。如何使用例如 matplotlib/pandas 在 python 中创建曼哈顿图。问题是在这些图中,x 轴是离散的。

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?

采纳答案by Elad Joseph

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.

我刚刚创建了一个额外的运行索引列来控制 x 标签位置。

enter image description here

在此处输入图片说明

回答by Jan Christoph Terasa

import matplotlib.pyplot als plt
from numpy.random import randn, random_sample

g = random_sample(int(1e5))*10 # uniform random values between 0 and 10
p = abs(randn(int(1e5))) # abs of normally distributed data

"""
plot g vs p in groups with different colors
colors are cycled automatically by matplotlib
use another colormap or define own colors for a different cycle
"""
for i in range(1,11): 
    plt.plot(g[abs(g-i)<1], p[abs(g-i)<1], ls='', marker='.')

plt.show()

Example of a manhattan style plot

曼哈顿风格的情节示例

You can also check out this script, which seems to offer a finished solution to your problem.

您还可以查看此脚本,它似乎为您的问题提供了完整的解决方案。