搜索
首页后端开发Python教程怎么用Python绘制有趣的可视化图表

SchemDraw

那么在SchemDraw模块当中呢,有六个元素用来代表流程图的主要节点的,椭圆形代表的是决策的开始和结束,代码如下:

import schemdraw
from schemdraw.flow import *
with schemdraw.Drawing() as d:
 d += Start().label("Start")

output

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

箭头表示的是决策的走向,用来连接各个节点的,代码如下:

with schemdraw.Drawing() as d:
 d += Arrow(w = 5).right().label("Connector")

output

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

平行四边形代表的是你所要去处理和解决的问题,而长方形所代表的是你所要为此做出的努力或者说是过程,代码如下:

with schemdraw.Drawing() as d:
 d += Data(w = 5).label("What's the problem")

output

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

with schemdraw.Drawing() as d:
 d += Process(w = 5).label("Processing")

output

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

而菱形代表的则是决策的具体情况,代码如下:

with schemdraw.Drawing() as d:
 d += Decision(w = 5).label("Decisions")

output

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

我们来绘制一个简单的流程图,假如周末的时候我们想着要不要出去露营(Camping),那既然要去露营的话,我们肯定是需要查看一下天气,看一下是否是晴天(Sunny),如果是下雨天(Rainy)的话,就不去,按照这种逻辑,我们来绘制一下流程图,代码如下:

import schemdraw
from 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

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

Networkx

Networkx模块用来创建和处理复杂的图网络结构,生成多种随机网络和经典网络,分析网络结构和建立网络模型,例如在绘制人脉关系网的案例当中就可以用到networkx模块,

而例如一个公司的组织架构图,也可以用到该模块,来简单直观的绘制公司的整体架构,代码如下:

import networkx as nx
import matplotlib.pyplot as plt
import numpy as np
G = 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

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

看到这里,大家可能会觉得会指出来的结果有点简单,想要添加上去些许颜色,代码如下:

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绘制有趣的可视化图表

以上是怎么用Python绘制有趣的可视化图表的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:亿速云。如有侵权,请联系admin@php.cn删除
Python:深入研究汇编和解释Python:深入研究汇编和解释May 12, 2025 am 12:14 AM

pythonisehybridmodelofcompilationand interpretation:1)thepythoninterspretercompilesourcececodeintoplatform- interpententbybytecode.2)thepytythonvirtualmachine(pvm)thenexecuteCutestestestesteSteSteSteSteSteSthisByTecode,BelancingEaseofuseWithPerformance。

Python是一种解释或编译语言,为什么重要?Python是一种解释或编译语言,为什么重要?May 12, 2025 am 12:09 AM

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允许fordingfordforderynamictynamictymictymictymictyandrapiddefupment,尽管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

对于python中的循环时循环与循环:解释了关键差异对于python中的循环时循环与循环:解释了关键差异May 12, 2025 am 12:08 AM

在您的知识之际,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations则youneedtoloopuntilaconditionismet

循环时:实用指南循环时:实用指南May 12, 2025 am 12:07 AM

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

Python:它是真正的解释吗?揭穿神话Python:它是真正的解释吗?揭穿神话May 12, 2025 am 12:05 AM

pythonisnotpuroly interpred; itosisehybridablectofbytecodecompilationandruntimeinterpretation.1)PythonCompiLessourceceCeceDintobyTecode,whitsthenexecececected bytybytybythepythepythepythonvirtirtualmachine(pvm).2)

与同一元素的Python串联列表与同一元素的Python串联列表May 11, 2025 am 12:08 AM

concateNateListsinpythonwithTheSamelements,使用:1)operatototakeepduplicates,2)asettoremavelemavphicates,or3)listCompreanspearensionforcontroloverduplicates,每个methodhasdhasdifferentperferentperferentperforentperforentperforentperfortenceandordormplications。

解释与编译语言:Python的位置解释与编译语言:Python的位置May 11, 2025 am 12:07 AM

pythonisanterpretedlanguage,offeringosofuseandflexibilitybutfacingperformancelanceLimitationsInCricapplications.1)drightingedlanguageslikeLikeLikeLikeLikeLikeLikeLikeThonexecuteline-by-line,允许ImmediaMediaMediaMediaMediaMediateFeedBackAndBackAndRapidPrototypiD.2)compiledLanguagesLanguagesLagagesLikagesLikec/c thresst

循环时:您什么时候在Python中使用?循环时:您什么时候在Python中使用?May 11, 2025 am 12:05 AM

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具