繁体   English   中英

如何在 plotly(python)中双向 plot plot,两侧均带有正标签?

[英]how to plot bidirectional plot in plotly (python) with positive labels on both side?

我正在尝试使用 Python 中的 Plotly 创建一个双向条形图。我使用了此链接中给出的代码: 双向条形图,带注释 python plotly

代码:

import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots

# define data set
s1 = pd.Series(["negative_regulation_of_axon_extension_involved_in_axon_guidance",4,"regulation_of_neuronal_synaptic_plasticity",5])
s2 = pd.Series(["neuronal_stem_cell_population_maintenance",4,"synapse_assembly",8])
s3 = pd.Series(["neural_crest_cell_migration",5,"neuron_differentiation",11])
s4 = pd.Series(["chromatin_silencing",5,"axon_guidance",15])
s5 = pd.Series(["neuron_differentiation",28,"signal_transduction",113])
df = pd.DataFrame([list(s1),list(s2),list(s3),list(s4),list(s5)], columns = ['label1','value1','label2','value2'])

# create subplots
fig = make_subplots(rows=1, cols=2, specs=[[{}, {}]], shared_xaxes=True,
                    shared_yaxes=True, horizontal_spacing=0)

fig.append_trace(go.Bar(y=df.index, x=df.value1, orientation='h', width=0.4, showlegend=False, marker_color='#4472c4'), 1, 1)
fig.append_trace(go.Bar(y=df.index, x=df.value2, orientation='h', width=0.4, showlegend=False, marker_color='#ed7d31'), 1, 2)
fig.update_yaxes(showticklabels=False) # hide all yticks

annotations = []
for i, row in df.iterrows():
    if row.label1 != '':
        annotations.append({
            'xref': 'x1',
            'yref': 'y1',
            'y': i,
            'x': row.value1,
            'text': row.value1,
            'xanchor': 'right',
            'showarrow': False})
        annotations.append({
            'xref': 'x1',
            'yref': 'y1',
            'y': i-0.3,
            'x': -1,
            'text': row.label1,
            'xanchor': 'right',
            'showarrow': False})            
    if row.label2 != '':
        annotations.append({
            'xref': 'x2',
            'yref': 'y2',
            'y': i,
            'x': row.value2,
            'text': row.value2,
            'xanchor': 'left',
            'showarrow': False})  
        annotations.append({
            'xref': 'x2',
            'yref': 'y2',
            'y': i-0.3,
            'x': 1,
            'text': row.label2,
            'xanchor': 'left',
            'showarrow': False})

fig.update_layout(annotations=annotations)
fig.show()

我面临着几个问题:

  1. 在我的原始数据中,我只有正数数据。 但是当我尝试使用此代码对上述数据进行 plot 时,轴会像这样扭曲:

在此处输入图像描述

虽然如果我为一个数据集提供负数它工作正常:

数据:

s1 = pd.Series(["negative_regulation_of_axon_extension_involved_in_axon_guidance",-4,"regulation_of_neuronal_synaptic_plasticity",5])
s2 = pd.Series(["neuronal_stem_cell_population_maintenance",-4,"synapse_assembly",8])
s3 = pd.Series(["neural_crest_cell_migration",-5,"neuron_differentiation",11])
s4 = pd.Series(["chromatin_silencing",-5,"axon_guidance",15])
s5 = pd.Series(["neuron_differentiation",-28,"signal_transduction",113])
df = pd.DataFrame([list(s1),list(s2),list(s3),list(s4),list(s5)], columns = ['label1','value1','label2','value2'])

结果: 在此处输入图像描述

当我的两个轴都有正数时,如何为数据自定义相同的数据,这样 plot 看起来只像第二张图片,但在相反的轴上有正数?

  1. 如果我们看第二张图片中的条,因为轴,4 的条似乎比另一边的 8 或 11 的条大,是否有可能以某种方式将其更改为相对的?

谢谢你。

  • 使用Plotly Express的不同方法
  • 首先将数据框重组为:
团体 label 价值
1个 negative_regulation_of_axon_extension_involved_in_axon_guidance 4个
1个 neuronal_stem_cell_population_maintenance 4个
1个 neural_crest_cell_migration 5个
1个 染色质沉默 5个
1个 神经元分化 28
2个 调节_of_neuronal_synaptic_plasticity 5个
2个 突触组装 8个
2个 神经元分化 11
2个 axon_guidance 15
2个 信号转导 113
  • 现在可以为颜色facet_col做一个使用
  • 最后格式化所有轴和布局以获得所需的结果
import plotly.express as px
import pandas as pd

# define data set
s1 = pd.Series(
    [
        "negative_regulation_of_axon_extension_involved_in_axon_guidance",
        4,
        "regulation_of_neuronal_synaptic_plasticity",
        5,
    ]
)
s2 = pd.Series(["neuronal_stem_cell_population_maintenance", 4, "synapse_assembly", 8])
s3 = pd.Series(["neural_crest_cell_migration", 5, "neuron_differentiation", 11])
s4 = pd.Series(["chromatin_silencing", 5, "axon_guidance", 15])
s5 = pd.Series(["neuron_differentiation", 28, "signal_transduction", 113])
df = pd.DataFrame(
    [list(s1), list(s2), list(s3), list(s4), list(s5)],
    columns=["label1", "value1", "label2", "value2"],
)

fig = px.bar(
    pd.wide_to_long(
        df.reset_index(), stubnames=["label", "value"], i="index", j="group"
    )
    .reset_index()
    .drop(columns="index")
    .assign(group=lambda d: d["group"].astype(str)),
    y="label",
    x="value",
    facet_col="group",
    facet_col_spacing=10 ** -9,
    color="group",
    color_discrete_sequence=["#4472c4", "#ed7d31"],
)

fig.update_layout(
    yaxis2={"side": "right", "matches": None, "showticklabels": False},
    yaxis={"showticklabels": False},
    xaxis={"autorange": "reversed"},
    xaxis2={"matches": None},
    showlegend=False,
)
fig.for_each_annotation(lambda a: a.update(text=""))
fig.update_traces(texttemplate="%{y}", textposition="auto")

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM