Projects

Test to publish a plot from my Semester Project”Prey dependent survival of M. xanthus under acidic stress” that I did in the Fall Semester 2023 with Plotly.

import pandas as pd
import numpy as np
import plotly.express as px

data = pd.read_csv("pHSavior_CTT_EC.csv")

# Convert to categorical types
data['day'] = pd.Categorical(data['day'])
data['initial_pH'] = pd.Categorical(data['initial_pH'])
data['prey_presence'] = pd.Categorical(np.where(data['treatment'] == "M", 0, 1))
data['myxo_presence'] = pd.Categorical(np.where(data['treatment'] == "E", 0, 1))

data['prey_count'] = data['prey_count'] + 1  # To avoid log(0)
data['swarming_area_log'] = np.log10(data['swarming_area'] + 1)
data['swarming_area_sqrt'] = np.sqrt(data['swarming_area'])
data['prey_count_log'] = np.log10(data['prey_count'] + 1)

# Filter data for day 7 and myxo_presence == 1
dataM7 = data[(data['myxo_presence'] == 1) & (data['day'] == 7)]

#calculate summary statistics
grouped = dataM7.groupby(['day', 'myxo_presence', 'prey_presence', 'initial_pH', 'medium'], observed=True).agg(
    meanSWA=('swarming_area', 'mean'),
    sdSWA=('swarming_area', 'std'),
    n=('swarming_area', 'size')
).reset_index()

# Calculate standard error and confidence intervals, this could maybe be done better but I did it similar to my R Code
grouped['seSWA'] = grouped['sdSWA'] / np.sqrt(grouped['n'])
grouped['ci'] = 1.96 * grouped['seSWA']  # 95% confidence interval
grouped['ci_lower'] = grouped['meanSWA'] - grouped['ci']
grouped['ci_upper'] = grouped['meanSWA'] + grouped['ci']

# Merge the confidence interval data back into the original DataFrame
dataM7 = pd.merge(dataM7, grouped[['prey_presence', 'initial_pH', 'medium', 'ci_lower', 'ci_upper']], 
                  on=['prey_presence', 'initial_pH', 'medium'], how='left')

# Create the faceted plot with error bars
fig = px.scatter(dataM7, x='prey_presence', y='swarming_area', 
                 error_y=dataM7['ci_upper'] - dataM7['swarming_area'], 
                 error_y_minus=dataM7['swarming_area'] - dataM7['ci_lower'],
                 color='prey_presence', facet_col='initial_pH', facet_row='medium', 
                 labels={"swarming_area": "M. xanthus swarming area (mm²)", "prey_presence": "E. coli presence", "initial_pH": "pH"},
                 category_orders={"prey_presence": ["0", "1"]})  # Define order for Prey_presence
#Make X axes categorical
fig.update_xaxes(type='category')
# Update the x-axis tick labels
fig.update_xaxes(tickvals=[0, 1], ticktext=['False', 'True'])
fig.update_layout(
    title='M. xanthus Swarming Area by E. coli Presence, Initial pH, and Medium',
    showlegend=False,
    width=800,   # Adjust width here
    height=600,
    paper_bgcolor="LightGrey"
)
#Modify the Y-axis (to hae only one title instead of two times the same)
fig.update_yaxes(title='')#remove the labels
fig.add_annotation(x=-0.05,y=0.5,
                   text="M. xanthus swarming area (mm²)", textangle=-90,
                    xref="paper", yref="paper")
fig.show()