首页 > 数据库 > 数据库开发技术 > Python制作精美的环形图
pycirclize库是一个用于制作环形图的工具,可以帮助用户展示环形结构数据的关系。本文将介绍如何在Python中使用pycirclize制作精美的环形图。
pycirclize的环形图主要的应用场景有:
基因组数据:通常用于比较不同样本的基因表现、突变等。
网络数据:对于可视化关系或交互非常有效,尤其是使用和弦图。
比较数据:可用于跨参数(如销售或绩效指标)比较。
时间序列数据:可以表示一段时间内的模式,尤其是对于季节性数据。
多维数据比较:可以表示跨类别的多个测量值的数据。圆形热图可用于显示变量之间的交互作用。
pip install pycirclize
Pycirclize包中的每个扇区代表圆周上的一个划分,数据中的“扇区”则表示各种类别或分组。在显示数据的每个扇区中,都存在称为“轨道”的同心环,可以呈现多层信息。
了解扇区和轨道,稍后我们将使用新数据看看如何使用扇区和轨道。
from pycirclize import Circos
# Initialize circos sectors
sectors = {"cat A": 15, "Cat B": 15, "Cat C": 12, "Cat D": 20, "Cat E": 10}
circos = Circos(sectors, space=6) #space =6, the space between each sector
for sector in circos.sectors:
# Plot sector axis & name text
sector.axis(fc="pink", ls="dashdot", lw=2, ec="green", alpha=0.5, )
sector.text(f"Sector: {sector.name}={sector.size}", size=12) #size of the text on figure
fig = circos.plotfig()
轨迹是圆内的同心环,每个圆环显示数据的不同方面或测量值。扇区提供分类细分,而轨迹提供数据细节层,允许在单个圆形图中查看数据集的视图。
可以在扇区半径范围 (0 – 100) 内自由放置轨迹。
from pycirclize import Circos
# Initialize circos sectors
sectors = {"cat A": 15, "Cat B": 15, "Cat C": 12, "Cat D": 20, "Cat E": 10}
circos = Circos(sectors, space=5)
for sector in circos.sectors:
# Plot sector axis & name text
sector.axis(fc="none", ls="dashed", lw=2, ec="Blue", alpha=0.3)
sector.text(f"{sector.name}={sector.size}", size=10)
# Set Track01 (Radius: 80 - 100)
track1 = sector.add_track((80, 100))
track1.axis(fc="red", alpha=0.2)
track1.text(track1.name)
# as you can see the track radius is kept from 80 to 100 and then
# next track is kept at 75 so that there is a gap of 5 between the tracks
# we can reduce or increase it, as done in the next tracks
# Set Track02 (Radius: 50 - 75)
track2 = sector.add_track((50, 75))
track2.axis(fc="cyan", alpha=0.2)
track2.text(track2.name)
# Set Track03 (Radius: 20 - 45)
track3 = sector.add_track((20, 48))
track3.axis(fc="lime", alpha=0.2)
track3.text(track3.name)
#Set Track04 (Radius 5 -15)
track4 = sector.add_track((5, 15))
track4.axis(fc="yellow", alpha=0.2)
track4.text(track4.name)
fig = circos.plotfig()
from pycirclize import Circos
import numpy as np
np.random.seed(0)
sectors = {"A": 10, "B": 15, "C": 12, "D": 20, "E": 15}
circos = Circos(sectors, space=5) #initilaise
for sector in circos.sectors:
# Plot sector name
sector.text(f"Sector {sector.name}", r=106, size=12)
# r = 106 place #the text, if we change the value the texts move
# Create x positions & randomized y values for data plotting
x = np.arange(sector.start, sector.end) + 0.5
#sector.start
#is 0 and ends at the value of sector defined ,for a it is 10, B is 15
y = np.random.randint(0, 100, len(x))
# Plot line
line_track = sector.add_track((75, 100), r_pad_ratio=0.1)
line_track.axis(fc='yellow')
line_track.xticks_by_interval(1)
line_track.line(x, y)
# Plot points
points_track = sector.add_track((45, 70), r_pad_ratio=0.1)
points_track.axis(fc='blue')
points_track.scatter(x, y)
# Plot bar
bar_track = sector.add_track((15, 40), r_pad_ratio=0.1)
bar_track.axis()
bar_track.bar(x, y)
fig = circos.plotfig()
pycirclize能完成许多精美环形图的制作,想深入了解的读者,可查阅官网的文档。
参考文献地址:https://moshi4.github.io/pyCirclize/
原文作者:Python学习杂记;如有侵权,请联系管理员删除。