From 65455ecfd2136f6aedb721cf016db82162bb532d Mon Sep 17 00:00:00 2001 From: Anthony Volk Date: Thu, 24 Oct 2024 23:22:43 +0200 Subject: [PATCH] fix: Conditionally import matplotlib --- microdf/chart_utils.py | 9 ++++++--- microdf/charts.py | 4 ++-- microdf/style.py | 6 ++---- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/microdf/chart_utils.py b/microdf/chart_utils.py index c680e97..e8196fc 100644 --- a/microdf/chart_utils.py +++ b/microdf/chart_utils.py @@ -1,6 +1,3 @@ -import matplotlib as mpl - - def dollar_format(suffix=""): """Dollar formatter for matplotlib. @@ -19,6 +16,12 @@ def currency_format(currency="USD", suffix=""): :returns: FuncFormatter. """ + try: + import matplotlib as mpl + except ImportError: + raise ImportError( + "The function you've called requires extra dependencies. Please install microdf with the 'charts' extra by running 'pip install microdf[charts]'" + ) prefix = {"USD": "$", "GBP": "£"}[currency] diff --git a/microdf/charts.py b/microdf/charts.py index 49a97c2..891065b 100644 --- a/microdf/charts.py +++ b/microdf/charts.py @@ -1,5 +1,3 @@ -import matplotlib as mpl -import matplotlib.pyplot as plt import numpy as np import microdf as mdf @@ -20,6 +18,8 @@ def quantile_pct_chg_plot(df1, df2, col1, col2, w1=None, w2=None, q=None): """ try: import seaborn as sns + import matplotlib as mpl + import matplotlib.pyplot as plt except ImportError: raise ImportError( "The function you've called requires extra dependencies. Please install microdf with the 'charts' extra by running 'pip install microdf[charts]'" diff --git a/microdf/style.py b/microdf/style.py index 0729e45..a3a1030 100644 --- a/microdf/style.py +++ b/microdf/style.py @@ -1,7 +1,3 @@ -import matplotlib as mpl -import matplotlib.font_manager as fm - - TITLE_COLOR = "#212121" AXIS_COLOR = "#757575" GRID_COLOR = "#eeeeee" # Previously lighter #f5f5f5. @@ -17,6 +13,8 @@ def set_plot_style(dpi: int = DPI): """ try: import seaborn as sns + import matplotlib as mpl + import matplotlib.font_manager as fm except ImportError: raise ImportError( "The function you've called requires extra dependencies. Please install microdf with the 'charts' extra by running 'pip install microdf[charts]'"