GraphPlot.jl icon indicating copy to clipboard operation
GraphPlot.jl copied to clipboard

node colors not saving to `.pdf`

Open SimonEnsemble opened this issue 4 years ago • 6 comments

oddly the node colors are not saving when I save to .pdf.

after

gp = gplot(SimpleGraph(xtal.bonds), 
	       nodefillc=node_colors, 
	       nodestrokec=colorant"black", 
	       nodestrokelw=0.5)

what Pluto.jl displays: image after

draw(PDF("IRMOF-1_graph.pdf", 12cm, 12cm), gp)

what the .pdf looks like: image

SimonEnsemble avatar Sep 14 '21 22:09 SimonEnsemble

very oddly, I cannot re-create this behavior with the minimal example below!

	my_graph = SimpleGraph(350)
	add_edge!(my_graph, 1, 3)
	colorz = [colorant"red" for i = 1:nv(my_graph)]
	my_gp = gplot(my_graph, nodefillc=colorz, nodestrokec=colorant"black", nodestrokelw=1)
	draw(PDF("toy_graph.pdf", 8cm, 8cm), my_gp)

SimonEnsemble avatar Sep 14 '21 22:09 SimonEnsemble

mk this is bizarre. the PDF function properly saves color when I put the gplot(...) directly into the function draw. so

draw(PDF("toy_graph.pdf", 8cm, 8cm), gplot(...))

totally works! it was defining the output of gplot to be gp then passing it to draw that caused this odd behavior of leaving out colors. actually if I view gp again in a separate Pluto.jl cell, the colors disappear. odd right?

image

SimonEnsemble avatar Sep 15 '21 03:09 SimonEnsemble

When I ran the following code in Jupyter Notebook the nodes are black:

 my_graph = SimpleGraph(100)
 add_edge!(my_graph, 1, 3)
 colorz = [colorant"red" for i = 1:nv(my_graph)]
 my_gp = gplot(my_graph, nodefillc=colorz)

The same code, but setting the number of Nodes as 99 shows red. Really strange. It also works if I set nodefillc=colorant"red" (both with 99 and 100). It also works if I change only the first color of the array to something else. image

When saving to pdf or png the output is always the same as in IJulia Cell. Maybe the process of saving to pdf is not the problem here.

System Linux Mint with Julia 1.6.3.

tomtuamnuq avatar Oct 27 '21 17:10 tomtuamnuq

When I ran the following code in Jupyter Notebook the nodes are black:

 my_graph = SimpleGraph(100)
 add_edge!(my_graph, 1, 3)
 colorz = [colorant"red" for i = 1:nv(my_graph)]
 my_gp = gplot(my_graph, nodefillc=colorz)

I get red nodes in VS Code with the above code. Windows OS. Julia 1.7.

However, when run my_gp on the REPL, the plot colors switch to black....

Decreasing the number of nodes to 99 or just using colorz = colorant"red" fixes the problem, which makes me think this has something to do with the size of the nodefillc....

hdavid16 avatar Jul 27 '22 04:07 hdavid16

@SimonEnsemble @tomtuamnuq This is a bug in Compose.jl A workaround is to ensure that if you use vector inputs, the corresponding inputs for that same Compose object should also be vectors.

From the GraphPlot sourcecode:

    compose(context(units=UnitBox(-1.2, -1.2, +2.4, +2.4)),
            compose(context(), texts, fill(nodelabelc), fontsize(nodelabelsize)),
            compose(context(), nodes, fill(nodefillc), stroke(nodestrokec), linewidth(nodestrokelw)),
            compose(context(), edgetexts, fill(edgelabelc), fontsize(edgelabelsize)),
            compose(context(), arrows, stroke(edgestrokec), linewidth(edgelinewidth)),
            compose(context(), lines, stroke(edgestrokec), linewidth(edgelinewidth)))

If nodefillc is a vector, then nodestrokec and nodestrokelw should also be vectors (of size nv(g)), since these are all used for the nodes object. Doing so will preserve color when saving.

However, this is only an issue for large graphs and will hopefully get fixed in a future release of Compose.jl

hdavid16 avatar Jul 27 '22 06:07 hdavid16

Or, just use GraphMakie.jl:

 my_graph = SimpleGraph(100)
 add_edge!(my_graph, 1, 3)
 colorz = [colorant"red" for i = 1:nv(my_graph)]
 my_gp = graphplot(my_graph, node_color=colorz)

image

hdavid16 avatar Apr 03 '23 15:04 hdavid16