0

I want to use pycaret's plot_model to save a plot image, however I don't want to display the plot. I'm looping through many figures and models and therefore displaying all the plots is resulting in a memory.

Ideally I'm looking for something like this, but I don't see any way to suppress the displaying of the plot:

plot_image = plot_model(plot='ts', return_fig=True, display_fig=False)

I tried setting verbose=False but it didn't do anything

1 Answers1

1

You can use save parameter to save a plot then PyCaret will not display any plot. You can set save=True to save a plot to the current directory or you can set save={destination_dir} to save a plot to your destination directory.

from pycaret.datasets import get_data
from pycaret.time_series import TSForecastingExperiment
import pathlib

# Get sample data
y = get_data('airline', verbose=False)

# Experiment
exp = TSForecastingExperiment()
exp.setup(data=y, fh=12, session_id=42)
top3 = exp.compare_models(n_select=3, turbo=True)

# Get all plots
all_plots = list(exp.get_config('_available_plots').keys())
base_dir = 'D:\\Python\\figures'

for model in top3:
    # Get model name
    model_name = type(model).__name__
    save_dir = f'{base_dir}\\{model_name}'
    # Create a directory for each model
    pathlib.Path(save_dir).mkdir(exist_ok=True)
    
    for plot in all_plots:
        try:
            # Save each plot of each model to desired directory
            exp.plot_model(model, plot=plot, save=save_dir)
        except Exception as e:
            print(f'{model_name} has error in {plot} plot')
Tatchai S.
  • 315
  • 1
  • 3
  • 9