python – 在nodebox opnegl中向标签的边缘添加标签
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 在nodebox opnegl中向标签的边缘添加标签,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3396字,纯文字阅读大概需要5分钟。
内容图文
![python – 在nodebox opnegl中向标签的边缘添加标签](/upload/InfoBanner/zyjiaocheng/771/4da69d0ec27e43049a1e28c4dca8fe14.jpg)
我想在我的图表中为每条边添加一个标签,如下所示:
基本上上面是中心每个边缘的标签:
当我为每个图添加边时,我试图添加一个标签,就像这样(对于图g):
g.add_edge(... label=edge.distance ...)
经过一些研究,我发现这种标签在Nodebox 1, which only works for Mac下是可能的,文件中似乎没有合适的Nodebox-OpenGL替代品.我收到的错误:
Traceback (most recent call last):
File "C:\foo\bar\baz\Imager.py", line 29, in <module>
g.add_edge(edge.fr, edge.to, length=edge.distance, weight=2, stroke=color(1.0, 0.2, 0.0), label="cheese")
File "C:\Python27\lib\site-packages\nodebox\graphics\physics.py", line 1254, in add_edge
e2 = e2(n1, n2, *args, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'label'
您可以重现该问题:
from nodebox.graphics import *
from nodebox.graphics.physics import Node, Edge, Graph
# Create a graph with randomly connected nodes.
# Nodes and edges can be styled with fill, stroke, strokewidth parameters.
# Each node displays its id as a text label, stored as a Text object in Node.text.
# To hide the node label, set the text parameter to None.
g = Graph()
# Random nodes.
for i in range(50):
g.add_node(id=str(i+1),
radius = 5,
stroke = color(0),
text = color(0))
# Random edges.
for i in range(75):
node1 = choice(g.nodes)
node2 = choice(g.nodes)
g.add_edge(node1, node2,
length = 1.0,
weight = random(),
stroke = color(0),
label = "Placeholder") #!!!!!!!!!!!!! ADDING THE label HERE
# Two handy tricks to prettify the layout:
# 1) Nodes with a higher weight (i.e. incoming traffic) appear bigger.
for node in g.nodes:
node.radius = node.radius + node.radius*node.weight
# 2) Nodes with only one connection ("leaf" nodes) have a shorter connection.
for node in g.nodes:
if len(node.edges) == 1:
node.edges[0].length *= 0.1
g.prune(depth=0) # Remove orphaned nodes with no connections.
g.distance = 10 # Overall spacing between nodes.
g.layout.force = 0.01 # Strength of the attractive & repulsive force.
g.layout.repulsion = 15 # Repulsion radius.
dragged = None
def draw(canvas):
canvas.clear()
background(1)
translate(250, 250)
# With directed=True, edges have an arrowhead indicating the direction of the connection.
# With weighted=True, Node.centrality is indicated by a shadow under high-traffic nodes.
# With weighted=0.0-1.0, indicates nodes whose centrality > the given threshold.
# This requires some extra calculations.
g.draw(weighted=0.5, directed=True)
g.update(iterations=10)
# Make it interactive!
# When the mouse is pressed, remember on which node.
# Drag this node around when the mouse is moved.
dx = canvas.mouse.x - 250 # Undo translate().
dy = canvas.mouse.y - 250
global dragged
if canvas.mouse.pressed and not dragged:
dragged = g.node_at(dx, dy)
if not canvas.mouse.pressed:
dragged = None
if dragged:
dragged.x = dx
dragged.y = dy
canvas.size = 500, 500
canvas.run(draw)
那么,问题仍然存在,如何在Nodebox-OpenGL中为图形边缘添加标签?
解决方法:
正如您在source中看到的那样,add_edge没有参数label. (搜索类Edge(对象):)
我能看到的最好的方法是创建自己的MyEdge类,该类源自官方Edge类,它使用文本(标签)添加
txt = Text(str, x=0, y=0, width=None, height=None)
要么
textpath(string, x=0, y=0, fontname=None, fontsize=None, fontweight=None)
在draw()方法中.
编辑
注意add_edge方法docstring:
def add_edge(self, id1, id2, *args, **kwargs):
""" Appends a new Edge to the graph.
An optional base parameter can be used to pass a subclass of Edge:
Graph.add_edge("cold", "winter", base=IsPropertyOf)
"""
内容总结
以上是互联网集市为您收集整理的python – 在nodebox opnegl中向标签的边缘添加标签全部内容,希望文章能够帮你解决python – 在nodebox opnegl中向标签的边缘添加标签所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。