diff --git a/microdf/chart_utils.py b/microdf/chart_utils.py index c680e97..c9dbb14 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,14 @@ 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 cab747e..35ba8b7 100644 --- a/microdf/charts.py +++ b/microdf/charts.py @@ -1,7 +1,4 @@ -import matplotlib as mpl -import matplotlib.pyplot as plt import numpy as np -import seaborn as sns import microdf as mdf @@ -19,6 +16,17 @@ def quantile_pct_chg_plot(df1, df2, col1, col2, w1=None, w2=None, q=None): :returns: Axis. """ + 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]'" + ) + if q is None: q = np.arange(0.1, 1, 0.1) # Calculate weighted quantiles. diff --git a/microdf/style.py b/microdf/style.py index 3915bb8..512c6cb 100644 --- a/microdf/style.py +++ b/microdf/style.py @@ -1,8 +1,3 @@ -import matplotlib as mpl -import matplotlib.font_manager as fm -import seaborn as sns - - TITLE_COLOR = "#212121" AXIS_COLOR = "#757575" GRID_COLOR = "#eeeeee" # Previously lighter #f5f5f5. @@ -16,6 +11,16 @@ def set_plot_style(dpi: int = DPI): (200). :type dpi: int, optional """ + 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]'" + ) sns.set_style("white") diff --git a/setup.py b/setup.py index 9a14c87..7a6f0d6 100644 --- a/setup.py +++ b/setup.py @@ -10,12 +10,16 @@ license="MIT", packages=["microdf"], install_requires=[ - "matplotlib", - "matplotlib-label-lines", "numpy", "pandas", - "seaborn", ], - extras_require={"taxcalc": ["taxcalc"]}, + extras_require={ + "taxcalc": ["taxcalc"], + "charts": [ + "seaborn", + "matplotlib", + "matplotlib-label-lines" + ] + }, zip_safe=False, )