0

I am trying to create a Manhattan plot that will be vertically highlighted at certain parts of the plot given a list of values corresponding to points in the scatter plot. I looked at several examples but I am not sure how to proceed. I think using axvspan or ax.fill_between should work but I am not sure how. The code below was lifted directly from How to create a Manhattan plot with matplotlib in python?

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

given a list of values of the point, pvalues e.g

lst = [0.288686, 0.242591, 0.095959, 3.291343, 1.526353]

How do I highlight the region containing these points on the plot just as shown in green in the image below? Something similar to:

Text](https://stackoverflow.com/image.jpg)[![enter image description here]1

jackpetes
  • 15
  • 1
  • 4

1 Answers1

0

It would help if you have a sample of your dataframe for your reference.

Assuming you want to match your lst values with Y values, you need to iterate through each Y value you're plotting and check if they are within lst.

for num, (name, group) in enumerate(df_grouped):

group Variable in your code are essentially partial dataframes of your main dataframe, df. Hence, you need to put in another loop to look through all Y values for lst matches

region_plot = []
for num, (name, group) in enumerate(a.groupby('group')):
    group.plot(kind='scatter', x='ind', y='minuslog10pvalue',color=colors[num % len(colors)], ax=ax)
    #create a new df to get only rows that have matched values with lst
    temp_group = group[group['minuslog10pvalue'].isin(lst)] 
    for x_group in temp_group['ind']:
        #If condition to make sure same region is not highlighted again
        if x_group not in region_plot:
            region_plot.append(x_group)
            ax.axvspan(x_group, x_group+1, alpha=0.5, color='green')
            #I put x_group+1 because I'm not sure how big of a highlight range you want

Hope this helps!