Pandas: How to plot multiple lines against date using plotly as backend?

Short answer:

You're seeing this error because running df.plot() will trigger px.line() after defining pd.options.plotting.backend="plotly". And px.line() does not have a legend atribute. But you don't need it. All you need is:

px.line(df, x = 'Date', y = 'Balance', color = 'Account')

And you'll get:

enter image description here

The details:

Setting pd.options.plotting.backend="plotly" will, as you surely may know, override the default plotting backend for pandas which is matplotlib. Still, when running help(df.plot()) after that, the help info that pops up still seems to be info about matplotlib, which does in fact have a legend attribute.

But px.line() is what will be triggered by df.plot() after instantiating pd.options.plotting.backend="plotly". And this is what triggers your error, since px.line does not have legend attribute. Dut don't worry about that, since things are about to get really simple for you, because px.line() will produce a grouped legend for you. You don't even need to group your data as long as you apply df.plot() correctly.

But before we come to that, we'll have to take a look at your provided dataset. Given the wording of your question, and the look of the 'data' what you've provided, my understanding is that you've got several non-unique accounts under account associated with different values for balance spread across multiple non-unique dates. Something like this:

          Date             Account  Balance
0   01.01.2022  NL00ABCD0123456789        1
1   01.01.2022  NL00ABCD0123456790        2
2   01.01.2022  NL00ABCD0123456791        2
3   01.01.2022  NL00ABCD0123456792        3
4   01.01.2022  NL00ABCD0123456793        4
5   02.01.2022  NL00ABCD0123456789        2
6   02.01.2022  NL00ABCD0123456790        3
7   02.01.2022  NL00ABCD0123456791        3
8   02.01.2022  NL00ABCD0123456792        4
9   02.01.2022  NL00ABCD0123456793        5

If that's the case, then all you need to do is run:

px.line(df, x = 'Date', y = 'Balance', color = 'Account')

Plot:

enter image description here

Complete code:

import pandas as pd
import plotly.express as px

pd.options.plotting.backend="plotly"
df = pd.DataFrame({'Date': {0: '01.01.2022',
              1: '01.01.2022',
              2: '01.01.2022',
              3: '01.01.2022',
              4: '01.01.2022',
              5: '02.01.2022',
              6: '02.01.2022',
              7: '02.01.2022',
              8: '02.01.2022',
              9: '02.01.2022',
              10: '03.01.2022',
              11: '03.01.2022',
              12: '03.01.2022',
              13: '03.01.2022',
              14: '03.01.2022',
              15: '04.01.2022',
              16: '04.01.2022',
              17: '04.01.2022',
              18: '04.01.2022',
              19: '04.01.2022'},
             'Account': {0: 'NL00ABCD0123456789',
              1: 'NL00ABCD0123456790',
              2: 'NL00ABCD0123456791',
              3: 'NL00ABCD0123456792',
              4: 'NL00ABCD0123456793',
              5: 'NL00ABCD0123456789',
              6: 'NL00ABCD0123456790',
              7: 'NL00ABCD0123456791',
              8: 'NL00ABCD0123456792',
              9: 'NL00ABCD0123456793',
              10: 'NL00ABCD0123456789',
              11: 'NL00ABCD0123456790',
              12: 'NL00ABCD0123456791',
              13: 'NL00ABCD0123456792',
              14: 'NL00ABCD0123456793',
              15: 'NL00ABCD0123456789',
              16: 'NL00ABCD0123456790',
              17: 'NL00ABCD0123456791',
              18: 'NL00ABCD0123456792',
              19: 'NL00ABCD0123456793'},
             'Balance': {0: 1,
              1: 2,
              2: 2,
              3: 3,
              4: 4,
              5: 2,
              6: 3,
              7: 3,
              8: 4,
              9: 5,
              10: 3,
              11: 4,
              12: 4,
              13: 5,
              14: 6,
              15: 4,
              16: 5,
              17: 5,
              18: 6,
              19: 7}})

px.line(df, x = 'Date', y = 'Balance', color = 'Account')