11"""
22visualizer.py
33Generates supply chain performance charts for management reporting.
4+ Updated for Streamlit Web Rendering.
45"""
56import matplotlib .pyplot as plt
67import pandas as pd
78
89def plot_scenario_comparison (df_baseline : pd .DataFrame , df_scenario : pd .DataFrame , scenario_name : str ):
910 """
1011 Saves a comparison chart of Baseline vs. Scenario Demand.
12+ (Kept as-is for backward compatibility with main.py)
1113 """
1214 plt .figure (figsize = (10 , 6 ))
1315
@@ -33,28 +35,29 @@ def plot_scenario_comparison(df_baseline: pd.DataFrame, df_scenario: pd.DataFram
3335def plot_forecast (df_history : pd .DataFrame , forecast_values : pd .Series ):
3436 """
3537 Plots historical data vs. predicted future demand.
38+ RETURNS the figure object for Streamlit rendering.
3639 """
37- plt .figure (figsize = (12 , 6 ))
40+ # CHANGE 1: Create figure and axes explicitly using subplots
41+ fig , ax = plt .subplots (figsize = (12 , 6 ))
3842
3943 # 1. Plot History (Solid Blue Line)
40- plt .plot (df_history .index , df_history ['demand' ],
44+ # CHANGE 2: Use 'ax.plot' instead of 'plt.plot'
45+ ax .plot (df_history .index , df_history ['demand' ],
4146 marker = 'o' , linestyle = '-' , color = '#0078D4' , label = 'Historical Data' )
4247
4348 # 2. Plot Forecast (Dashed Green Line)
44- # Create an index for the future (12, 13, 14...)
4549 last_index = df_history .index [- 1 ]
4650 future_indices = range (last_index + 1 , last_index + 1 + len (forecast_values ))
4751
48- plt .plot (future_indices , forecast_values ,
52+ ax .plot (future_indices , forecast_values ,
4953 marker = 'x' , linestyle = '--' , color = '#107C10' , label = 'Forecast (AI Prediction)' )
5054
51- plt .title ('Demand Forecast: Next Quarter Prediction' , fontsize = 14 )
52- plt .xlabel ('Time Period (Month Index)' , fontsize = 12 )
53- plt .ylabel ('Demand' , fontsize = 12 )
54- plt .grid (True , linestyle = ':' , alpha = 0.6 )
55- plt .legend ()
55+ # CHANGE 3: Use 'ax.set_title' instead of 'plt.title'
56+ ax .set_title ('Demand Forecast: Next Quarter Prediction' , fontsize = 14 )
57+ ax .set_xlabel ('Time Period (Month Index)' , fontsize = 12 )
58+ ax .set_ylabel ('Demand' , fontsize = 12 )
59+ ax .grid (True , linestyle = ':' , alpha = 0.6 )
60+ ax .legend ()
5661
57- output_file = 'forecast_chart.png'
58- plt .savefig (output_file )
59- print (f"🔮 Forecast chart saved as '{ output_file } '" )
60- plt .close ()
62+ # CHANGE 4: Return 'fig' instead of saving/closing
63+ return fig
0 commit comments