【问题标题】:Moving y axis to the middle and making two legends on each side for Plotly diverging bar chart将 y 轴移动到中间并在每侧制作两个图例以用于 Plotly 发散条形图
【发布时间】:2021-12-19 04:19:46
【问题描述】:

我正在按照这个示例代码制作一个发散条形图,但不知道如何:

  1. 使实体名称(A、B、C 等)位于中间(0 处)
  2. 为每一侧制作两个单独的图例,而不是一个

输出图片:https://imgur.com/a/cdbc5j0

import numpy as np
import pandas as pd
import plotly.graph_objects as go

d = {'Who': ['A', 'B', 'C', 'D', 'E', 'F'],
     'Pants on Fire': [9,7,6,4, 2, 1],
     'False': [7, 6, 4, 5, 2,1],
     'Mostly False': [5, 4, 6,4, 2, 6],
     'Half True' : [4,2,5,6,3, 2],
     'Mostly True': [5,3,2,3,4,3],
    ' True': [2,4,3,6, 6, 8]}
df = pd.DataFrame(d)

fig = go.Figure()
for col in df.columns[1:4]:
    fig.add_trace(go.Bar(x=-df[col].values,
                         y=df['Who'],
                         orientation='h',
                         name=col,
                         customdata=df[col],
                         hovertemplate = "%{y}: %{customdata}"))
for col in df.columns[4:]:
    fig.add_trace(go.Bar(x= df[col],
                         y =df['Who'],
                         orientation='h',
                         name= col,
                         hovertemplate="%{y}: %{x}"))    

fig.update_layout(barmode='relative', 
                  height=400, 
                  width=700, 
                  yaxis_autorange='reversed',
                  bargap=0.01,
                  legend_orientation ='v',
                  legend_x=1.0, legend_y=1.0
                 )```

【问题讨论】:

  • 据我所知,Y 轴不能放在除左右(水平图的顶部和底部)以外的任何位置,因此唯一的方法是使用注释将其放置为零。另外,我认为您也不能拆分图例。根据我有限的经验,我能想到的唯一方法是将它们组合在一个子情节中。
  • 您的意思是您希望 A、B、C 的所有 6 条('True'、...、'Pants on Fire')从 0 开始并变为负数?
  • @r-beginners 感谢您的意见。这两项任务似乎都无法仅通过代码轻松完成。我将分别创建两个面,然后将两个图像连接起来。这听起来是目前最简单的解决方法。

标签: python plotly bar-chart


【解决方案1】:
from plotly.subplots import make_subplots
import pandas as pd
import plotly.express as px

d = {
    "Who": ["A", "B", "C", "D", "E", "F"],
    "Pants on Fire": [9, 7, 6, 4, 2, 1],
    "False": [7, 6, 4, 5, 2, 1],
    "Mostly False": [5, 4, 6, 4, 2, 6],
    "Half True": [4, 2, 5, 6, 3, 2],
    "Mostly True": [5, 3, 2, 3, 4, 3],
    " True": [2, 4, 3, 6, 6, 8],
}
df = pd.DataFrame(d)

# use two sub-plots so yaxes can be in middle...
fig = make_subplots(rows=1, cols=2, horizontal_spacing=0)

# create the two figures as per question ...
fig1 = px.bar(df, y="Who", x=df.columns[1:4]).update_traces(
    legendgroup="pants", legendgrouptitle_text="Pants"
)
fig2 = px.bar(
    pd.merge(
        df["Who"],
        df.loc[:, df.select_dtypes("number").columns] * -1,
        left_index=True,
        right_index=True,
    ),
    x=df.columns[4:],
    y="Who",
).update_traces(legendgroup="truth", legendgrouptitle_text="Truth")

# add traces from figures to sub-plots figure
for t in fig1.data:
    fig.add_trace(t, row=1, col=1)
for t in fig2.data:
    fig.add_trace(t, row=1, col=2)

# final config
fig.update_layout(barmode="relative", yaxis_visible=False)
fig.update_xaxes(autorange="reversed")

【讨论】:

  • 谢谢!这太棒了。我做了一些调整,我的情节看起来几乎和我想象的完全一样。我还想做 2 个额外的修改。 1. 每边有不同的离散色标 2. 在图下移动图例(左侧图例和右侧右侧图例) 我正在尝试将平面划分为 4 个子图以放入图例底部的子图,但还不知道如何一次在单个条上做色标。
  • 色标很容易完成plotly.com/python/discrete-color。每个图形(而不是每个子图)仅限于一个图例。因此我使用 legendgroup
猜你喜欢
  • 2021-07-22
  • 2020-03-20
  • 1970-01-01
  • 2020-08-26
  • 2015-06-09
  • 1970-01-01
  • 1970-01-01
  • 2019-07-29
  • 2014-08-02
相关资源
最近更新 更多