ncardian commited on
Commit
093725d
·
verified ·
1 Parent(s): 5d4c455

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -21
app.py CHANGED
@@ -16,7 +16,6 @@ import plotly.graph_objects as go
16
  import os
17
 
18
  GROQ_API_KEY = os.environ.get("GROQ_TOKENS")
19
-
20
  st.set_page_config(layout="wide")
21
 
22
  # Custom style CSS
@@ -1175,44 +1174,105 @@ with col2a: # Sidebar untuk chatbot
1175
  print("\\nHasil analisis:")
1176
  print(result)
1177
 
1178
- - Untuk visualisasi WAJIB gunakan Plotly dengan template:
1179
- import plotly.express as px
1180
  # JANGAN gunakan fig.show() atau renderer apapun
1181
- # Contoh bar chart
1182
- fig = px.bar(
1183
- df,
1184
- x='col1',
1185
- y='col2',
1186
- title='Judul Chart',
1187
- color='col3',
1188
- template='plotly_dark'
1189
  )
 
 
 
1190
  fig.update_layout(
1191
- xaxis_title='Label X',
1192
- yaxis_title='Label Y',
1193
- hovermode='x unified'
 
 
 
1194
  )
1195
- fig.show()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1196
 
1197
  # Contoh scatter plot
1198
  fig = px.scatter(
1199
  df,
1200
  x='col1',
1201
  y='col2',
1202
- size='col3',
1203
  color='col4',
1204
  trendline='ols'
1205
  )
1206
 
1207
  # Contoh pie chart
 
 
 
 
 
 
 
 
 
1208
  fig = px.pie(
1209
- df,
1210
- names='col_category',
1211
- values='col_value',
1212
- hole=0.3
 
 
 
 
 
 
 
 
 
 
1213
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
1214
  - Selalu gunakan border untuk chart
1215
- - Gunakan bargap 0.1 untuk bar chart
1216
  - Simpan objek figure sebagai variabel
1217
 
1218
 
 
16
  import os
17
 
18
  GROQ_API_KEY = os.environ.get("GROQ_TOKENS")
 
19
  st.set_page_config(layout="wide")
20
 
21
  # Custom style CSS
 
1174
  print("\\nHasil analisis:")
1175
  print(result)
1176
 
1177
+ - Untuk visualisasi WAJIB gunakan Plotly dengan contoh template:
 
1178
  # JANGAN gunakan fig.show() atau renderer apapun
1179
+ Contoh untuk histogram:
1180
+ fig = px.histogram(
1181
+ df_temp,
1182
+ x=column_name,
1183
+ opacity=0.7,
1184
+ color_discrete_sequence=["#00CC96"],
 
 
1185
  )
1186
+
1187
+ fig.update_traces(marker_line_color='black', marker_line_width=1)
1188
+
1189
  fig.update_layout(
1190
+ template='seaborn',
1191
+ title=f"Histogram of {{column_name}}",
1192
+ xaxis_title=column_name,
1193
+ yaxis_title="Count",
1194
+ bargap=0.1
1195
+ )
1196
  )
1197
+
1198
+ - Berikut adalah contoh bar chart namun pastikan dulu unique value yang akan diplot kurang dari 100
1199
+ top_10_customers['Customer_ID'] = top_10_customers['Customer_ID'].astype(str)
1200
+ # selalu cek dulu jumlah unique value data
1201
+ if top_10_customers['Customer_ID'].unique() < 100:
1202
+ # Plot bar chart
1203
+ fig = px.bar(
1204
+ top_10_customers,
1205
+ x='Customer_ID',
1206
+ y='CLV',
1207
+ color='CLV',
1208
+ template='plotly_dark',
1209
+ title='Top 10 Customers by CLV',
1210
+ category_orders={{"Customer_ID": top_10_customers['Customer_ID'].tolist()}}
1211
+ )
1212
+
1213
+ # Pastikan sumbu X bertipe kategori (bukan numeric)
1214
+ fig.update_xaxes(type='category')
1215
+
1216
+ fig.update_layout(
1217
+ bargap=0.1,
1218
+ xaxis_title='Customer ID',
1219
+ yaxis_title='CLV',
1220
+ hovermode='x unified'
1221
+ )
1222
+
1223
+ Jika if top_10_customers['Customer_ID'].unique() > 100 maka buat bar chart seperti biasa, perinta ini tidak berlaku untuk chart lain seperti pie chart
1224
+
1225
 
1226
  # Contoh scatter plot
1227
  fig = px.scatter(
1228
  df,
1229
  x='col1',
1230
  y='col2',
 
1231
  color='col4',
1232
  trendline='ols'
1233
  )
1234
 
1235
  # Contoh pie chart
1236
+ # Tentukan pull untuk bagian terbesar agar sedikit terpisah
1237
+ feedback_counts = df[feedback_col].value_counts().reset_index()
1238
+ feedback_counts.columns = [feedback_col, 'count']
1239
+
1240
+ # Print feedback counts
1241
+ print("\nFeedback counts:")
1242
+ print(feedback_counts)
1243
+
1244
+ # Create pie chart for feedback
1245
  fig = px.pie(
1246
+ feedback_counts,
1247
+ names=feedback_col,
1248
+ values='count',
1249
+ hole=0.3,
1250
+ title='Feedback Distribution'
1251
+ )
1252
+
1253
+ # Determine the largest segment to pull out
1254
+ max_segment = feedback_counts[feedback_counts['count'] == feedback_counts['count'].max()][feedback_col].values[0]
1255
+ pull = [0.2 if feedback == max_segment else 0 for feedback in feedback_counts[feedback_col]]
1256
+
1257
+ # Update traces for pulling out the largest segment
1258
+ fig.update_traces(
1259
+ pull=pull
1260
  )
1261
+
1262
+ # Update layout for better visualization
1263
+ fig.update_layout(
1264
+ margin=dict(t=50, b=50, l=50, r=50),
1265
+ title_x=0.5
1266
+ )
1267
+
1268
+ # Add border to the chart
1269
+ fig.update_xaxes(showline=True, linewidth=1, linecolor='black', mirror=True)
1270
+ fig.update_yaxes(showline=True, linewidth=1, linecolor='black', mirror=True)
1271
+
1272
+ fig.show()
1273
+
1274
  - Selalu gunakan border untuk chart
1275
+ - Jangan gunakan fig.show()
1276
  - Simpan objek figure sebagai variabel
1277
 
1278