Error band using plt.fill_between is giving two areas instead of one

23 Views Asked by At

I am trying to process some data for cyclic compression testing and I have a pre-test and reload curve for three different test groups. I am using plt.fill_between to generate an error band, which has worked with other graphs but with cyclic data it does not seem to work.

this is the code i am using:

def process_data(directory_path, specimens, phase):
    strains = []
    stresses = []

    for specimen in specimens:
        filepath = glob.glob(f"{directory_path}/Specimen_RawData_{specimen}.csv")[0]
        df = pd.read_csv(filepath, skiprows=5, usecols=['(mm/mm)', '(kPa)', '(s)'])

        if phase == 'Pre-test':
            data = df[(df['(s)'] >= 0) & (df['(s)'] <= 30)]
        elif phase == 'Reload':
            data = df[(df['(s)'] > 30) & (df['(s)'] <= 60)]

        strains.append(data['(mm/mm)'])
        stresses.append(data['(kPa)'])

    strain_df = pd.DataFrame(strains).transpose()
    stress_df = pd.DataFrame(stresses).transpose()

    avg_strain = strain_df.mean(axis=1)
    avg_stress = stress_df.mean(axis=1)
    std_stress = stress_df.std(axis=1)

    return avg_strain, avg_stress, std_stress


def plot_group_data(directory_path, group_specimens, group_labels, colors, phase):
    plt.figure(figsize=(12, 8))

    for specimens, label, color in zip(group_specimens, group_labels, colors):
        avg_strain, avg_stress, std_stress = process_data(directory_path, specimens, phase)
        plt.plot(avg_strain, avg_stress, label=f'Avg {label} - {phase}', color=color, linewidth=2)
        plt.fill_between(avg_strain ,avg_stress - std_stress, avg_stress + std_stress, color=color, alpha=0.1)


    plt.xlabel('Strain (mm/mm)')
    plt.ylabel('Stress (kPa)')
    plt.title(f'Combined Groups - {phase}')
    plt.legend()
    plt.show()

and am calling the function:

# Plot data for each phase
group_specimens = [group1_specimens, group2_specimens, group3_specimens]
group_labels = ['Group 1', 'Group 2', 'Group 3']
colors = ['blue', 'green', 'red']  # Define different colors for each group

plot_group_data(directory_path, group_specimens, group_labels, colors, 'Pre-test')
plot_group_data(directory_path, group_specimens, group_labels, colors, 'Reload')

The error bands are incorrect, should be symmetrical about the cyclic curve:

the error bands are incorrect, should be symmetrical about the cyclic curve

This is how it should look:

this is how it should look

0

There are 0 best solutions below