Inconsistent row and col parameters used to add vline to faceted subplots
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
I see where this is coming from; start_cell is set to "bottom-left" in the call to make_subplots(): link
Unfortunately it's not as simple as just changing start_cell to "top-left" because that results in the subplots being out of order when read from the top left -- still looking into where else this would need to be changed.
Bumping this issue as it's something that I recently spent too much time debugging to arrive at this GH issue. Would be nice if either this "bottom-left" behavior was documented in the plotly express documentation re: faceting (or the docs linked to this issue) .
I was able to throw together a slightly more minimal example displaying the same issue as OP:
import pandas as pd
import plotly.express as px
df = pd.DataFrame.from_records(
data=[
{"x": x, "y": y, "z": z, "facet": facet}
for x, y, z in zip(
range(0, 10),
range(0, 10),
range(0, 10),
)
for facet in range(0, 9)
]
)
fig = px.scatter(data_frame=df, x="x", y="y", color="z", facet_col="facet", facet_col_wrap=3)
fig.print_grid()
for i, facet in enumerate(range(0, 9)):
row, col = map(lambda v: v + 1, divmod(i, 3))
fig.add_vline(row=row, col=col, x=facet, annotation_text=f"({row}, {col})")
fig
This is the format of your plot grid:
[ (3,1) x7,y7 ] [ (3,2) x8,y8 ] [ (3,3) x9,y9 ]
[ (2,1) x4,y4 ] [ (2,2) x5,y5 ] [ (2,3) x6,y6 ]
[ (1,1) x,y ] [ (1,2) x2,y2 ] [ (1,3) x3,y3 ]
Even if this bug requires reworking a number of internal parts, it would be great if there were some additional tools to improve the ergonomics of either accessing subplots by (row, col) coordinates and strategy ("top-left" or "bottom-right") or mapping coordinates from one strategy to another.
I've been a happy user of all things plotly/dash for a while, but this is a glaring bug and IMO should have some kind of workaround at this point.