File size: 2,517 Bytes
7087cc8
 
 
408537b
b43f093
3d271a9
 
7087cc8
2167a2b
7963821
2167a2b
5d29fa6
 
7087cc8
408537b
 
 
2167a2b
7087cc8
be3d365
 
 
 
 
 
7087cc8
be3d365
 
5a58459
 
408537b
be3d365
 
2167a2b
 
408537b
be3d365
 
 
 
e277b73
 
 
be3d365
 
 
2167a2b
fe95227
408537b
be3d365
408537b
 
2167a2b
 
be3d365
 
7087cc8
be3d365
 
 
7087cc8
e277b73
2167a2b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import streamlit as st
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.font_manager import FontProperties
from fields.likert_fields import likert_fields
from fields.field_translation_mapping import field_translation_mapping
from fields.translation_mapping import translation_mapping


@st.cache_data
def show(df):
    st.title("Students Attitudes (Overall)")
    st.write("Students Attitudes across all Likert fields without clustering")

    # Chinese font
    chinese_font = FontProperties(fname='mingliu.ttf')

    if df is not None:

        # Rename the columns in the DataFrame for visualization
        df_translated = df.rename(columns={
            field: f"{field} ({field_translation_mapping[category][i]})"
            for category, fields in likert_fields.items()
            for i, field in enumerate(fields)
        })

        # Loop through each category in likert_fields to create visualizations
        for category, fields in likert_fields.items():
            st.markdown(
                f"<h2 style='text-align: center;'>{translation_mapping[category]}</h2>", unsafe_allow_html=True)

            # Calculate the number of rows needed for this category
            num_fields = len(fields)
            # Equivalent to ceil(num_fields / 2)
            num_rows = -(-num_fields // 2)

            # Create subplots with 2 columns for this category
            fig, axs = plt.subplots(num_rows, 2, figsize=(15, 5 * num_rows))
            axs = axs.flatten()  # Flatten the array of subplots

            # Add padding to fit in the Chinese titles
            plt.subplots_adjust(hspace=0.4)

            # Loop through each field in the category to create individual bar plots
            for i, field in enumerate(fields):
                # Create the bar plot
                sns.countplot(
                    x=f"{field} ({field_translation_mapping[category][i]})", data=df_translated, ax=axs[i], palette="coolwarm")

                # Add title and labels
                title_chinese = field
                title_english = field_translation_mapping[category][i]
                axs[i].set_title(
                    f"{title_chinese}\n{title_english}", fontproperties=chinese_font)
                axs[i].set_xlabel('Likert Scale')
                axs[i].set_ylabel('Frequency')

            # Remove any unused subplots
            for i in range(num_fields, num_rows * 2):
                fig.delaxes(axs[i])

            # Show the plot
            st.pyplot(fig)