gpt4 book ai didi

python - 将 Holoviews 轴更改为正确的格式

转载 作者:行者123 更新时间:2023-12-01 07:14:44 26 4
gpt4 key购买 nike

我正在尝试创建一个具有多个轴的绘图。但是,holoviews 不是将 Gene 和 db 放在 x 轴上,将突变放在 y 轴上,而是将 db 放在 y 轴上,将gene 放在 x 轴上。

我怎样才能从中得到多类别图?

mutated_positions = hv.Scatter(totaldf,
['gene', 'db'], 'mutations', xrotation=45).opts(size=10, color='#024bc2', line_color='#002869', jitter=0.2, alpha=0.5)

当前的情节如下所示: /image/jje5h.jpg我试图得到这样的轴: /image/GBziX.jpgY 轴上有突变。

我使用的数据框如下所示:

      gene       db  mutations
0 IGHV1-3 G1K_CL2 6
1 IGHV1-58 G1K_CL2 2
2 IGHV1-58 G1K_CL2 3
3 IGHV1-8 G1K_CL2 2
4 IGHV3-16 G1K_CL2 3
.. ... ... ...
141 IGHV4-61 G1K_CL3 11
142 IGHV4-61 G1K_CL3 12
143 IGHV4-61 G1K_CL3 10
144 IGHV4-61 G1K_CL3 13
145 IGHV7-81 G1K_CL3 4

最佳答案

下面的代码是将突变放在 y 轴上,并将 db 和/或基因放在 x 轴上的一种方法。
它创建一个Ndlayout,这意味着为每个基因创建一个单独的图。

# import libraries
import pandas as pd
import holoviews as hv
from holoviews import opts
hv.extension('bokeh')

# create dataframe
data = [
['IGHV4-61', 'G1K_CL2', 11],
['IGHV4-61', 'G1K_CL3', 12],
['IGHV4-61', 'G1K_CL3', 10],
['IGHV7-81', 'G1K_CL2', 13],
['IGHV7-81', 'G1K_CL3', 4],
]
df = pd.DataFrame(data, columns=['gene', 'db', 'mutations'])

# create layout plot with mutations on the y-axis
layout_plot = hv.Dataset(df).to.scatter('db', 'mutations').layout('gene')

# make plot look nicer
layout_plot = layout_plot.opts(opts.Scatter(size=10, ylim=(0, 15), width=250))

# show structure of holoviews layout plot
print(layout_plot)

# show plot in Jupyter
layout_plot

情节结构如下:

:NdLayout [gene]

:Scatter [db] (mutations)

结果图如下所示:
Ndlayout for gene db and mutations

作为替代方案,您也可以使用构建在 Holoviews 之上的 hvplot 库,它会给您与上面相同的结果。这与 pandas 绘图基本相同,您可以使用 argument by='gene' 和 subplots='True' 来创建 Ndlayout。

# import libraries
import hvplot
import hvplot.pandas
hv.extension('bokeh')

# create layout plot with hvplot
layout_plot = df.hvplot(
kind='scatter',
x='db',
y='mutations',
by='gene',
subplots=True, # creates a layout
size=100, # marker size
ylim=(0, 15),
width=250, # width of plot
)

# show structure of holoviews layout plot
print(layout_plot)

# show plot in Jupyter
layout_plot

关于python - 将 Holoviews 轴更改为正确的格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58030186/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com