Home > Blockchain >  Trying to create a function to plot multiple line graphs
Trying to create a function to plot multiple line graphs

Time:08-04

So I am trying to create a function that will enable me plot a similar line graph multiple time based on different data without having to cut and paste codes. However, the current code I have doesn't seem to work in anyway.

My table looks this:

https://i.stack.imgur.com/yWqz7.png

The group_type column has different values I would like to plot with different line graphs, but I would not want to just cut and paste the code.

Below is a function I created that doesn't when I reuse, I just get an empty chart.

def plot_this_thing(df_gg_control):
    plt.figure(figsize=(9,5))

    bins = [0, 0.25, 0.5, 0.75, 1, 1.25]
    plt.plot(df_gg_control.trial_end_days, df_gg_control.paid_retention_rate, 'g-')
    
    plt.title('paid retention upgrade rate for the contol group')
    plt.xticks(df_gg_control.trial_end_days[::30])
    plt.yticks(bins)

    plt.xlabel('Distribution of Days (Control Group)')
    plt.ylabel('upgrade rate')

    plt.show()
 

CodePudding user response:

Can't test it without data but this should work:

def plot_this_thing(df_gg_control, title):
    fig, ax = plt.subplots(figsize=(9,5))

    bins = [0, 0.25, 0.5, 0.75, 1, 1.25]
    df_gg_control.groupby('groupe_type').plot('trial_end_days', 'paid_retention_rate', ax=ax)
    
    ax.set_title(title)
    ax.set_xticks(df_gg_control.trial_end_days[::30])
    ax.set_yticks(bins)

    ax.set_xlabel('Distribution of Days (Control Group)')
    ax.set_ylabel('upgrade rate')

    plt.show()

plot_this_thing(df_gg_control, title='paid retention upgrade rate for the contol group')
  • Related