Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing background color in graph-tool

I'm setting about to use a python package named graph-tool to visualize graphs. For some reason it sets grey background color each time saving a graph, which looks far from pleasant. Anyone knows how to change it to white?

For example, this sample code:

from graph_tool.all import *
g = price_network(5000)
p = sfdp_layout(g)
graph_draw(g, pos=p, output="example.png")

being run on iPython notebook, displays graph with white background on the screen, but saved picture has grey background.

The outcome is the same whatever format is used for output (at least with .png, .pdf and .svg). With networkX there was no such a problem, but graph drawing there is slower and much less flexible.

Thanks for any help!

like image 815
kurtosis Avatar asked Sep 15 '25 17:09

kurtosis


2 Answers

You should just pass the bg_color option to graph_draw(). For example:

graph_draw(g, pos=p, bg_color=[1,1,1,1], output="example.png")

will produce a white background, instead of transparent.

like image 154
Tiago Peixoto Avatar answered Sep 17 '25 07:09

Tiago Peixoto


The "grey background color" that you see is your PNG viewers way of rendering the transparent background. For example, I see a checkerboard pattern on my screen:

enter image description here

but when I upload the image or print it out anywhere it has a (default) white background:

enter image description here

Flatten the image in an editor to remove the alpha channel if it bothers you.

In theory you should be able to pass gprops={"bgcolor":"white"} to graph_draw but this doesn't work for me.

like image 21
Hooked Avatar answered Sep 17 '25 07:09

Hooked