krishaamer commited on
Commit
3c1c755
·
1 Parent(s): 32173b7

Add treemap

Browse files
Files changed (1) hide show
  1. page_personas.py +40 -1
page_personas.py CHANGED
@@ -4,6 +4,7 @@ import numpy as np
4
  import textwrap
5
  import matplotlib.pyplot as plt
6
  import seaborn as sns
 
7
  from adjustText import adjust_text
8
  from wordcloud import WordCloud
9
  from sklearn.decomposition import PCA
@@ -63,6 +64,9 @@ def show(df):
63
  f"<h2 style='text-align: center;'>Mean Answer Scores</h2>", unsafe_allow_html=True)
64
  get_kmeans_table(df)
65
  show_clustering_heatmap(df, chinese_font)
 
 
 
66
 
67
  def plot_loadings_for_cluster(cluster_id, df_cluster, cluster_names, chinese_font, num_components=2, num_top_features=30):
68
 
@@ -372,4 +376,39 @@ def add_border(img_array, color, width):
372
  border_img_array[:, :width, :] = color
373
  border_img_array[:, -width:, :] = color
374
  border_img_array[width:-width, width:-width, :] = img_array
375
- return border_img_array
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  import textwrap
5
  import matplotlib.pyplot as plt
6
  import seaborn as sns
7
+ import squarify
8
  from adjustText import adjust_text
9
  from wordcloud import WordCloud
10
  from sklearn.decomposition import PCA
 
64
  f"<h2 style='text-align: center;'>Mean Answer Scores</h2>", unsafe_allow_html=True)
65
  get_kmeans_table(df)
66
  show_clustering_heatmap(df, chinese_font)
67
+ st.markdown(
68
+ f"<h2 style='text-align: center;'>Agreement between personas</h2>", unsafe_allow_html=True)
69
+ create_treemap()
70
 
71
  def plot_loadings_for_cluster(cluster_id, df_cluster, cluster_names, chinese_font, num_components=2, num_top_features=30):
72
 
 
376
  border_img_array[:, :width, :] = color
377
  border_img_array[:, -width:, :] = color
378
  border_img_array[width:-width, width:-width, :] = img_array
379
+ return border_img_array
380
+
381
+ def create_treemap():
382
+ categories = {
383
+ 'Ethical Consumption and Labor Concerns': 3.2,
384
+ 'Environmental Awareness and Action': 3.8,
385
+ 'Economic Views and Sustainability': 2.9,
386
+ 'Personal Finance and Investment': 3.5,
387
+ 'Technology and AI Engagement': 3.1,
388
+ 'Health and Safety Concerns': 4.2,
389
+ 'Climate and Pollution Concerns': 4.0
390
+ }
391
+
392
+ # Create sizes for the rectangles based on the average agreement levels
393
+ sizes = [value for value in categories.values()]
394
+
395
+ # Create labels for the rectangles with the category name and the size
396
+ labels = [f'{key}\n({value:.2f})' for key, value in categories.items()]
397
+
398
+ # Choose colors for each category
399
+ colors = [plt.cm.Spectral(i/float(len(labels))) for i in range(len(labels))]
400
+
401
+ # Create a figure and a set of subplots
402
+ fig, ax = plt.subplots(figsize=(12, 8))
403
+
404
+ # Create the treemap on the created axes ax
405
+ squarify.plot(sizes=sizes, label=labels, color=colors, alpha=0.8, text_kwargs={'fontsize':9}, ax=ax)
406
+
407
+ # Remove the axis for a cleaner look
408
+ ax.axis('off')
409
+
410
+ # Add a title to the plot
411
+ plt.title('Average Agreement Level by Question Category', fontsize=15)
412
+
413
+ # Use the figure object (fig) in st.pyplot() to display the plot
414
+ st.pyplot(fig)