Search code examples
pythonmatplotlibplot

Adding a dashed line of a different dataset to an existing plot


I've got that dataset which I used to plot the concentration against some growth measurements. However, now I want to add a dashed line with values of another dataset. When I print the value, it prints 0.25 which is what I want but on the plot the line is showing a bit after my concentration of 0.03 and before 0.06. I feel like my dashed line is not taking in account the concentration values. Do you have an explanation?

df_filtered['CONC'] = pd.to_numeric(df_filtered['CONC'], errors='coerce')
df_filtered = df_filtered.dropna(subset=['CONC'])

df_filtered = df_filtered[df_filtered['CFZ_BINARY_PHENOTYPE'].isin(['S', 'R'])]

colors = {'S': 'red', 'R': 'blue'}

sns.boxplot(data=df_filtered, x='CONC', y='growth_in_pixels', hue='CFZ_BINARY_PHENOTYPE', palette=colors)

plt.xlabel('Concentration')
plt.ylabel('Growth in Pixels')
plt.title('Boxplot of Growth in Pixels for CFZ')

plt.xticks(rotation=45)

# Add ECOFF line for CFZ

ecoff_value_cfz = df_ECOFF.loc[df_ECOFF['DRUG_ID'] == 'CFZ', 'ECOFF'].values[0]
print(ecoff_value_cfz)

plt.axvline(x=ecoff_value_cfz, color='red', linestyle='--', label=f"ECOFF (CFZ): {ecoff_value_cfz}")

To see the plot


Solution

  • What about doing something like this:

    import pandas as pd
    import matplotlib.pyplot as plt
    import seaborn as sns
    
    data = {
        'CONC': [0.01, 0.02, 0.03, 0.03, 0.06, 0.06, 0.06, 0.25, 0.25],
        'growth_in_pixels': [20, 30, 40, 50, 60, 70, 80, 90, 100],
        'CFZ_BINARY_PHENOTYPE': ['S', 'S', 'R', 'R', 'S', 'S', 'R', 'R', 'S']
    }
    df_filtered = pd.DataFrame(data)
    
    ecoff_data = {
        'DRUG_ID': ['CFZ', 'Another'],
        'ECOFF': [0.25, 0.5]
    }
    df_ECOFF = pd.DataFrame(ecoff_data)
    
    
    df_filtered['CONC'] = pd.to_numeric(df_filtered['CONC'], errors='coerce')
    df_filtered = df_filtered.dropna(subset=['CONC'])
    df_filtered = df_filtered[df_filtered['CFZ_BINARY_PHENOTYPE'].isin(['S', 'R'])]
    
    colors = {'S': 'red', 'R': 'blue'}
    sns.boxplot(data=df_filtered, x='CONC', y='growth_in_pixels', hue='CFZ_BINARY_PHENOTYPE', palette=colors)
    
    plt.xlabel('Concentration')
    plt.ylabel('Growth in Pixels')
    plt.title('Boxplot of Growth in Pixels for CFZ')
    plt.xticks(rotation=45)
    
    ecoff_value_cfz = df_ECOFF.loc[df_ECOFF['DRUG_ID'] == 'CFZ', 'ECOFF'].values[0] 
    
    unique_concs = sorted(df_filtered['CONC'].unique())
    position = unique_concs.index(ecoff_value_cfz)  
    
    plt.axvline(x=position, color='red', linestyle='--', label=f"ECOFF (CFZ): {ecoff_value_cfz}")
    plt.legend()
    
    plt.show()
    

    which gives

    enter image description here