eaton-lab / toytree

A minimalist tree plotting library using toyplot graphs
http://eaton-lab.org/toytree
BSD 3-Clause "New" or "Revised" License
164 stars 28 forks source link

Set svg tree plot without transparency #60

Closed MauriAndresMU1313 closed 2 years ago

MauriAndresMU1313 commented 2 years ago

I have been working with a plot of a phylogenetic tree. All is fine, however, I was unable to generate a SVG file without a transparency background. I want a white background. Here an example:

Screen Shot 2022-06-24 at 14 54 21

I read the documentation, however, I was unable to find out. Here the code

###################################################################################################
##### Creating canvas and creating the tree (new)

canvas = toyplot.Canvas(width=1200, height=2100)
ax0 = canvas.cartesian(bounds=(10, 1000, 50, 2000), padding=15, ymin=0, ymax=ntips)

##### Setting node colors only for 100.0 node support
colors = ['red' if ((i) == '100.0') else False for i in tre2.get_node_values('support', 1, 0)]
sizes = [10 if ((i) == '100.0') else 0 for i in tre2.get_node_values('support', 1, 0)]

##### Tree style
style = {
    "tip_labels_align": True,
    "tip_labels_style": {
        "font-size": "12px",
    },
    "edge_style": {
        "stroke": "black",
        "stroke-width": 3,
    },
}

#### creating canvas
tre2.draw(
    tip_labels=newNodnames,
    **style,
    node_labels=None,
    node_sizes=sizes,
    node_colors=colors,
    #tip_labels_colors=v3NewColors,
    scalebar=True,
    axes=ax0,
);

# Create and add matrix
matrix = np.arange(ntips * 1).reshape(ntips, 1)
matrix.shape

table = canvas.table(
    rows=ntips,
    columns=1,
    margin=0,
    bounds=(1110, 1140, 50, 2000), ### adding 7 to y-axis to make it frame 
)

# apply a color to each cell in the table
aa = 0
for ridx in range(matrix.shape[0]):
    bb = ntips - aa - 1
    for cidx in range(matrix.shape[1]):
        cell = table.cells.cell[ridx, cidx]
        cell.style = {
            "fill": v3NewColors[bb]
        }
        aa = (aa + 1)

# style axes
ax0.show = False

Any idea?

eaton-lab commented 2 years ago

Hi @MauriAndresMU1313, the trick is to set the background color of the canvas. Best, Deren.

import toytree
import toyplot, toyplot.svg

tree = toytree.rtree.rtree(10, seed=123)
c, a, m = tree.draw();
c.style["background-color"] = "white"
toyplot.svg.render(c, "/tmp/file.svg")