我会说使用 Matplotlib 绘制它并不容易,但我假设您会对其他库感到满意,因为您在问题中声称“或其他包”。
我将展示我使用 Plotly 绘制它的方法。你可以简单地运行pip install plotly 来拥有这个库。
我会使用极轴来绘制圆形图表,这样可以让一切变得更容易。
首先,让我们定义一个函数,用于获取半径r 与theta 值在特定时间重复。请注意,num_points 是一个常数值,如果您将其设置得更高,它会使您的图表更加“平滑”。
import plotly.graph_objects as go
import numpy as np
def get_theta(pct, num_points=360):
start = pct[0] * 360
length = (pct[1] - pct[0]) * 360
step = 360 / num_points
return np.arange(start, start + length + step, step)
我会硬编码你的基因列表。在你的情况下,我认为你不必这样做:)
# Hard-code your gene list
gene_list = [
[0.1, 0.25],
[0.15, 0.3],
[0.6, 0.68]
]
然后,绘制圆图的代码。
# Get radial scale
max_r = 1 + (len(gene_list) + 1) * 0.1
# Create a figure
fig = go.Figure()
# Create the main circle
fig.add_trace(go.Scatterpolar(
r=[1]*360,
theta=get_theta([0, 1]),
mode='lines',
line_color='black',
line_width=3
))
# Create the zero indicator
fig.add_trace(go.Scatterpolar(
r=[1, max_r - 0.1],
theta=[0, 0],
mode='lines',
line_color='black',
line_width=3
))
# Loop the gene list to add all the gene cirles
for index, circle in enumerate(gene_list):
fig.add_trace(go.Scatterpolar(
r = [1 + (index + 1) * 0.1] * 360,
theta = get_theta(circle),
mode='lines',
line_width=3
))
# Configure the layout based on the requirements.
fig.update_layout(
polar=dict(
angularaxis=dict(
rotation=90,
direction="clockwise",
showticklabels=True,
showgrid=False
),
radialaxis=dict(
range=[0, 1 + (len(gene_list) + 1) * 0.1],
showticklabels=False,
visible=False
)
),
paper_bgcolor='white'
)
# Show the figure
fig.show()
这是代码生成的图表。
当然,我可以看到仍然存在问题。每个“基因”段不会在相同的半径上渲染。完全复制您的示例并非不可能,但显然并不容易。希望这对你来说已经足够了。如果它对你真的很重要。可能值得尝试使用小于0.1 的步骤(在我的代码中找到0.1 并替换它)。