import pandas as pd
import numpy as np
import plotly.express as px
= pd.read_csv("pHSavior_CTT_EC.csv")
data
# Convert to categorical types
'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)
data[
# Filter data for day 7 and myxo_presence == 1
= data[(data['myxo_presence'] == 1) & (data['day'] == 7)]
dataM7
#calculate summary statistics
= dataM7.groupby(['day', 'myxo_presence', 'prey_presence', 'initial_pH', 'medium'], observed=True).agg(
grouped =('swarming_area', 'mean'),
meanSWA=('swarming_area', 'std'),
sdSWA=('swarming_area', 'size')
n
).reset_index()
# Calculate standard error and confidence intervals, this could maybe be done better but I did it similar to my R Code
'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']
grouped[
# Merge the confidence interval data back into the original DataFrame
= pd.merge(dataM7, grouped[['prey_presence', 'initial_pH', 'medium', 'ci_lower', 'ci_upper']],
dataM7 =['prey_presence', 'initial_pH', 'medium'], how='left')
on
# Create the faceted plot with error bars
= px.scatter(dataM7, x='prey_presence', y='swarming_area',
fig =dataM7['ci_upper'] - dataM7['swarming_area'],
error_y=dataM7['swarming_area'] - dataM7['ci_lower'],
error_y_minus='prey_presence', facet_col='initial_pH', facet_row='medium',
color={"swarming_area": "M. xanthus swarming area (mm²)", "prey_presence": "E. coli presence", "initial_pH": "pH"},
labels={"prey_presence": ["0", "1"]}) # Define order for Prey_presence
category_orders#Make X axes categorical
type='category')
fig.update_xaxes(# Update the x-axis tick labels
=[0, 1], ticktext=['False', 'True'])
fig.update_xaxes(tickvals
fig.update_layout(='M. xanthus Swarming Area by E. coli Presence, Initial pH, and Medium',
title=False,
showlegend=800, # Adjust width here
width=600,
height="LightGrey"
paper_bgcolor
)#Modify the Y-axis (to hae only one title instead of two times the same)
='')#remove the labels
fig.update_yaxes(title=-0.05,y=0.5,
fig.add_annotation(x="M. xanthus swarming area (mm²)", textangle=-90,
text="paper", yref="paper")
xref fig.show()
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.