python – matplotlib中的序列标识:对齐xticks
发布时间:2020-12-20 11:58:20 所属栏目:Python 来源:网络整理
导读:我正在尝试使用matplotlib绘制 sequence logos. 整个代码可在 gist获得 相关部分是: class Scale(matplotlib.patheffects.RendererBase): def __init__(self,sx,sy=None): self._sx = sx self._sy = sy def draw_path(self,renderer,gc,tpath,affine,rgbFac
我正在尝试使用matplotlib绘制
sequence logos.
整个代码可在 gist获得 相关部分是: class Scale(matplotlib.patheffects.RendererBase): def __init__(self,sx,sy=None): self._sx = sx self._sy = sy def draw_path(self,renderer,gc,tpath,affine,rgbFace): affine = affine.identity().scale(self._sx,self._sy)+affine renderer.draw_path(gc,rgbFace) def draw_logo(all_scores): fig = plt.figure() fig.set_size_inches(len(all_scores),2.5) ax = fig.add_subplot(111) ax.set_xticks(range(len(all_scores))) xshift = 0 trans_offset = transforms.offset_copy(ax.transAxes,fig=fig,x=0,y=0,units='points') for scores in all_scores: yshift = 0 for base,score in scores: txt = ax.text(0,base,transform=trans_offset,fontsize=80,color=COLOR_SCHEME[base],weight='bold',ha='center',family='sans-serif' ) txt.set_clip_on(False) txt.set_path_effects([Scale(1.0,score)]) fig.canvas.draw() window_ext = txt.get_window_extent(txt._renderer) yshift = window_ext.height*score trans_offset = transforms.offset_copy(txt._transform,y=yshift,units='points') xshift += window_ext.width trans_offset = transforms.offset_copy(ax.transAxes,x=xshift,units='points') ax.set_yticks(range(0,3)) seaborn.despine(ax=ax,offset=30,trim=True) ax.set_xticklabels(range(1,len(all_scores)+1),rotation=90) ax.set_yticklabels(np.arange(0,3,1)) plt.show() ALL_SCORES1 = [[('C',0.02247014831444764),('T',0.057903843733384308),('A',0.10370837683591219),('G',0.24803586793255664)],[('T',0.046608227674354567),0.048827667087419063),0.084338697696451109),('C',0.92994511407402669)],[('G',0.0),0.011098351287382456),0.022196702574764911),1.8164301607015951)],[('C',0.020803153636453006),0.078011826136698756),0.11268374886412044),0.65529933954826969)],0.017393530660176126),0.030438678655308221),0.22611589858228964),0.45078233627623127)],0.022364103549245576),0.043412671595594352),0.097349627214363091),0.1657574733649966)],0.03264675899941203),0.045203204768416654),0.082872542075430544),1.0949220710572034)],0.0076232429756614498),0.011434864463492175),1.8867526364762088)],0.0018955903000026028),0.0094779515000130137),0.35637097640048931),0.58005063180079641)],[('A',0.01594690817903021),0.017541598996933229),0.2774762023151256),0.48638069946042134)],0.003770051401807444),0.0075401028036148881),0.011310154205422331),1.8624053924928772)],0.036479877757360731),0.041691288865555121),0.072959755514721461),1.1517218549109602)],0.011831087684038642),0.068620308567424126),0.10174735408273231),1.0009100180696691)],0.015871770937774379),0.018757547471915176),0.32176408355669878),0.36505073156881074)],0.022798100897300954),0.024064662058262118),0.24571286522646588),0.34070495229855319)]] ALL_SCORES2 = [[('A',0.01653482213365913),0.026710097292833978),0.035613463057111966),0.057235922770358522)],0.020055669245080433),0.023816107228533015),0.031336983195438178),0.058913528407423782)],0.018666958185377256),0.084001311834197651),0.093334790926886277),0.30333807051238043)],0.32027512306044359),0.82203948252180525)],0.012698627658037786),0.053334236163758708),0.096509570201087178),0.10920819785912497)],0.089472611853783468),0.1930724782107959),0.22132698721725386)],0.020962390607965918),0.026202988259957396),0.066380903591892068),0.07336836712788071)],0.10236420974570831),0.15354631461856247),0.29173799777526871)],0.027681850851852024),0.089966015268519078),0.53287562889815143)],0.034165612000664765),0.06833122400132953),0.072601925501412631),0.28186629900548432)],0.037325935579058833),0.23328709736911771),0.72785574379164719)],0.017470244196759552),0.062892879108334396),0.094339318662501587),0.19916078384305891)],0.096447131567581681),0.15844885900388422),0.48223565783790845)],0.069291952024925829),0.20787585607477749),0.46425607856700307)],0.21713201856318373),1.1495224512168551)],0.048934292002649343),0.27263391258618919),0.42642740173737281)],0.053607190685875404),0.2054942309625224),0.69689347891638032)],0.31312908494534769),0.84220926295645249)],0.068079835765814778),1.3207488138568066)],0.020257705570431345),0.048618493369035232),0.055371061892512348)],0.076286510680262556),0.20538675952378382),0.34622339462580698)]] “ALL_SCORE2”的输出: 期望的输出: 如notebook所示,xticklabels与字母表不能很好地对齐.我希望能够在xticks上应用offset_copy变换,以便字母的中心与刻度线对齐. 更新 我在pyseqlogo将它包装成python包 解决方法
我建议在整个脚本中使用数据坐标和单位坐标.
gact.py import matplotlib as mpl from matplotlib.text import TextPath from matplotlib.patches import PathPatch from matplotlib.font_manager import FontProperties fp = FontProperties(family="Arial",weight="bold") globscale = 1.35 LETTERS = { "T" : TextPath((-0.305,"T",size=1,prop=fp),"G" : TextPath((-0.384,"G","A" : TextPath((-0.35,"A","C" : TextPath((-0.366,"C",prop=fp) } COLOR_SCHEME = {'G': 'orange','A': 'red','C': 'blue','T': 'darkgreen'} def letterAt(letter,x,y,yscale=1,ax=None): text = LETTERS[letter] t = mpl.transforms.Affine2D().scale(1*globscale,yscale*globscale) + mpl.transforms.Affine2D().translate(x,y) + ax.transData p = PathPatch(text,lw=0,fc=COLOR_SCHEME[letter],transform=t) if ax != None: ax.add_artist(p) return p 运行代码 import matplotlib.pyplot as plt import gact fig,ax = plt.subplots(figsize=(10,3)) all_scores = ALL_SCORES2 x = 1 maxi = 0 for scores in all_scores: y = 0 for base,score in scores: gact.letterAt(base,score,ax) y += score x += 1 maxi = max(maxi,y) plt.xticks(range(1,x)) plt.xlim((0,x)) plt.ylim((0,maxi)) plt.tight_layout() plt.show() (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |