From 933f3eba0aec081d8d13f032ad09695591e5d5bf Mon Sep 17 00:00:00 2001 From: Jamie Date: Fri, 30 Apr 2021 15:59:37 -0400 Subject: [PATCH] change where mpl backend import is changed --- gamestonk_terminal/backtesting/bt_controller.py | 4 ++++ gamestonk_terminal/backtesting/bt_helper.py | 12 +----------- gamestonk_terminal/config_plot.py | 4 ++-- gamestonk_terminal/helper_funcs.py | 3 +++ 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/gamestonk_terminal/backtesting/bt_controller.py b/gamestonk_terminal/backtesting/bt_controller.py index 032129c89f9..efeaefd6226 100644 --- a/gamestonk_terminal/backtesting/bt_controller.py +++ b/gamestonk_terminal/backtesting/bt_controller.py @@ -11,10 +11,14 @@ from gamestonk_terminal import feature_flags as gtff from gamestonk_terminal.helper_funcs import get_flair from gamestonk_terminal.menu import session +# This code below aims to fix an issue with the fnn module, used by bt module +# which forces matplotlib backend to be 'agg' which doesn't allow to plot +# Save current matplotlib backend default_backend = mpl.get_backend() # pylint: disable=wrong-import-position from gamestonk_terminal.backtesting import bt_view # noqa: E402 +# Restore backend matplotlib used mpl.use(default_backend) diff --git a/gamestonk_terminal/backtesting/bt_helper.py b/gamestonk_terminal/backtesting/bt_helper.py index 6f7dde84fb7..41fdbc09f29 100644 --- a/gamestonk_terminal/backtesting/bt_helper.py +++ b/gamestonk_terminal/backtesting/bt_helper.py @@ -9,21 +9,11 @@ from matplotlib import pyplot as plt from pandas.plotting import register_matplotlib_converters import bt from gamestonk_terminal.helper_funcs import plot_autoscale -from gamestonk_terminal.config_plot import PLOT_DPI, backend +from gamestonk_terminal.config_plot import PLOT_DPI from gamestonk_terminal import feature_flags as gtff register_matplotlib_converters() -if backend is not None: - print(f"Setting matplotlib backend to {backend}") - matplotlib.use(backend) - -if matplotlib.get_backend() in ["Agg", "MacOSX"]: - print( - "You may encounter errors due to your MatplotLib backend. " - " Please configure in config_plot.py." - ) - def buy_and_hold(ticker: str, start: Union[str, datetime], name: str): """ diff --git a/gamestonk_terminal/config_plot.py b/gamestonk_terminal/config_plot.py index 002a93b0919..a38c987f559 100644 --- a/gamestonk_terminal/config_plot.py +++ b/gamestonk_terminal/config_plot.py @@ -1,11 +1,11 @@ PLOT_DPI = 100 # Backend to use for plotting -backend = None +BACKEND = None # Examples: +# "tkAgg" - This uses the tkinter library. If unsure, set to this # "module://backend_interagg" - This is what pycharm defaults to in Scientific Mode # "MacOSX" - Mac default. Does not work with backtesting -# "tkAgg" - This uses the tkinter library. # "Qt5Agg" - This requires the PyQt5 package is installed # See more: https://matplotlib.org/stable/tutorials/introductory/usage.html#the-builtin-backends diff --git a/gamestonk_terminal/helper_funcs.py b/gamestonk_terminal/helper_funcs.py index 49d4171bb3f..05e575dca0b 100644 --- a/gamestonk_terminal/helper_funcs.py +++ b/gamestonk_terminal/helper_funcs.py @@ -6,6 +6,7 @@ import re import sys from pytz import timezone import iso8601 +import matplotlib import matplotlib.pyplot as plt from holidays import US as holidaysUS from colorama import Fore, Style @@ -17,6 +18,8 @@ from gamestonk_terminal import feature_flags as gtff from gamestonk_terminal import config_plot as cfgPlot register_matplotlib_converters() +if cfgPlot.BACKEND is not None: + matplotlib.use(cfgPlot.BACKEND) def check_non_negative(value) -> int: