将holoviews轴更改为正确的格式

时间:2019-09-20 14:21:53

标签: python holoviews hvplot holoviz

我正在尝试创建具有多轴的绘图。但是,不是将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)

当前情节如下所示: https://imgur.com/a/NNaIJdr 我试图得到这样的轴: https://imgur.com/a/ZmXjvRa 在Y轴上有突变。

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

      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

1 个答案:

答案 0 :(得分:1)

下面的代码是一种将突变置于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]

     
    

:散点[db](突变)

  

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

作为替代方案,您还可以使用基于holoviews构建的hvplot库,它与上面的内容相同。这项工作与熊猫绘图基本上相同,在熊猫绘图中,您可以使用 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