Search code examples
pythonchartsplotlyline

Plotly: How to plot a line chart from two rows of data?


I'm trying to do a benchmarking line chart of a login times of a website between two groups of user. My dataframe is as follows:

df:

Group     Jan     Feb     Mar     Apr     May     June
A          12      62     44      34      15       25
B          55      43     42      29      42       33

How can I make a chart with two lines: A and B, with X axis being the months and y axis being the login times? I appreciate any assistance on this. Thanks in advance!


Solution

  • The arguably easiest way to do this is transposing your dataframe using df.T, set the pandas plotly backend to plotly using pd.options.plotting.backend = "plotly", and then just use df.plot(). If you've got your exact data in a pandas dataframe, you only need to use:

    df.set_index('Group').T.plot()
    

    Plot:

    enter image description here

    Complete code with data sample:

    import pandas as pd
    pd.options.plotting.backend = "plotly"
    
    df = pd.DataFrame({'Group': {0: 'A', 1: 'B'},
                         'Jan': {0: 12, 1: 55},
                         'Feb': {0: 62, 1: 43},
                         'Mar': {0: 44, 1: 42},
                         'Apr': {0: 34, 1: 29},
                         'May': {0: 15, 1: 42},
                         'June': {0: 25, 1: 33}}).set_index('Group')
    
    df.T.plot()