- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 Python 中生成具有并行标记边/顶点的有向图(如下图所示)的最佳方法是什么?
我已经尝试过networkx,但它不适用于平行边缘。
这是我用来生成图表数据的代码。
from forex_python.converter import CurrencyRates
import pandas as pd
chosen_currencies = ['GBP', 'EUR', 'USD']
c = CurrencyRates()
rates = []
for currency_index in range(len(chosen_currencies)):
temp_list = []
for second_index in range(len(chosen_currencies)):
temp_list.append(c.get_rate(chosen_currencies[currency_index], chosen_currencies[second_index]))
rates.append(temp_list)
df = (pd.DataFrame.from_records(rates)).transpose()
df.columns = chosen_currencies
最佳答案
您可以使用数据帧将边直接读入 NetworkX 图,使用 from_pandas_adjacency 。为此,我们将dataframe
的索引设置为chosen_currencies
,以确保边缘映射正确。
from forex_python.converter import CurrencyRates
import pandas as pd
chosen_currencies = ['GBP', 'EUR', 'USD']
c = CurrencyRates()
rates = []
for currency_index in range(len(chosen_currencies)):
temp_list = []
for second_index in range(len(chosen_currencies)):
temp_list.append(c.get_rate(chosen_currencies[currency_index], chosen_currencies[second_index]))
rates.append(temp_list)
df = (pd.DataFrame.from_records(rates)).transpose()
df.columns = chosen_currencies
# GBP EUR USD
#0 1.000000 0.83238 0.768233
#1 1.201374 1.00000 0.922935
#2 1.301689 1.08350 1.000000
现在设置索引
df.set_index([pd.Index(chosen_currencies)], inplace=True)
# GBP EUR USD
#GBP 1.000000 0.83238 0.768233
#EUR 1.201374 1.00000 0.922935
#USD 1.301689 1.08350 1.000000
现在让我们创建图表
import networkx as nx
import matplotlib.pyplot as plt
G = nx.from_pandas_adjacency(df, create_using=nx.DiGraph)
# Set the figure size
plt.figure(figsize=(8,8))
# Get the edge labels and round them to 3 decimal places
# for more clarity while drawing
edge_labels = dict([((u,v), round(d['weight'], 3))
for u,v,d in G.edges(data=True)])
# Get the layout
pos = nx.spring_layout(G)
# Draw edge labels and graph
nx.draw_networkx_edge_labels(G,pos,edge_labels=edge_labels,
label_pos=0.15, font_size=10)
nx.draw(G, pos, with_labels=True,
connectionstyle='arc3, rad = 0.15',
node_size=800)
plt.show()
注意:箭头附近的数字是边权重。
您还可以查看this Google Colab Notebook与上面的工作示例。
您可以根据需要调整字体大小、标签位置等。
引用文献:
关于python - 在 Python 中生成具有并行标记边/顶点的有向图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60262159/
我想使用图中所示的迷宫,使用迭代深度优先搜索找到从起始节点到目标的路径。它是一个仅包含一对数字的文本文件,例如成对连接,又称边/弧。像这样: 11 3 2 3 0 3 1 4 5 4 5 7 6 7
问题:您有一个无向图 G = (V, E)(V = 顶点,E = 边),您必须访问每个顶点并在两个方向上通过每个边。 我所知道的图算法只有 DFS、BFS 和一些 MST(Kruskal 等)不幸的是
枚举任意图中两个顶点之间的所有简单路径通常需要指数时间,因为顶点之间可能存在指数数量的简单路径。但是,如果我们只对位于两个末端顶点之间的至少一条简单路径上的顶点怎么办? 即:给定一个无向图和两个不同的
我正在开发一个简单的 opengl 游戏以了解更多相关信息。但是由于某种原因,当我尝试随时间旋转我的立方体时,它会被拉伸(stretch)。你可以在照片中看到它: 我认为这与我的模型矩阵有关,但我不确
我已经在谷歌上搜索了很长一段时间,但我找不到任何东西。如何使用 Graphviz 绘制没有连接顶点的图形? 最佳答案 像这样: digraph g { SingleNode; } 简单地不定义
我目前正在使用 R 中的“igraph”包进行一些社交网络分析,我想知道是否有一种方法可以个性化社交网络中节点的放置。 例如,使用以下玩具代码: library(igraph) edg
我在 Box2D 中有一个多边形形状。形状是一个三角形,我希望有 3 个顶点。事实上,我创建的所有形状都会输出 8 个顶点。为什么是这样?如果我输出顶点数,那总是正确的数量。我不想渲染不必要的线条,但
来自user manual CGAL Surface_mesh 类: the data structure uses integer indices as descriptors for vertic
我正在尝试找到引用 ARFaceGeometry 网格索引的方法为了使用 ARKit 将图形放置在面部的特定部位。 我见过很多例子,其中功能与一些索引号,但我找不到对此列表的任何引用。它似乎有超过12
Apache TomCat(版本未知) 业务对象 4.1 顶点 4.4.3 在一台服务器上,我们拥有 TomCat 和 Business Objects。 APEX 也使用 TomCat。 在对我们的
我正在使用 MX Graph 进行一些工作,以帮助识别网站中的关键内容路径。我将其设置为每个顶点代表网站上的一个页面,每条边代表一组从页面 A 访问页面 B 的访问者。 一切都运行良好,除了边太多,我
我正在尝试使用三角形 strip 绘制一个平面。我了解如何手动执行此操作,但我真的很难使用 for 循环来执行此操作。到目前为止,下面的代码绘制了两个三角形。 //vertices for trian
如果我想通过 id 顶点获取名称,我可以使用这个函数:VAS(g, "name",id)但是如果我想要相反的方式,通过名称获取 id,我该怎么做呢? 最佳答案 igraph 本身不提供按名称查找顶点的
我有一个三角形,其任意顶点位于 3D 空间中。 我知道通过以下操作很容易找到这种三角形的质心: float centroid[3] = { 0, 0, 0 }; for (int i = 0; i =
我有一个点数组。每个点都有位置(x, y, z) 和法 vector (xn, yn, zn) ,一共6个 double 。考虑到浮点容差,我需要在此数组中找到唯一元素并删除重复条目。 实现它的简单有
我有一个相互连接的边列表 (E),如何找到从一个顶点连接到另一个顶点的最短路径? 我正在考虑使用 lowest common ancestors ,但边缘没有明确定义的根,所以我认为该解决方案不起作用
我现在正在使用计算着色器开发粒子系统。我将所有粒子都放在着色器存储缓冲区中。一个粒子包含两个顶点,当前位置和先前位置。 struct Particle{ glm::vec4 _currPo
我将我的顶点剪裁在边缘上,如这张专辑所示: http://imgur.com/a/VkCrJ 当我的地形大小为 400 x 400 时,我得到裁剪,但在 40x40 或更小时,我没有得到任何裁剪。这是
总是在顶点着色器中而不是在片段着色器中更好地进行硬计算吗?即使是具有超过 100.000 个多边形的高网格模型(假设有一堆独特的顶点)? 最佳答案 不,它并不总是更好。 选择合适的计算位置的最佳方法是
我想编辑一个立方体上的 1 个顶点,但我不知道该怎么做。我试过到处寻找此功能,但找不到解决方案。 这是我想要实现的目标的图像: 最佳答案 http://answers.unity3d.com/ques
我是一名优秀的程序员,十分优秀!