plotly / plotly/plotly.py

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

Aperta
#4,553 2 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug P3 sev-2
Lingua principale
Python
Stelle
18.8k
Fork
2.8k
Merge medio
16h 26m
PR unite (30g)
21

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia riproducendo l'MRE fornito con plotly 5.13.0, concentrandoti su come facet_col_wrap assegna le righe dei subplot e su come add_vline interpreta row e col. Esamina il comportamento dei facet e delle righe/colonne dei subplot esposto da queste API. Il lavoro è completato quando la numerazione delle righe corrisponde al layout del faceting oppure quando la documentazione/riferimento dell'API spiega chiaramente il comportamento esistente e il relativo workaround.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
data-visualization
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.