Skip to content

Instantly share code, notes, and snippets.

@CnrLwlss
Created July 7, 2016 10:50
Show Gist options
  • Save CnrLwlss/a7841969c068293a80afc75a2b7993ea to your computer and use it in GitHub Desktop.
Save CnrLwlss/a7841969c068293a80afc75a2b7993ea to your computer and use it in GitHub Desktop.
Visualising changes in rank order. For example, can think of changes in rank order of phenotypes in genetic screen with experiment or with type of analysis.
# Plot comparing ranks
import string
import random
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
def experiment(genes,vmin=0.0,vmax=1.0):
'''Simulate an experiment with (average) measurement for a set of genotypes'''
return({g:random.uniform(vmin,vmax) for g in genes})
def jitter(expt,sigma=0.01):
'''Add a little noise to existing experiment to simulate minor differences'''
return({g:expt[g]+random.gauss(0,sigma) for g in expt.keys()})
def rankvalues(dictionary):
'''Get order of dictionary keys that would sort dictionary values'''
sortedkeys=sorted(dictionary, key=dictionary.get,reverse=False)
return({k:(i+1) for i,k in enumerate(sortedkeys)})
Nexpt=5
genes=list(string.ascii_lowercase)
sigma=0.1
currentexpt=experiment(genes)
expts={}
for i in range(0,Nexpt):
expts["expt{:03}".format(i)]=currentexpt
currentexpt=jitter(currentexpt,sigma)
ranks={expt:rankvalues(expts[expt]) for expt in sorted(expts.keys())}
coords={gene:[ranks[rank][gene] for rank in sorted(ranks.keys())] for gene in genes}
exptlabs=sorted(expts.keys())
fig = plt.figure(facecolor='white')
ax = plt.axes(frameon=False)
ax.get_xaxis().tick_bottom()
cols=cm.coolwarm(np.linspace(0,1,len(genes))) # Specify colour scheme!
for gene,col in zip(genes,cols):
plt.plot(coords[gene],color=col)
for g,r,col in zip(genes,[ranks[exptlabs[-1]][g] for g in genes],cols):
plt.text(len(exptlabs)-1,r,g,color=col)
plt.xticks(range(0,len(exptlabs)),exptlabs)
ax.yaxis.set_visible(False)
plt.ylabel("Rank")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment