将轴更改为正确的形式

2024-06-16 13:32:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个多轴的绘图。但不是把基因和db放在x轴上,把突变放在y轴上,而是把db放在y轴上,把基因放在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

Tags: httpscom绘图db基因colormutationsgene
1条回答
网友
1楼 · 发布于 2024-06-16 13:32:02

下面的代码是把你的突变放在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

作为一个替代方案,你也可以使用库hvplot,这是建立在holoviews之上,它将为你提供如上所述。这与pandas plotting的工作原理基本相同,您可以使用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

相关问题 更多 >