Inconsistent row and col parameters used to add vline to faceted subplots
還沒有人認領這個 Issue。
- 主要語言
- 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:
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
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 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