Xemnas
Xemnas

Reputation: 1

How to round Geodesic distances to the nearest meter

I have calculated the distances between a large number of nodes to give weights to the edges in a network that I'm building. Now that I'm trying to graph the network to display it, it comes out very messy because all the distances are going to 5/6+ decimal places and running over into the names of the nodes and it becomes unreadable.

I've tried using the round function with roung(g,3) where g is the geodesic distance between some two nodes but get "TypeError: type geodesic doesn't define round method". For constructing my graph I've tried multiple layouts and spring/fruchterman reingold layouts work the best but it still looks like a mess because of all the distance/weight numbers being so long. I've also tried using

weight=nx.get_edge_attributes(map,'distance') arr = np.array(list(weight.values())) K=3 rounded=np.round(arr,K)

but when I do this I get "AttributeError: 'geodesic' object has no attribute 'rint'". If there's a way to either round these distances down or have nx.draw() function only display the first x amount of numbers that would be perfect.

Upvotes: 0

Views: 51

Answers (1)

Haley Schuhl
Haley Schuhl

Reputation: 21

The datatype is incompatible with the numpy function you are calling, but can you cast the datatype to float and then try rounding?

I have some python code that labels distances on an image and do the decimal rounding with text = f"{data_list[i]:0,.3f}" where data_list is a list of floats.

import numpy as np
# convert to float
arr.astype(np.float32)
K=3
rounded=np.round(arr,K)

Upvotes: 1

Related Questions