ClarzW commited on
Commit
5eeb710
·
verified ·
1 Parent(s): cf5b450

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -19
app.py CHANGED
@@ -136,29 +136,24 @@ if viz_button:
136
 
137
  # Expenses Over Time Line Chart with Title, Labels, and Point Labels
138
  st.subheader("Expenses Over Time")
139
-
140
 
 
141
 
142
- # Create a new DataFrame for monthly aggregation
143
- monthly_expenses = st.session_state['expenses'].copy()
144
- monthly_expenses['Date'] = pd.to_datetime(monthly_expenses['Date'], errors='coerce')
145
- monthly_expenses['Month_Year'] = monthly_expenses['Date'].dt.to_period('M')
146
- monthly_summary = monthly_expenses.groupby('Month_Year')['Amount'].sum().reset_index()
147
- monthly_summary['Month_Year'] = monthly_summary['Month_Year'].dt.to_timestamp() # Convert back to timestamp for plotting
148
 
149
- fig, ax = plt.subplots(figsize=(10,6))
150
- ax.plot(monthly_summary['Month_Year'], monthly_summary['Amount'], marker='o')
151
- ax.set_title("Monthly Expenses Over Time", pad=20) # Title for the line chart
152
- ax.set_ylabel("Amount Spent (£)", labelpad=15) # Y-axis label
153
-
154
- # Formatting the x-axis to show Month-Year
155
- ax.xaxis.set_major_formatter(mdates.DateFormatter('%b %Y'))
156
- plt.xticks(rotation=45)
157
-
158
- # Adding labels to each point
159
- for date, amount in zip(monthly_summary['Month_Year'], monthly_summary['Amount']):
160
- ax.text(date, amount + 0.05, f'{amount:.2f}', ha='right')
161
 
 
 
 
 
 
 
162
 
163
  st.pyplot(fig)
164
 
 
136
 
137
  # Expenses Over Time Line Chart with Title, Labels, and Point Labels
138
  st.subheader("Expenses Over Time")
 
139
 
140
+ monthly_df = st.session_state['expenses'].copy()
141
 
142
+ monthly_df['Date'] = pd.to_datetime(monthly_df['Date'])
143
+
144
+ # Extract month and year for grouping
145
+ monthly_df['Month'] = monthly_df['Date'].dt.to_period('M')
 
 
146
 
147
+ # Group by the new "Month" column and aggregate the "Amount"
148
+ monthly_expenses = monthly_df.groupby('Month')['Amount'].sum().reset_index()
149
+
 
 
 
 
 
 
 
 
 
150
 
151
+ plt.plot(monthly_expenses['Month'], monthly_expenses['Amount'], marker='o')
152
+ plt.title('Monthly Expenses')
153
+ plt.xlabel('Month')
154
+ plt.ylabel('Amount')
155
+ plt.grid(True)
156
+ plt.show()
157
 
158
  st.pyplot(fig)
159