Python NetworkX module allows us to create, manipulate, and study structure, functions, and dynamics of complex networks.

Python NetworkX模块允许我们创建,操纵和研究复杂网络的结构,功能和动态。

1. Python NetworkX (1. Python NetworkX)

  • NetworkX is suitable for real-world graph problems and is good at handling big data as well.NetworkX适用于现实世界中的图形问题,也擅长处理大数据 。
  • As the library is purely made in python, this fact makes it highly scalable, portable and reasonably efficient at the same time.由于该库纯粹是用python制作的,因此这一事实使它同时具有高度的可伸缩性,可移植性和合理的效率。
  • It is open source and released under 3-clause BSD License.它是开源的,并根据3条款BSD许可发布。

2.为什么选择NetworkX? (2. Why NetworkX?)

NetworkX gives you a lot of reasons to go with it. Following are some features of NetworkX that makes it a package to go with:

NetworkX为您提供了很多使用它的理由。 以下是NetworkX使其与之配套的一些功能:

  • NetworkX has numerous standard graph algorithmsNetworkX具有众多标准图形算法
  • It supports data structures for graphs, digraphs, and multigraphs它支持图,有向图和多图的数据结构
  • It provides various network structure and measures for analysis它提供了各种网络结构和分析措施
  • Making classic/random graphs and synthetic networks is much easier using generators provided in the package使用软件包中提供的生成器,制作经典/随机图和合成网络要容易得多
  • Nodes in your network or graph can be absolutely anything, be it images, XML data or anything else网络或图形中的节点绝对可以是任何东西,可以是图像,XML数据或其他任何东西
  • Edges also can hold arbitrary data like timestamp and weight边缘还可以保存时间戳和权重之类的任意数据
  • It has been well tested with about 90% code coverage它已经过良好的测试,大约90%的代码覆盖率

Apart from above, it has an additional benefit because it is based on pure Python and so, it has a fast-prototyping syntax and very easy to learn. Let’s get started!

除此之外,它还具有其他优势,因为它基于纯Python,因此,它具有快速原型语法并且非常易于学习。 让我们开始吧!

3. NetworkX入门 (3. Getting Started with NetworkX)

NetworkX requires Python >= 2.7 installed on the machine. Let’s complete the installation as a first step.

NetworkX需要在计算机上安装Python> = 2.7。 首先,让我们完成安装。

3.1)安装NetworkX模块 (3.1) Install NetworkX Module)

We can install NetworkX using Python Package Index (pip):

我们可以使用Python Package Index(pip)安装NetworkX:

pip install networkx

In case you face any issues while installing the package using pip, install it from GitHub using the git command:

如果在使用pip安装软件包时遇到任何问题,请使用git命令从GitHub安装它:

pip install git://github.com/networkx/networkx.git

3.2)使用NetworkX (3.2) Using NetworkX)

Now that we have NetworkX installed on our machine, we can use it in any of our scripts using the following import statement:

现在我们已经在计算机上安装了NetworkX,我们可以使用以下import语句在任何脚本中使用它:

import networkx

3.3)创建图 (3.3) Creating Graphs)

As NetworkX library is used to manage relationships using the Graph structure, we can get started by creating a graph with no nodes and edges:

由于NetworkX库用于使用Graph结构管理关系,因此我们可以开始创建一个没有节点和边的图:

import networkx
graph = networkx.Graph()

Since there are no nodes or edges we can’t see the graph so let’s use idle to check if a graph is created or not:

由于没有节点或边,因此我们看不到图形,因此让我们使用idle检查是否创建了图形:

3.4)将节点添加到图 (3.4) Adding Nodes to a Graph)

Adding and checking nodes is quite simple and can be done as:

添加和检查节点非常简单,可以通过以下方式完成:

graph.add_node(1)

Or using list as:

或将列表用作:

graph.add_nodes_from([2,3])

And to see the nodes in existing graph:

并查看现有图中的节点:

graph.nodes()

When we run these set of commands, we will see the following output:

As of now, a graph does exist in the system but the nodes of the graphs aren’t connected. This can be done using the edges in a graph which makes a path between two Graph nodes.

运行这些命令集时,将看到以下输出:

到目前为止,系统中确实存在图,但是图的节点尚未连接。 这可以使用图的边缘来完成,该图在两个Graph节点之间建立路径。

3.5)在节点之间添加边 (3.5) Adding Edges between nodes)

Adding and checking edges is quite simple as well and can be done as:

添加和检查边缘也非常简单,可以通过以下方式完成:

graph.add_edge(1,2)

Or using list as:

或将列表用作:

graph.add_edges_from([(1,2),(2,3)])

And to see the nodes in existing graph, we can again print the edges of the graph object:

为了查看现有图形中的节点,我们可以再次打印图形对象的边缘:

graph.edges()

When we run these set of commands, we will see the following output:

运行这些命令集时,将看到以下输出:

4.属性 (4. Attributes)

Graphs are data structures which are used to connect related data and show the relationship between them by using a weight. This weight can be called an attribute of the relation of the two nodes in the Graph. Also, to exhibit properties for a node or an edge or for the graph itself, we can use attributes as well.

图形是数据结构,用于连接相关数据并通过权重显示它们之间的关系。 该权重可以称为图中两个节点的关系的属性。 同样,要显示节点或边线或图形本身的属性,我们也可以使用属性。

4.1)图形属性 (4.1) Graph Attributes)

We can assign meta-data to a Graph by adding graph attributes to a Graph object. Let’s see a code snippet on how this can be done:

通过将图形属性添加到Graph对象,我们可以将元数据分配给Graph。 让我们看一下如何做到这一点的代码片段:

graph.graph["day"]="Monday"
graph.graph

4.2)节点属性 (4.2) Node Attributes)

Here, we will add attributes to the nodes of the Graph object:

在这里,我们将属性添加到Graph对象的节点:

graph.add_node(1, time='5pm')
graph.add_nodes_from([3], time='2pm')
graph.node[1]
graph.node[1]['room'] = 714
graph.nodes(data=True)

4.3)边缘属性 (4.3) Edge Attributes)

Finally, we will assign some attributes to the edges of the Graph object. To assign edge attributes:

最后,我们将一些属性分配给Graph对象的边缘。 分配边缘属性:

graph.add_edge(1, 2, weight=4.7 )
graph.add_edges_from([(3,4),(4,5)], color='red')
graph.add_edges_from([(1,2,{'color':'blue'}), (2,3,{'weight':8})])
graph[1][2]['weight'] = 4.7
graph.edge[1][2]['weight'] = 4

Once we have added the attributes to the Graph, the nodes and the edges, we can finally print all the data:

将属性添加到图,节点和边后,我们最终可以打印所有数据:

5.有向图 (5. Directed Graph)

In the last section, we saw we could assign attributes to edges of a Graph. We can create a directed graph and add weighted edges as shown below.

在上一节中,我们看到可以将属性分配给Graph的边缘。 我们可以创建一个有向图并添加加权边,如下所示。

DG=networkx.DiGraph()
DG.add_weighted_edges_from([(1,2,0.5), (3,1,0.75)])
DG.out_degree(1,weight='weight')
DG.degree(1,weight='weight')
DG.successors(1)
DG.neighbors(1)

Once we run these commands, we will be able to see neighbors and successors of the Graph we just made:

一旦运行了这些命令,我​​们将能够看到我们刚刚制作的图的邻居和后继者:

6.绘图图 (6. Drawing Graph)

So far we have been performing various operations on graphs but not able to visualize any of the operations. Now let’s try to visualize them. For this, we’ll need the help of matplotlib library:

到目前为止,我们一直在对图形执行各种操作,但无法可视化任何操作。 现在,让我们尝试形象化它们。 为此,我们需要matplotlib库的帮助:

import matplotlib.pyplot as plt
networkx.draw(graph)
networkx.draw_random(graph)
networkx.draw_circular(graph)
networkx.draw_spectral(graph)
plt.show()

And the result of above-created graphs can be seen as:

上面创建的图的结果可以看成是:

7.结论 (7. Conclusion)

In this post, we have seen that NetworkX make it very easy to create and work with graphs. We have seen several examples of creating graphs and assigning attributes, weights, and direction to the edges of the Graphs as well.

在本文中,我们已经看到NetworkX使创建和使用图形变得非常容易。 我们已经看到了几个创建图形并将属性,权重和方向分配给图形边缘的示例。

NetworkX makes it easy to create graphs without much of hassle and with just a few lines of code. It also has generators for graphs and various networks and also it’s easy to analyze the graphs.

NetworkX使创建图形变得容易,而没有太多麻烦,只需要几行代码。 它还具有用于图形和各种网络的生成器,并且易于分析图形。

Reference: API Doc

参考: API文档

翻译自: https://www.journaldev.com/19410/networkx-python-graph-library

Python NetworkX – Python图形库相关推荐

  1. python networkx绘制图

    python networkx绘制图 1. 效果图 2. 安装 3. 源码 参考 这篇博客将介绍如何使用python,networkx绘制图. 1. 效果图 可调整点的大小,点是否有label,点的颜 ...

  2. wxpython图形_wxPython(Python的GUI图形库)v3.0.2.0免费版-独木成林

    wxPython(Python的GUI图形库) v3.0.2.0免费版 wxPython2.8-win32-ansi-py27 对应于32位 python 2.7版本. 简单例子: #!/usr/bi ...

  3. Python networkx 根据节点坐标来画网络图

    上篇请移步到python 使用networkx绘制带权无向图和带权有向图,以及标注特定路径_networkx 有向图_水w的博客-CSDN博客 python networkx图可视化 基础知识以及解决 ...

  4. Python+NetworkX画图的nx.draw_networkx(函数详解)

    Python+NetworkX画图的nx.draw_networkx函数详解 Python+NetworkX画图的nx.draw_networkx(函数详解) Python+NetworkX画图的nx ...

  5. python相关性分析及画图_数据分析Python手绘图形库有哪些?

    数据分析Python手绘图形库有哪些,今天,给大家介绍一个很酷的 Python 手绘风格可视化神包:cutecharts. 和 Matplotlib .pyecharts 等常见的图表不同,使用这个包 ...

  6. python中tk_可爱的 Python:Python 中的 TK编程

    可爱的 Python:Python 中的 TK编程 给使用 Python GUI 库的初学者的提示 David Mertz 博士 2000 年 12 月 01 日发布 我想要向您介绍能想像到的开始 G ...

  7. 什么是Python线程?Python线程如何创建?

    相信正在学习Python技术或者对Python语言有一定了解的人对于Python线程应该都不陌生,但是也有刚接触Python的小伙伴对于Python线程并不了解,今天小编就跟大家聊聊什么是Python ...

  8. Python杂谈——Python都能干什么呢?

    Python 今年 28 岁了.尽管它比我的许多读者年纪还要大,但是仍然受到高度的关注,因为它可以被应用于如今你所能想得到的相当多的软件开发和操作场景.要管理本地或者云基础设施吗?Python可以.开 ...

  9. 【Python】Python简单入门

    Python介绍   Python是一种高级的.动态类型的多范型编程语言.现在常用的Python版本是Python3.x. Python代码通常被认为是伪代码,因为在简明易懂的几行代码中可以表达出非常 ...

最新文章

  1. 【科普】为什么ip地址通常以192.168开头?
  2. CVPR 2018 MCCT:《Multi-Cue Correlation Filters for Roubust Visual Tracking》论文笔记
  3. 空之轨迹手游服务器维护,《空之轨迹》手游06月15日更新公告
  4. 任务管理平台_软件品质评测系统任务分发管理平台
  5. php flash 图片上传,Flash教程:flash+php实现图片上传
  6. css固定定位与绝对定位的区别
  7. leetcode题库5-- 最长回文子串
  8. 消息队列中点对点与发布订阅区别(good)
  9. 【转】JS判断SWF,JPG加载完毕、兼容(Activex,plugIn)所有浏览器
  10. Day05_生命周期_组件进阶
  11. Python 定时获取卫星图像做为桌面背景
  12. 手绘 | 我说话直,你别介意——我呸!
  13. Visio复制的图片在Word中不显示
  14. vscode代码格式管理插件prettier-Code formatter安装和设置
  15. 云平台编程与开发(一):云平台服务商一览
  16. 南理工校外调剂计算机有消息,提醒!这些学校已经开启预调剂了!
  17. 秉火429笔记之十四 USART--串口通信
  18. 入门系列:gdb学习——函数调用时参数传递
  19. 第108篇 Compound 简单部署
  20. RouterOS(ROS)软路由PPPOE拨号上网配置指南(附授权镜像下载)

热门文章

  1. AIR学习教程(一)
  2. [转载] python 去除字符串的标点符号 用_Python成为专业人士笔记–String字符串方法
  3. FPGA内部资源总结
  4. 项目案例模板之jdbc两种连接方式
  5. 【原创】Altium Designer Winter 09 笔记 之一
  6. Python 扩展知识:编程习惯
  7. myeclipse发布项目
  8. 捷信达会员管理系统SQL语句相关
  9. MVC部分视图的使用
  10. Mac OS 10.12 - 如何关闭Rootless机制?