plotly / plotly/plotly.py

Inconsistent row and col parameters used to add vline to faceted subplots

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

还没有人认领这个 Issue。

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

描述

I'm trying to have a vertical line added to faceted plots, with the line being at a different position for every subplot. Here's a MRE:

import plotly.express as px
import pandas as pd
import numpy as np
from itertools import product

# Prepare data
combinations = list(product(range(1, 11), repeat=2))
df = pd.DataFrame(combinations, columns=['cat', 'x'])
df['y'] = np.random.randint(1, 100, size=len(df))

# Figure
fig = px.line(df, x='x', y='y', facet_col='cat', facet_col_wrap=3)

# Add vlines in a loop
def generate_lines(n, d):
    tuples_list = []
    a = 1  # Initial value for a
    b = 1  # Initial value for b
    for i in range(1, n+1):
        tuples_list.append((i, a, b))
        if i % d == 0:  # Check if the divider is reached
            a += 1  # Increment a when the divider is reached
            b = 1   # Reset b to 1 when the divider is reached
        else:
            b += 1  # Increment b otherwise
    return tuples_list

lines = generate_lines(10, 3)
for i, a, b in lines:
    fig.add_vline(x=i, row=a, col=b, opacity=1, line_width=2, line_dash='dash', line_color='red')

fig.show()

What this code should produce is a vertical line at the value of the cat parameter but this is obviously not the case:

image

It took me a while to figure out what's actually happening. The issue is that the row argument does not start at the top left in the same way as faceting does, but instead starts at the bottom left. As a result, the (1,2) plot with a value of 2 is not shown because that's an empty space (would correspond to cat=11) and the 2nd subplot does not receive their vertical line.

This leads to inconsistencies and the required code to make the location more complicated because e.g. reducing the number of categories to 9 would mean that (1,1) needs to suddenly get a value of 7.

The behavior would be more intuitive if row 1 was the top one instead of the bottom one. If this is not a bug but a feature, it would be good to have this explicitly stated in the documentation / API reference.

Using plotly 5.13.0

贡献指南

打开贡献指南

从这里开始

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

调研方向

首先使用 plotly 5.13.0 复现所提供的 MRE,重点关注 facet_col_wrap 如何分配子图行,以及 add_vline 如何解释 row 和 col。检查这些 API 所暴露的 facet 以及子图行/列行为。完成的标准是:行编号与 faceting 布局一致,或者文档/API 参考清楚地解释现有行为及其 workaround。

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

评估

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

把新 issue 发到你的邮箱

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