unit8co / darts

A python library for user-friendly forecasting and anomaly detection on time series.
https://unit8co.github.io/darts/
Apache License 2.0
7.96k stars 866 forks source link

Gridlines automatically created in matplotlib and seaborn plots [BUG] #2380

Open ETTAN93 opened 4 months ago

ETTAN93 commented 4 months ago

Darts package seems to have some parameter under the hood that inserts gridlines into every matplotlib and seaborn plot:

If i do not import darts, this seaborn heatmap doesnt show gridlines,

import seaborn as sns
import numpy as np
from lib.data_ingestion.general_utils import pickle_file, load_pickle_file

combined_park_df = load_pickle_file(f'pickle_files/balti2_preprocessed_df.pkl')

overall_corr = combined_park_df[['edm_power_feed_in', 'edm_p_mw', 'power_diff']].corr()
matrix = np.triu(overall_corr)
sns.heatmap(overall_corr, annot = True)

image

However, if I include darts import as part of the imports, gridlines will appear in the plot:

import seaborn as sns
import numpy as np
from lib.data_ingestion.general_utils import pickle_file, load_pickle_file
import darts 

combined_park_df = load_pickle_file(f'pickle_files/balti2_preprocessed_df.pkl')

overall_corr = combined_park_df[['edm_power_feed_in', 'edm_p_mw', 'power_diff']].corr()
matrix = np.triu(overall_corr)
sns.heatmap(overall_corr, annot = True)

image

Is there a reason why it's implemented this way?

dennisbader commented 4 months ago

Hi @ETTAN93, when importing anything from Darts, we change the matplotlib settings/style for plotting. You can deactivate this with environment variable "DARTS_CONFIGURE_MATPLOTLIB"] = "0".

Like below

import os

os.environ["DARTS_CONFIGURE_MATPLOTLIB"] = "0"

import darts

import matplotlib.pyplot as plt
import seaborn as sns

iris = sns.load_dataset('iris')

overall_corr = iris[iris.columns[:4]].corr()
sns.heatmap(overall_corr, annot=True)
plt.show()

There were already some discussion about not changing the style by default (see #924). Maybe we should apply this at some point.

@madtoinou what do you think about this?

madtoinou commented 4 months ago

I agree that not changing the global config would be better, especially if users use Darts in the context of large projects in combination with other plotting libraries.

ETTAN93 commented 3 months ago

Hi @madtoinou and @dennisbader, I'm not sure if this is a related issue but I think it is also related to plotting functions that are provided by Darts. For example, I have set os.environ["DARTS_CONFIGURE_MATPLOTLIB"] = "0" according to the suggested solution but it didn't solve the issues below:

Example 1: plot_residuals_analysis function image

Example 2:

import lightgbm as lgb
lgb_model = lgbm_model.get_estimator(horizon=0, target_dim=0)
imp = lgb_model.booster_.feature_importance()
features_names = lgbm_model.lagged_feature_names
[lgbm_model.lagged_feature_names[idx] for idx in np.argsort(imp)[::-1]]
lgb.plot_importance(lgb_model, importance_type = 'split')

image

The formatting of the diagrams are all over the place.

turbotimon commented 4 weeks ago

@ETTAN93 you may provide a full reproducible example. I think some other library or code is messing with the matplot parameters in your case. At least i can not reproduce it. And if so, it may be better to open a separate issue.

import os
# Make sure to set this env variable BEFORE importing darts!
os.environ.setdefault("DARTS_CONFIGURE_MATPLOTLIB", "0")
import darts
from darts.utils.statistics import plot_residuals_analysis
import numpy as np

ts = darts.TimeSeries.from_values(np.array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]))
plot_residuals_analysis(ts)

image

turbotimon commented 4 weeks ago

I agree that not changing the global config would be better, especially if users use Darts in the context of large projects in combination with other plotting libraries.

@madtoinou @dennisbader I may can work on this, but would need some more information:

I personally would prefer the first one (and only custom plots if its really necessary or helpful. Like e.g. adding a grid). Because it's much easier and less error prone

A third option would be, to at least mention DARTS_CONFIGURE_MATPLOTLIB in the documentation. E.g. under the FAQ. I could do that too