首页 > 编程知识 正文

matplotlib可以交互吗,matplotlib网格线

时间:2023-05-05 11:55:34 阅读:141840 作者:4395

什么是networkx?

networkx出生于02年5月。 用python语言编写的软件包,便于创建、操作和学习复杂的网络。 使用networkx,可以以标准化和非标准化数据格式存储网络,生成多个随机网络和经典网络,分析网络结构,构建网络模型,以及创建新的网络算法——百度百科

networkx能做什么?

3359 networkx.github.io/documentation/stable/auto _ examples/index.html

1 .画画

2 .有向图、有向图、网络图……

3 .总之各种各样的图

看到这个,心里不是动了吗? 今天的教程教你画封面的三层感知机的模型图!

获得开始!

首先,引入networkx和matplotlib模块importnetworkxasnximportmatplotlib.pyplotaspltimportnetworkxasnxg=NX.graph (),以获取热鹅添加一个节点g.add_node(a ) )和(a )添加节点g.add _ nodes _ from ([ 2,3 ] ) (两个节点g.add_edges_from,2和3 ) 2 )、2以下举例说明h=NX.path_graph(10 ) G.add_nodes_from(H ) h ) G.add_node(H ) h ) G.add_node(H )

importmatplotlib.pyplotaspltimportnetworkxasnxh=NX.path _ graph (10 ) G.add_nodes_from(H ) h ) nx.draw(G

生成了标签从0到9的10个点! 请不要着急。 丑陋是丑陋的。 稍后给他化妆。

另外,将板栗g=NX.graph(# )导入所有边,各边分别为g.add _ edges _ from ([ (1,2 ) ],) 1,3 ),2,4 ),2,5 ),3,6 )

那么,你现在知道了如何在图中添加边和节点。 接下来是构建环:

画圈

importmatplotlib.pyplotaspltimportnetworkxasnx # h=NX.path _ graph (10 ) # G.add_nodes_from(H ) h=NX.NX

画五角星

importnetworkxasnximportmatplotlib.pyplotasplt #画画! g=NX.graph(g.add_node )1) g.add _ nodes _ from [ 2,3,4,5 ] ) for i in range(5)5) : for j in range(i)

_edge(i+1, j+1)nx.draw(G, with_labels=True, #这个选项让节点有名称 edge_color='b', # b stands for blue! pos=nx.circular_layout(G), # 这个是选项选择点的排列方式,具体可以用 help(nx.drawing.layout) 查看 # 主要有spring_layout (default), random_layout, circle_layout, shell_layout # 这里是环形排布,还有随机排列等其他方式 node_color='r', # r = red node_size=1000, # 节点大小 width=3, # 边的宽度 )plt.show()

import randomG = nx.gnp_random_graph(10,0.3)for u,v,d in G.edges(data=True): d['weight'] = random.random()edges,weights = zip(*nx.get_edge_attributes(G,'weight').items())pos = nx.spring_layout(G)nx.draw(G, pos, node_color='b', edgelist=edges, edge_color=weights, width=10.0, edge_cmap=plt.cm.Blues)# plt.savefig('edges.png')plt.show()

加入权重

import matplotlib.pyplot as pltimport networkx as nxG = nx.Graph()G.add_edge('a', 'b', weight=0.6)G.add_edge('a', 'c', weight=0.2)G.add_edge('c', 'd', weight=0.1)G.add_edge('c', 'e', weight=0.7)G.add_edge('c', 'f', weight=0.9)G.add_edge('a', 'd', weight=0.3)elarge = [(u, v) for (u, v, d) in G.edges(data=True) if d['weight'] > 0.5]esmall = [(u, v) for (u, v, d) in G.edges(data=True) if d['weight'] <= 0.5]pos = nx.spring_layout(G) # positions for all nodes# nodesnx.draw_networkx_nodes(G, pos, node_size=700)# edgesnx.draw_networkx_edges(G, pos, edgelist=elarge, width=6)nx.draw_networkx_edges(G, pos, edgelist=esmall, width=6, alpha=0.5, edge_color='b', style='dashed')# labelsnx.draw_networkx_labels(G, pos, font_size=20, font_family='sans-serif')plt.axis('off')plt.show()


有向图

from __future__ import divisionimport matplotlib.pyplot as pltimport networkx as nxG = nx.generators.directed.random_k_out_graph(10, 3, 0.5)pos = nx.layout.spring_layout(G)node_sizes = [3 + 10 * i for i in range(len(G))]M = G.number_of_edges()edge_colors = range(2, M + 2)edge_alphas = [(5 + i) / (M + 4) for i in range(M)]nodes = nx.draw_networkx_nodes(G, pos, node_size=node_sizes, node_color='blue')edges = nx.draw_networkx_edges(G, pos, node_size=node_sizes, arrowstyle='->', arrowsize=10, edge_color=edge_colors, edge_cmap=plt.cm.Blues, width=2)# set alpha value for each edgefor i in range(M): edges[i].set_alpha(edge_alphas[i])ax = plt.gca()ax.set_axis_off()plt.show()

颜色渐变的节点

import matplotlib.pyplot as pltimport networkx as nxG = nx.cycle_graph(24)pos = nx.spring_layout(G, iterations=200)nx.draw(G, pos, node_color=range(24), node_size=800, cmap=plt.cm.Blues)plt.show()

颜色渐变的边

import matplotlib.pyplot as pltimport networkx as nxG = nx.star_graph(20)pos = nx.spring_layout(G)colors = range(20)nx.draw(G, pos, node_color='#A0CBE2', edge_color=colors, width=4, edge_cmap=plt.cm.Blues, with_labels=False)plt.show()

如何画一个多层感知机?

import matplotlib.pyplot as pltimport networkx as nx left, right, bottom, top, layer_sizes = .1, .9, .1, .9, [4, 7, 7, 2]# 网络离上下左右的距离# layter_sizes可以自己调整import randomG = nx.Graph()v_spacing = (top - bottom)/float(max(layer_sizes))h_spacing = (right - left)/float(len(layer_sizes) - 1)node_count = 0for i, v in enumerate(layer_sizes): layer_top = v_spacing*(v-1)/2. + (top + bottom)/2. for j in range(v): G.add_node(node_count, pos=(left + i*h_spacing, layer_top - j*v_spacing)) node_count += 1# 这上面的数字调整我想了好半天,汗for x, (left_nodes, right_nodes) in enumerate(zip(layer_sizes[:-1], layer_sizes[1:])): for i in range(left_nodes): for j in range(right_nodes): G.add_edge(i+sum(layer_sizes[:x]), j+sum(layer_sizes[:x+1])) # 慢慢研究吧pos=nx.get_node_attributes(G,'pos')# 把每个节点中的位置pos信息导出来nx.draw(G, pos, node_color=range(node_count), with_labels=True, node_size=200, edge_color=[random.random() for i in range(len(G.edges))], width=3, cmap=plt.cm.Dark2, # matplotlib的调色板,可以搜搜,很多颜色呢 edge_cmap=plt.cm.Blues )plt.show()

差不多就是这个效果了。

后续我会封装为一个类,加入动态演示,比如通过颜色深浅,显示神经网络在优化的时候权重变化。应该会很好玩,嘿嘿。

上面你也可以改变layer_sizes

比如改为233333

调皮了

layter_sizes = [2, 3, 4, 5, 5, 4, 3, ] 贼丑了

完。

原文发布时间为:2018-08-06
本文作者:DeepWeaver
本文来自云栖社区合作伙伴“ Python爱好者社区”,了解相关信息可以关注“ Python爱好者社区”

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。