sadickam commited on
Commit
f4ac013
·
verified ·
1 Parent(s): 621e00c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -8
app.py CHANGED
@@ -169,19 +169,25 @@ sdg_colors = {
169
  }
170
 
171
  # Function to plot SDG dominant bar graphs using Plotly
172
- def plot_sdg(df, title, pred_column):
173
- """Plots a bar graph for SDG data using Plotly.
 
 
174
  Args:
175
- df: DataFrame containing SDG predictions.
176
- title: Title of the plot.
177
- pred_column: Column to use for plotting.
 
 
 
 
178
  """
179
  df_filtered = df[df[pred_column].notna()]
180
  labels = df_filtered[pred_column].value_counts().sort_values(ascending=False)
181
  total = labels.sum()
182
  percentages = (labels / total) * 100
183
 
184
- # Create a bar plot with Plotly
185
  fig = px.bar(
186
  percentages.rename_axis('SDG Label').reset_index(name='Percentage'),
187
  y='SDG Label',
@@ -212,9 +218,9 @@ def plot_sdg(df, title, pred_column):
212
  title=None,
213
  tickfont=dict(size=12)
214
  ),
215
- margin=dict(l=20, r=5, t=30, b=20),
216
  height=600,
217
- width=700,
218
  showlegend=False,
219
  template="simple_white",
220
  xaxis=dict(
@@ -222,6 +228,37 @@ def plot_sdg(df, title, pred_column):
222
  ),
223
  )
224
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
225
  return fig
226
 
227
  def save_figure_as_jpeg(fig, filename):
 
169
  }
170
 
171
  # Function to plot SDG dominant bar graphs using Plotly
172
+ def plot_sdg(df, title, pred_column, icons_folder='assets/icons/'):
173
+ """
174
+ Plots a horizontal bar graph of SDG predictions and superimposes the icon of the most frequent SDG.
175
+
176
  Args:
177
+ df (pd.DataFrame): DataFrame containing SDG predictions.
178
+ title (str): Title of the plot.
179
+ pred_column (str): Column name to use for plotting (e.g., 'pred1').
180
+ icons_folder (str): Path to the folder containing SDG icons.
181
+
182
+ Returns:
183
+ plotly.graph_objs._figure.Figure: The Plotly figure object.
184
  """
185
  df_filtered = df[df[pred_column].notna()]
186
  labels = df_filtered[pred_column].value_counts().sort_values(ascending=False)
187
  total = labels.sum()
188
  percentages = (labels / total) * 100
189
 
190
+ # Create a horizontal bar plot with Plotly
191
  fig = px.bar(
192
  percentages.rename_axis('SDG Label').reset_index(name='Percentage'),
193
  y='SDG Label',
 
218
  title=None,
219
  tickfont=dict(size=12)
220
  ),
221
+ margin=dict(l=20, r=150, t=30, b=20), # Increased right margin for icon
222
  height=600,
223
+ width=800, # Increased width to accommodate the icon
224
  showlegend=False,
225
  template="simple_white",
226
  xaxis=dict(
 
228
  ),
229
  )
230
 
231
+ # Identify the most frequent SDG
232
+ if not percentages.empty:
233
+ top_sdg_label = percentages.index[0] # e.g., 'SDG1_No Poverty'
234
+
235
+ # Map SDG label to icon filename
236
+ # Assuming naming convention 'SDG1.png', 'SDG2.png', etc.
237
+ sdg_number = top_sdg_label.split('_')[0] # Extract 'SDG1'
238
+ icon_filename = f"{sdg_number}.png" # e.g., 'SDG1.png'
239
+ icon_path = os.path.join(icons_folder, icon_filename)
240
+
241
+ # Check if the icon file exists
242
+ if os.path.exists(icon_path):
243
+ # Read and encode the image
244
+ with open(icon_path, 'rb') as image_file:
245
+ encoded_image = base64.b64encode(image_file.read()).decode('utf-8')
246
+
247
+ # Add the icon as an image in the Plotly figure
248
+ fig.add_layout_image(
249
+ dict(
250
+ source='data:image/png;base64,' + encoded_image,
251
+ xref="paper", yref="paper",
252
+ x=1.05, y=1, # Positioning: slightly to the right and top
253
+ sizex=0.2, sizey=0.2, # Size of the icon
254
+ xanchor="left",
255
+ yanchor="top",
256
+ layer="above" # Ensure the icon is above other plot elements
257
+ )
258
+ )
259
+ else:
260
+ print(f"Icon file '{icon_path}' not found. Skipping icon overlay.")
261
+
262
  return fig
263
 
264
  def save_figure_as_jpeg(fig, filename):