Home > Back-end >  How can I make multiple subplots based on a common date column and each other column in a dataframe?
How can I make multiple subplots based on a common date column and each other column in a dataframe?

Time:02-03

I have this dataframe and I want to create a subplot for each exercise, depicting the sum of sets for each date in a trend line format.

I have tried grouping the dataframe by date and summing the grouped values

plt.figure(figsize=(20, 3))
grouped_data = data.groupby('Date').sum().sort_values(by = 'Date', ascending = True)
grouped_data

I want to depict that result in a trend line format for each exercise.

Thank you.

Screenshot enter image description here

CodePudding user response:

Did you try plt.plot() for plotting?

To make subplots you can use plt.subplot(y, x, crtPlot) where y is the number of subplots on the vertical axis, x is the number of subplots on the horizontal axis and crtPlot is referring to which subplot you are drawing with the last given data. After you input that data, you need to input which data you are plotting, so after each plt.subplot() you need to also call plt.plot().

CodePudding user response:

Try this (example on random data):

data = {'Pushups':[1,4,3,5,9],'Squats':[1,1,2,2,3],'Biceps Curls':[7,2,9,3,2], 'Date':['12-11-2020','12-11-2020','13-11-2020','13-11-2020','14-11-2020']}
df = pd.DataFrame(data=data)
df = df.groupby('Date').sum()

x = np.arange(len(df.index))
for col in df.columns:
    y = df[col]
    plt.scatter(x,y)
    z = np.polyfit(x, y, 1)
    p = np.poly1d(z)
    plt.plot(x,p(x),"--")

plt.show()

If you want only trendlines, just remove plt.scatter lines.

  •  Tags:  
  • Related