Intervention-Program-Analyst_2 / visualization.py
ProfessorLeVesseur's picture
Create visualization.py
ffd39e7 verified
raw
history blame
3.44 kB
import matplotlib.pyplot as plt
import io
import streamlit as st
class Visualization:
def plot_intervention_statistics(self, intervention_stats):
sessions_held = intervention_stats['Intervention Sessions Held'].values[0]
sessions_not_held = intervention_stats['Intervention Sessions Not Held'].values[0]
fig, ax = plt.subplots()
ax.bar(['Intervention Sessions'], [sessions_held], label='Held', color='#358E66')
ax.bar(['Intervention Sessions'], [sessions_not_held], bottom=[sessions_held], label='Not Held', color='#91D6B8')
ax.text(0, sessions_held / 2, str(sessions_held), ha='center', va='center', color='white', fontweight='bold', fontsize=14)
ax.text(0, sessions_held + sessions_not_held / 2, str(sessions_not_held), ha='center', va='center', color='black', fontweight='bold', fontsize=14)
ax.set_ylabel('Frequency')
ax.set_title('Intervention Sessions Held vs Not Held', fontsize=16)
handles, labels = ax.get_legend_handles_labels()
ax.legend(handles[::-1], labels[::-1])
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
st.pyplot(fig)
return fig
def plot_student_metrics(self, student_metrics_df, attendance_avg_stats, engagement_avg_stats):
fig, ax = plt.subplots(figsize=(10, 6))
bar_width = 0.35
index = range(len(student_metrics_df))
attendance_bars = ax.bar(
[i - bar_width / 2 for i in index],
student_metrics_df['Attendance (%)'],
width=bar_width, label='Attendance (%)',
color='#005288', alpha=0.7
)
engagement_bars = ax.bar(
[i + bar_width / 2 for i in index],
student_metrics_df['Engagement (%)'],
width=bar_width, label='Engagement (%)',
color='#3AB0FF', alpha=0.7
)
for bar in attendance_bars:
height = bar.get_height()
ax.text(bar.get_x() + bar.get_width() / 2, height, f'{height:.0f}%', ha='center', va='bottom', color='black')
for bar in engagement_bars:
height = bar.get_height()
ax.text(bar.get_x() + bar.get_width() / 2, height, f'{height:.0f}%', ha='center', va='bottom', color='black')
ax.axhline(y=attendance_avg_stats, color='#005288', linestyle='--', linewidth=1.5, label=f'Attendance Average: {attendance_avg_stats}%')
ax.axhline(y=engagement_avg_stats, color='#3AB0FF', linestyle='--', linewidth=1.5, label=f'Engagement Average: {engagement_avg_stats}%')
ax.set_xlabel('Student')
ax.set_ylabel('Percentage (%)')
ax.set_title('Student Attendance and Engagement Metrics')
ax.legend(loc='upper right', frameon=False)
ax.set_xticks(index)
ax.set_xticklabels(student_metrics_df['Student'], rotation=0, ha='right')
ax.set_ylim(0, 119)
ax.yaxis.set_ticks(range(0, 119, 20))
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.tight_layout()
st.pyplot(fig)
return fig
def download_chart(self, fig, filename):
buffer = io.BytesIO()
fig.savefig(buffer, format='png')
buffer.seek(0)
st.download_button(label="Download Chart", data=buffer, file_name=filename, mime='image/png', icon="📊", use_container_width=True)