Comments (4)
from pyvis.network import Network
import networkx as nx
g = Network(1000, 1000, notebook=True)
random_tree = nx.random_tree(50)
layout = nx.kamada_kawai_layout(random_tree)
g.from_nx(random_tree)
g.show("buttons.html")
for node in g.nodes:
node["x"] = layout[node["id"]][0] * 1000
node["y"] = layout[node["id"]][1] * 1000
g.toggle_physics(False)
g.show("buttons.html")
What I have done here is use networkx to generate a layout (dict of node id mappings to array of x and y). Then iterate through nodes and update each node object with relevant 'x' and 'y' attributes from the returned layout. You'll notice I also scaled the coordinates up by a factor of 1000 since resulting visual included nodes sitting on top of each other since the original coordinates were too small for the canvas. Also disable physics like in the example to make sure your layout is static.
You could also include the x and y coordinates when you add a node with add_node
or add_nodes
like in the example on the docs: https://pyvis.readthedocs.io/en/latest/tutorial.html#adding-list-of-nodes-with-properties
from pyvis.
Sorry the screenshots are not readable.
x=[21.4, 54.2, 11.2], y=[100.2, 23.54, 32.1]
What are the x and y units and where is the origo on screen?
from pyvis.
Sorry, I have edited my original comment with a more clear example.
Origin would be top left and follows HTML canvas coordinate system. https://www.w3schools.com/graphics/canvas_coordinates.asp
But to answer your question about the x and y's, if you use the add_nodes
method like:
g.add_nodes([1, 2], x=[10, 20], y=[5, 10])
then node 1 will have the coordinates (10, 5) and node 2 will have the coordinates (20, 10).
from pyvis.
Thank you!
from pyvis.
Related Issues (20)
- Clustering nodes with same labels?
- Filtering Nodes HOT 1
- Arrows are not displayed on the graph HOT 1
- highlightActive is not defined: Causes wierd jump of graph
- AttributeError: 'NoneType' object has no attribute 'render' HOT 4
- How do you change the fonts of the labels on the nodes?
- Be able to set the color of an edge independently of the source node
- Left to Right Orientation?
- Interactive Node in in Output HOT 1
- Use palette to color nodes and add colorbar
- Edge titles does not work? HOT 6
- Pyvis HTML Output does not use 100% of the page HOT 2
- Save as json
- Problem with labels, trying integration with pandas, Latex and Networkx
- No display of node labels if node is numeric HOT 1
- Change node size when label is inside
- Numeric string node ID silently cast from `str` to `int` in in `add_nodes` but not `add_edges`, raises `AssertionError`
- `write_html` opens browser to relative rather than absolute path, doesn't handle absolute paths as name
- Network not handling objects: assert isinstance(n_id, str) or isinstance(n_id, int) HOT 1
- Customizing Filter Menu Attribute Names in pyvis
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pyvis.