Multiple vlines as one legend element

Good afternoon!

I’m struggling to create multiple year vlines in my chart.

This command does work well:

fig.add_vline(name="Yearlines", x="2023-01-01", showlegend=True, dash="dot"))

But if I’m trying to add multiple years, like this:

fig.add_vline(name="Yearlines", x=["2022-01-01", "2023-01-01"], showlegend=True, dash="dot"))

it does not crash, but it does not show any vline at all…

Am I’m missing something?

Thank you for your help!

Hey @DaGloom welcome to the forums.

This is because the vline is actually a annotation. So what you are trying to do is not possible imho.

I understand that the issue creating two vlines leads to two legend entrie- which you don’t want, right?

Hey, thanks for the quick reply!

Indeed! Also, it won’t be just two or three lines, but several (depending on how many years the chart shows). So a legend element for all V-lines would be very helpful.

I’m not sure how the annotations are handeled under the hood. I’m pretty sure they are not traces, so you can’t group them with legendgroup

I would actually create scatter traces (lines) with a range of y.min() to y.max(), style them, group them with legendgroups. If you don’t want to see all the traces (vlines) you couls set showlegend=False to all but one trace.

I must confess, I did not try that, but that is what I would try. :slight_smile:

Thank you very much! I will definitely try this out later.
Currently I’m sticking with the vlines, but without the legend (and no interaction)…

1 Like