Friday 23 November 2018

Adjusting text style in pysvg

I wrote a previous blogpost on making a plot using pysvg.

Now I want to do something a bit more tricky, to adjust the font styles.
I found a tutorial online, but the commands in it don't seem to work with the current pysvg that's installed on the Sanger farm.
However, I found another file on github that gave me some hints...

Adjusting font style and alignment with pysvg
You need something like this in the Python:

# define the text style
    style = pysvg.builders.StyleBuilder()
    style.setFontFamily(fontfamily="Arial")
    style.setFontSize('1em')
    style.setFilling("blue")
    style.setTextAnchor("start")
    style.style_dict["alignment-baseline"] = "middle" # there doesn't seem to be a setAlignmentBaseline function


Then you can add text using something like this:
# add some text to our svg picture for this chromosome:
        t=pysvg.text.Text(mychr, x = mychrlen*xscale + 30, y = chrcnt*yscale + 30)
        t.set_style(style.getStyle())
        svg_document.addElement(t)

No comments: