I am trying to draw sequence logos using matplotlib. The entire code is available on gist
The relevant portion is:
class Scale(matplotlib.patheffects
I simplified the code to make it generalizable to DPI/screen coordinates. The major change is that it draws the first character and then measures its screen width/height and then uses those parameters for the rest.
from matplotlib.patheffects import RendererBase
from matplotlib.transforms import offset_copy
from matplotlib.patches import Rectangle
class Scale(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, tpath, affine, rgbFace)
iupac = "XACMGRSVTWYHKDBN"
iupac_colors = ['black'] * 16
iupac_colors[1<<0] = 'blue'
iupac_colors[1<<1] = 'red'
iupac_colors[1<<2] = 'green'
iupac_colors[1<<3] = 'gold'
fig, ax = plt.subplots()
fig.set_size_inches(4, 1)
w=25
ax.set_xlim(0, w)
xoffset=int((w-pwm.shape[1])/2)
wscale=1
for i in range(pwm.shape[1]):
yshift = 0
for j in range(4):
base=iupac[(1<