ProfessorLeVesseur commited on
Commit
0caf191
·
verified ·
1 Parent(s): 89ef316

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -101
app.py CHANGED
@@ -393,126 +393,49 @@ def compute_student_metrics(df):
393
  student_metrics_df.rename(columns={'index': 'Student'}, inplace=True)
394
  return student_metrics_df
395
 
396
- # def plot_student_metrics(student_metrics_df):
397
- # # Create the figure and axis
398
- # fig, ax = plt.subplots()
399
-
400
- # # Width for the bars and offset for overlapping effect
401
- # bar_width = 0.4
402
- # index = range(len(student_metrics_df))
403
-
404
- # # Plot Attendance and Engagement bars side by side with transparency
405
- # attendance_bars = ax.bar([i - bar_width/2 for i in index], student_metrics_df['Attendance (%)'],
406
- # width=bar_width, label='Attendance (%)', color='#005288', alpha=0.7)
407
- # engagement_bars = ax.bar([i + bar_width/2 for i in index], student_metrics_df['Engagement (%)'],
408
- # width=bar_width, label='Engagement (%)', color='#3AB0FF', alpha=0.7)
409
-
410
- # # Add labels to each bar
411
- # for bar in attendance_bars:
412
- # height = bar.get_height()
413
- # ax.text(bar.get_x() + bar.get_width() / 2, height, f'{height:.1f}%',
414
- # ha='center', va='bottom', color='black')
415
-
416
- # for bar in engagement_bars:
417
- # height = bar.get_height()
418
- # ax.text(bar.get_x() + bar.get_width() / 2, height, f'{height:.1f}%',
419
- # ha='center', va='bottom', color='black')
420
-
421
- # # Set labels, title, and legend
422
- # ax.set_xlabel('Student')
423
- # ax.set_ylabel('Percentage (%)')
424
- # ax.set_title('Student Attendance and Engagement Metrics')
425
- # ax.legend()
426
- # ax.set_xticks(index)
427
- # ax.set_xticklabels(student_metrics_df['Student'], rotation=45)
428
-
429
- # # Display the plot
430
- # st.pyplot(fig)
431
-
432
- import matplotlib.pyplot as plt
433
 
434
  def plot_student_metrics(student_metrics_df):
435
  # Create the figure and axis
436
- fig, ax = plt.subplots(figsize=(10, 6)) # Increased figure size for better readability
437
 
438
  # Width for the bars
439
- bar_width = 0.35
440
- index = range(len(student_metrics_df))
441
-
442
- # Plot Attendance and Engagement bars with transparency
443
- attendance_bars = ax.bar(index, student_metrics_df['Attendance (%)'],
444
- width=bar_width, label='Attendance (%)', color='#005288', alpha=0.7)
445
- engagement_bars = ax.bar(index, student_metrics_df['Engagement (%)'],
446
- width=bar_width * 0.7, label='Engagement (%)', color='#3AB0FF', alpha=0.7) # Skinnier engagement bars
447
-
 
 
 
 
448
  # Add labels to each bar
449
- font_size = 10 # Font size for the labels
450
  for bar in attendance_bars:
451
  height = bar.get_height()
452
- ax.text(bar.get_x() + bar.get_width() / 2, height, f'{height:.1f}%',
453
- ha='center', va='bottom', color='white', fontsize=font_size)
454
 
455
  for bar in engagement_bars:
456
  height = bar.get_height()
457
- ax.text(bar.get_x() + bar.get_width() / 2, height, f'{height:.1f}%',
458
- ha='center', va='bottom', color='black', fontsize=font_size)
459
-
460
  # Set labels, title, and legend
461
  ax.set_xlabel('Student')
462
  ax.set_ylabel('Percentage (%)')
463
  ax.set_title('Student Attendance and Engagement Metrics')
464
  ax.legend()
465
- ax.set_xticks(index)
466
- ax.set_xticklabels(student_metrics_df['Student'], rotation=45, ha='right') # Improved xticklabel alignment
467
-
468
- # Set y-axis limits from 0 to 100%
469
- ax.set_ylim(0, 100)
470
 
471
- # Tight layout to ensure all elements fit within the figure
472
- fig.tight_layout()
473
-
474
  # Display the plot
475
- st.pyplot(fig)
476
-
477
- return fig
478
 
479
- # def plot_student_metrics(student_metrics_df):
480
- # # Create the figure and axis
481
- # fig, ax = plt.subplots()
482
-
483
- # # Width for the bars and offset for overlapping effect
484
- # bar_width = 0.4
485
- # index = range(len(student_metrics_df))
486
-
487
- # # Plot Attendance and Engagement bars with different widths for overlapping
488
- # attendance_bars = ax.bar(index, student_metrics_df['Attendance (%)'],
489
- # width=bar_width, label='Attendance (%)', color='#005288', alpha=0.7)
490
- # engagement_bars = ax.bar(index, student_metrics_df['Engagement (%)'],
491
- # width=bar_width / 2, label='Engagement (%)', color='#3AB0FF', alpha=0.7)
492
-
493
- # # Add values to each bar
494
- # for bar in attendance_bars:
495
- # height = bar.get_height()
496
- # ax.text(bar.get_x() + bar.get_width() / 2, height, f'{height:.1f}%',
497
- # ha='center', va='bottom', color='black')
498
-
499
- # for bar in engagement_bars:
500
- # height = bar.get_height()
501
- # ax.text(bar.get_x() + bar.get_width() / 2, height, f'{height:.1f}%',
502
- # ha='center', va='bottom', color='black')
503
-
504
- # # Set labels, title, and legend
505
- # ax.set_xlabel('Student')
506
- # ax.set_ylabel('Percentage (%)')
507
- # ax.set_title('Student Attendance and Engagement Metrics')
508
- # ax.legend()
509
- # ax.set_xticks(index)
510
- # ax.set_xticklabels(student_metrics_df['Student'], rotation=45)
511
-
512
- # # Display the plot
513
- # st.pyplot(fig)
514
-
515
- # return fig
516
 
517
  def download_chart(fig, filename):
518
  # Create a buffer to hold the image data
 
393
  student_metrics_df.rename(columns={'index': 'Student'}, inplace=True)
394
  return student_metrics_df
395
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
396
 
397
  def plot_student_metrics(student_metrics_df):
398
  # Create the figure and axis
399
+ fig, ax = plt.subplots(figsize=(10, 6)) # Optional: Increased figure size for better readability
400
 
401
  # Width for the bars
402
+ bar_width = 0.35 # Width of the bars
403
+ index = range(len(student_metrics_df)) # Index for each student
404
+
405
+ # Plot Attendance and Engagement bars side by side
406
+ attendance_bars = ax.bar([i - bar_width / 2 for i in index],
407
+ student_metrics_df['Attendance (%)'],
408
+ width=bar_width, label='Attendance (%)',
409
+ color='#005288', alpha=0.7)
410
+ engagement_bars = ax.bar([i + bar_width / 2 for i in index],
411
+ student_metrics_df['Engagement (%)'],
412
+ width=bar_width, label='Engagement (%)',
413
+ color='#3AB0FF', alpha=0.7)
414
+
415
  # Add labels to each bar
 
416
  for bar in attendance_bars:
417
  height = bar.get_height()
418
+ ax.text(bar.get_x() + bar.get_width() / 2, height,
419
+ f'{height:.1f}%', ha='center', va='bottom', color='black')
420
 
421
  for bar in engagement_bars:
422
  height = bar.get_height()
423
+ ax.text(bar.get_x() + bar.get_width() / 2, height,
424
+ f'{height:.1f}%', ha='center', va='bottom', color='black')
425
+
426
  # Set labels, title, and legend
427
  ax.set_xlabel('Student')
428
  ax.set_ylabel('Percentage (%)')
429
  ax.set_title('Student Attendance and Engagement Metrics')
430
  ax.legend()
431
+ ax.set_xticks(index) # Set x-ticks to the index
432
+ ax.set_xticklabels(student_metrics_df['Student'], rotation=45, ha='right') # Set student names as x-tick labels
 
 
 
433
 
 
 
 
434
  # Display the plot
435
+ plt.tight_layout() # Adjust layout to fit elements
436
+ plt.show() # Show the plot in a script environment (use st.pyplot(fig) in Streamlit)
 
437
 
438
+ return fig
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
439
 
440
  def download_chart(fig, filename):
441
  # Create a buffer to hold the image data