25
25
import itertools
26
26
import logging
27
27
28
+ import matplotlib .dates as mdates
28
29
import matplotlib .pyplot as plt
29
30
import pandas as pd
30
31
@@ -344,9 +345,10 @@ def eai_metrics(self, npv: bool = True, **kwargs):
344
345
This computation may become quite expensive for big areas with high resolution.
345
346
346
347
"""
347
- return self ._compute_metrics (
348
+ df = self ._compute_metrics (
348
349
npv = npv , metric_name = "eai" , metric_meth = "calc_eai_gdf" , ** kwargs
349
350
)
351
+ return df
350
352
351
353
def aai_metrics (self , npv : bool = True , ** kwargs ):
352
354
"""Return the average annual impacts for each date.
@@ -567,6 +569,7 @@ def plot_per_date_waterfall(
567
569
ax = None ,
568
570
start_date : datetime .date | None = None ,
569
571
end_date : datetime .date | None = None ,
572
+ figsize = (12 , 6 ),
570
573
):
571
574
"""Plot a waterfall chart of risk components over a specified date range.
572
575
@@ -596,7 +599,9 @@ def plot_per_date_waterfall(
596
599
compared to the risk associated with future exposure and present hazard.
597
600
"""
598
601
if ax is None :
599
- _ , ax = plt .subplots (figsize = (12 , 6 ))
602
+ fig , ax = plt .subplots (figsize = figsize )
603
+ else :
604
+ fig = ax .figure # get parent figure from the axis
600
605
start_date = self .start_date if start_date is None else start_date
601
606
end_date = self .end_date if end_date is None else end_date
602
607
risk_component = self ._calc_waterfall_plot_data (
@@ -611,16 +616,30 @@ def plot_per_date_waterfall(
611
616
"interaction contribution" ,
612
617
]
613
618
]
614
- risk_component .plot (ax = ax , kind = "bar" , stacked = True )
619
+ # risk_component.plot(x="date", ax=ax, kind="bar", stacked=True)
620
+ ax .stackplot (
621
+ risk_component .index ,
622
+ [risk_component [col ] for col in risk_component .columns ],
623
+ labels = risk_component .columns ,
624
+ )
625
+ ax .legend ()
626
+ # bottom = [0] * len(risk_component)
627
+ # for col in risk_component.columns:
628
+ # bottom = [b + v for b, v in zip(bottom, risk_component[col])]
615
629
# Construct y-axis label and title based on parameters
616
630
value_label = "USD"
617
- title_label = (
618
- f"Risk between { start_date } and { end_date } (Annual Average impact)"
619
- )
631
+ title_label = f"Risk between { start_date } and { end_date } (Average impact)"
632
+
633
+ locator = mdates .AutoDateLocator ()
634
+ formatter = mdates .ConciseDateFormatter (locator )
635
+
636
+ ax .xaxis .set_major_locator (locator )
637
+ ax .xaxis .set_major_formatter (formatter )
620
638
621
639
ax .set_title (title_label )
622
640
ax .set_ylabel (value_label )
623
- return ax
641
+ ax .set_ylim (0.0 , 1.1 * ax .get_ylim ()[1 ])
642
+ return fig , ax
624
643
625
644
def plot_waterfall (
626
645
self ,
@@ -722,10 +741,11 @@ def plot_waterfall(
722
741
723
742
# Construct y-axis label and title based on parameters
724
743
value_label = "USD"
725
- title_label = f"Risk at { start_date } and { end_date } (Annual Average impact)"
744
+ title_label = f"Risk at { start_date } and { end_date } (Average impact)"
726
745
727
746
ax .set_title (title_label )
728
747
ax .set_ylabel (value_label )
748
+ ax .set_ylim (0.0 , 1.1 * ax .get_ylim ()[1 ])
729
749
ax .tick_params (
730
750
axis = "x" ,
731
751
labelrotation = 90 ,
0 commit comments