您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

怎么用Python绘制有趣的可视化图表

2025/5/20 5:03:42发布21次查看
schemdraw
那么在schemdraw模块当中呢,有六个元素用来代表流程图的主要节点的,椭圆形代表的是决策的开始和结束,代码如下:
import schemdrawfrom schemdraw.flow import *with schemdraw.drawing() as d: d += start().label("start")
output
箭头表示的是决策的走向,用来连接各个节点的,代码如下:
with schemdraw.drawing() as d: d += arrow(w = 5).right().label("connector")
output
平行四边形代表的是你所要去处理和解决的问题,而长方形所代表的是你所要为此做出的努力或者说是过程,代码如下:
with schemdraw.drawing() as d: d += data(w = 5).label("what's the problem")
output
with schemdraw.drawing() as d: d += process(w = 5).label("processing")
output
而菱形代表的则是决策的具体情况,代码如下:
with schemdraw.drawing() as d: d += decision(w = 5).label("decisions")
output
我们来绘制一个简单的流程图,假如周末的时候我们想着要不要出去露营(camping),那既然要去露营的话,我们肯定是需要查看一下天气,看一下是否是晴天(sunny),如果是下雨天(rainy)的话,就不去,按照这种逻辑,我们来绘制一下流程图,代码如下:
import schemdrawfrom schemdraw.flow import *with schemdraw.drawing() as d: d+= start().label("start") d+= arrow().down(d.unit/2) # 具体是啥问题嘞 d+= data(w = 4).label("go camping or not") d+= arrow().down(d.unit/2) # 第一步 查看天气 d+= box(w = 4).label("check weather first") d+= arrow().down(d.unit/2) # 是否是晴天 d+= (decision := decision(w = 5, h= 5,s = "true", e = "false").label("see if it's sunny")) # 如果是真的话 d+= arrow().length(d.unit/2) d+= (true := box(w = 5).label("sunny, go camping")) d+= arrow().length(d.unit/2) # 结束 d+= (end := ellipse().label("end")) # 如果不是晴天的话 d+= arrow().right(d.unit).at(decision.e) # 那如果是下雨天的话,就不能去露营咯 d+= (false := box(w = 5).label("rainy, stay at home")) # 决策的走向 d+= arrow().down(d.unit*2.5).at(false.s) # 决策的走向 d+= arrow().left(d.unit*2.15) d.save("palindrome flowchart.jpeg", dpi = 300)
output
networkx
networkx模块用来创建和处理复杂的图网络结构,生成多种随机网络和经典网络,分析网络结构和建立网络模型,例如在绘制人脉关系网的案例当中就可以用到networkx模块,
而例如一个公司的组织架构图,也可以用到该模块,来简单直观的绘制公司的整体架构,代码如下:
import networkx as nximport matplotlib.pyplot as pltimport numpy as npg = nx.digraph()nodes = np.arange(0, 8).tolist()g.add_nodes_from(nodes)# 节点连接的信息,哪些节点的是相连接的g.add_edges_from([(0,1), (0,2),(1,3), (1, 4),(2, 5), (2, 6), (2,7)])# 节点的位置pos = {0:(10, 10),1:(7.5, 7.5), 2:(12.5, 7.5),3:(6, 6), 4:(9, 6),5:(11, 6), 6:(14, 6), 7:(17, 6)}# 节点的标记labels = {0:"ceo", 1: "team a lead", 2: "team b lead", 3: "staff a", 4: "staff b", 5: "staff c", 6: "staff d", 7: "staff e"}nx.draw_networkx(g, pos = pos, labels = labels, arrows = true,node_shape = "s", node_color = "white")plt.title("company structure")plt.show()
output
看到这里,大家可能会觉得会指出来的结果有点简单,想要添加上去些许颜色,代码如下:
nx.draw_networkx(g, pos = pos, labels = labels,bbox = dict(facecolor = "skyblue",boxstyle = "round", ec = "silver", pad = 0.3),edge_color = "gray" )plt.title("company structure")plt.show()
output
以上就是怎么用python绘制有趣的可视化图表的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product