4

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!

kurtosis
  • 1,365
  • 2
  • 12
  • 27

2 Answers2

4

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.

Tiago Peixoto
  • 5,149
  • 2
  • 28
  • 28
  • Exactly what I needed, with a single argument! Thank you. I think this should be documented at http://graph-tool.skewed.de/static/doc/draw.html – kurtosis Sep 23 '14 at 12:49
  • Hope more questions on StackOverflow will make graph-tool more popular tool for graph processing. As yet, I think, it is the lack of questions on web that hinders graph-tool from being more competitive than networkX, for example. – kurtosis Sep 23 '14 at 12:58
  • Yes, this is an omission. I'll add it to the documentation. – Tiago Peixoto Sep 23 '14 at 13:35
  • @TiagoPeixoto How come `gprops={"bgcolor":"white"}` does not work as intended, does it not get sent to graphviz or are the graphviz arguments incorrect? – Hooked Sep 23 '14 at 14:35
  • @Hooked You are confusing two different functions, ``graph_draw()``, and ``graphviz_draw()``. The second uses graphviz, the first does not. The ``gprops`` parameter should only be passed to the second function. (Note that graphviz_draw() is deprecated) – Tiago Peixoto Sep 23 '14 at 14:38
  • @TiagoPeixoto: the documentation says that you bg_color can be a string, but this is not true, I think. https://graph-tool.skewed.de/static/doc/draw.html?highlight=bg_color – jvdh Jul 04 '18 at 13:06
2

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.

Hooked
  • 84,485
  • 43
  • 192
  • 261
  • Thank you! May I ask you if you would recommend some freeware for flatting png's on Mac OSX? – kurtosis Sep 22 '14 at 15:30
  • @kurtosis Happy to help! If this answers your question please accept the answer so we know this is what you are looking for. An awesome freeware alternative to photoshop is GIMP - it's quite simple to remove an alpha channel using it! – Hooked Sep 22 '14 at 16:40
  • Finally I accepted another answer providing solution with the single argument to graph_draw. You were almost right about bicolor! – kurtosis Sep 23 '14 at 13:00