plotly / plotly/plotly.py

Empty Facets on partially filled MultiIndex DataFrame with datetime.date in bar plot

未关闭
#4,415 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

bug P3 sev-2
主要语言
Python
星标
18.8k
派生
2.8k
平均合并
16 小时 26 分钟
30 天内合并 PR
21

描述

I have a pandas dataframe with a multicolumn index consisting of three levels, which I want to plot using a plotly.express.bar. First dimension Station (str) goes into the facets, the second dimension Night (datetime.date) goes onto the x-axis and the third dimension Limit (int) is going to be the stacked bars.

The bug is, that the resulting figure does not show any data on facets which do only have data at one Night. The expected outcome would be that even though data exists only on one date this data is presented.

A reproducible example looks like this:

import datetime
import pandas as pd
import plotly.express as px

df = pd.DataFrame([
    ["station-01", datetime.date(2023,10,1), 50, 0.1],
    ["station-01", datetime.date(2023,10,1), 100, 0.15],
    ["station-01", datetime.date(2023,10,2), 50, 0.2],
    ["station-01", datetime.date(2023,10,2), 100, 0.22],
    ["station-01", datetime.date(2023,10,3), 50, 0.05],
    ["station-01", datetime.date(2023,10,3), 100, 0.02],
    ["station-02", datetime.date(2023,10,1), 50, 0.5],
    ["station-02", datetime.date(2023,10,1), 100, 0.2],
    ["station-03", datetime.date(2023,10,1), 50, 0.5],
    ["station-03", datetime.date(2023,10,1), 100, 0.5],
], columns=["Station", "Night", "Limit", "Relative Duration"])
df = df.set_index(["Station", "Night", "Limit"])

px.bar(
    df,
    x=df.index.get_level_values("Night"),
    y="Relative Duration",
    range_y=[0, 1],
    color=df.index.get_level_values("Limit"),
    facet_col=df.index.get_level_values("Station"),
)

The figure created looks like this:
newplot (2)

When replacing the datetime.date type by a string the figure looks like expected:

import datetime
import pandas as pd
import plotly.express as px

df = pd.DataFrame([
    ["station-01", "101", 50, 0.1],
    ["station-01", "101", 100, 0.15],
    ["station-01", "102", 50, 0.2],
    ["station-01", "102", 100, 0.22],
    ["station-01", "103", 50, 0.05],
    ["station-01", "103", 100, 0.02],
    ["station-02", "101", 50, 0.5],
    ["station-02", "101", 100, 0.2],
    ["station-03", "101", 50, 0.5],
    ["station-03", "101", 100, 0.3],
], columns=["Station", "Night", "Limit", "Relative Duration"])
df = df.set_index(["Station", "Night", "Limit"])

px.bar(
    df,
    x=df.index.get_level_values("Night"),
    y="Relative Duration",
    range_y=[0, 1],
    color=df.index.get_level_values("Limit"),
    facet_col=df.index.get_level_values("Station"),
)

newplot (3)

Due to this behavior I suspect the plot yielded from the initial data frame is false and there is a bug inside of the plotly.express code.

Thanks for helping out.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先通过 plotly.express.bar 运行提供的可复现示例,并比较 datetime.date 和 string 两种情况。从该入口点跟踪 facet 和日期轴的处理;当仅有一个 Night 的数据时,facet 对 datetime.date 值仍能显示其条形,这项修复就完成了。

由索引模型根据 Issue 内容生成。

评估

技术栈
pandas, python
领域
data-visualization
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
45/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。