3

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!

vestland
  • 55,229
  • 37
  • 187
  • 305
Matthias Gallagher
  • 475
  • 1
  • 7
  • 20
  • Hi! I'd be happy to help, but your question does not exactly meet many of the SO standards. Please take a look at [ask] a question, and tell us what you have tried. Also take a look at how to [share your data sample as a pandas dataframe](https://stackoverflow.com/questions/63163251/pandas-how-to-easily-share-a-sample-dataframe-using-df-to-dict/63163254#63163254). – vestland Aug 31 '20 at 09:09

1 Answers1

4

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()
vestland
  • 55,229
  • 37
  • 187
  • 305
  • 3
    This answer works really well. For those of you wondering, `df.T.plot()` returns a plotly `Figure` object, which can be operated on like any other `plotly.figure.Figure`. As an example, a title could be added as: `fig=df.T.plot(); fig.update_layout(title="Months")` – Parmandeep Chaddha Mar 31 '21 at 17:28