Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edgelabels attached to the wrong edges #192

Closed
colbec opened this issue Dec 23, 2022 · 3 comments
Closed

Edgelabels attached to the wrong edges #192

colbec opened this issue Dec 23, 2022 · 3 comments

Comments

@colbec
Copy link

colbec commented Dec 23, 2022

With Julia 1.8.3 Official release on openSUSE Leap 15.4 and Gnome:

With the following Julia code (uploaded as txt) I attempt to create a graph with 6 nodes and 4 edges. I assign random real values as weights to the edges. The adjacency matrix is created correctly but the attempt to use the edge weights as edge labels is only correct about 50% of the time. The weights are always correct, but as labels they are frequently attached to the wrong edges in the plot.

The last run produced the attached image and this output:

julia> thetest()
1, 5, 0.89
2, 5, 0.67
3, 4, 0.68
4, 5, 0.51

I believe this may be an issue, but also my coding may not be appropriate. I'm working on it.

edgetest.jl.txt
thetest

@colbec
Copy link
Author

colbec commented Dec 24, 2022

Two additional observations:

  1. In another open issue it is noted that all grouped variables need to be the same type when passed to Compose, so I changed the edgecolors to a vector consisting of all colorant"lightgreen" but this did not ease the problem.
  2. One thing that seems consistent is that the first run of the script after Julia is up and running labels the edges correctly, but subsequent runs contain errors. This is a major problem in a Julia context since it means that repeated runs of the script must reload all the items in using all over again which is very painful.

@hdavid16
Copy link
Contributor

hdavid16 commented Dec 24, 2022

Hi @colbec,

Your issue is that your GraphPlot.jl iterates through edges(G) when assigning edge labels. Your vector edgeweights is not always in the same order as edges(G), which is what causes the labels to be placed on the edges you are not expecting. Because of this, you shouldn't need to use sort! or shuffle!. Here is a version of your code that should fix this problem (NOTE: you should avoid sharing code as an external file and just use GitHub's code insertion feature (i.e., paste your code and add three accent marks above and three accent marks below))):

using Graphs, SimpleWeightedGraphs, GraphPlot
using Random, Compose, Colors, Combinatorics
using Graphs, SimpleWeightedGraphs, GraphPlot
import Cairo, Fontconfig

function thetest()
	num_v = 6
	num_e = 4
	G = SimpleWeightedGraph()
	add_vertices!(G, num_v)
	labels = collect(1:num_v)
	nodecolors = rand(RGB{Float64},num_v)
	edgecolors = colorant"lightgreen"
	edgecombs = sort!(shuffle!(collect(combinations(1:num_v,2)))[1:num_e])
	# edgeweights = []
	for i in 1:num_e
		wt = round(max(rand(),0.1),digits=2)
		(x,y) = edgecombs[i]
		add_edge!(G, x, y, wt)
		println("$x, $y, $wt")
		# push!(edgeweights,wt)
	end
	draw(
		PNG("thetest.png",20cm,20cm), 
		gplot(G,nodelabel=labels,
			layout=stressmajorize_layout, 
			nodefillc=nodecolors,
			edgelabel=[G.weights[src(e),dst(e)] for e in edges(G)],#edgeweights,
			edgestrokec=edgecolors
		)
	)
    
    return G
end

@colbec
Copy link
Author

colbec commented Dec 24, 2022

Indeed this solves my issue. Thanks.

@colbec colbec closed this as completed Dec 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants