KIMIA Ghassemzadeh
KIMIA Ghassemzadeh

Reputation: 23

plotting multiple lines in one line plot

I have a dataframe that hast 3 columns. I made it up from a bigger dataframe like this :

new_df = df[['client_name', 'time_window_end', 'tag_count']]

then I used groupby to find out the number of tags for each client in each day using this code :

new_df.groupby(['client_name' ,'time_window_end']) ['tag_count'].count()

I totally have 70 client names in a list an I want to loop through my list to plot a line plot for each costumer name. in the x axis I want to have 'time_window_end' and in the y axis I want to have 'tag_count'. I want 70 plot but the for loop that I have written does not do that. I would be happy if you could help me to fix it.

clients = new_df['client_name'].unique()
client_list = clients.tolist()


for client in client_list[:60]:
  temp = new_df.loc[new_df['client_name'] == client]
  x = temp.groupby(temp['time_window_end'].dt.floor('d'))['tag_count'].sum()
  df2 = x.to_frame()
  df2.reset_index(inplace=True)
  df2["time_window_end"]= pd.to_datetime(df2["time_window_end"])
  line_chart = df2.copy()
  plt.plot(line_chart.reset_index()["time_window_end"], x)

Upvotes: 0

Views: 489

Answers (1)

Connor
Connor

Reputation: 91

If I'm understanding this right, it sounds like the seaborn package might have what you need. The plotting functions take the argument 'hue' which splits plots up into multiple lines, based on the data in a column

import seaborn as sn

new_df = new_df.groupby(['client_name' ,'time_window_end']) ['tag_count'].count().reset_index()

sn.relplot(
    data = new_df, 
    x = pd.to_datetime(new_df["time_window_end"]), 
    y = 'tag_count', 
    hue = 'client_name', 
    kind = 'line')

EDIT: to get multiple plots


import seaborn as sn

new_df["time_window_end"] = pd.to_datetime(new_df["time_window_end"])

g = sn.FacetGrid(
    data = new_df,
    row = 'client_name')

g.map(sn.lineplot, 'time_window_end', 'tag_count')

EDIT again: to get separate plot images

import matplotlib.pyplot as plt

for name in pd.unique(new_df.client_names):
   sn.lineplot(
      data = new_df.loc[new_df.client_names == name],
      x = 'time_window_end',
      y = 'tag_count',
      label = name)
   plt.show()

Upvotes: 1

Related Questions