Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xgboost plot importance figure size

How can I change the figure size of xgboost's plot importance function?

Trying to pass a figsize=(10,20) fails with the exception of unknown attribute.

like image 501
Georg Heiler Avatar asked Oct 17 '16 08:10

Georg Heiler


3 Answers

You can pass an axis in the ax argument in plot_importance(). For instance, use this wrapper:

def my_plot_importance(booster, figsize, **kwargs): 
    from matplotlib import pyplot as plt
    from xgboost import plot_importance
    fig, ax = plt.subplots(1,1,figsize=figsize)
    return plot_importance(booster=booster, ax=ax, **kwargs)

Then use my_plot_importance() instead of plot_importance()

like image 72
AJ Cloete Avatar answered Nov 15 '22 19:11

AJ Cloete


You can also set the figure size with:

from xgboost import plot_importance
import matplotlib.pyplot as plt

plt.rcParams["figure.figsize"] = (14, 7)
plot_importance(booster=booster)
like image 22
Roman Orac Avatar answered Nov 15 '22 19:11

Roman Orac


Only add plt.rcParams["figure.figsize"] = (20,50) to your code

For example:

from xgboost import plot_importance
plt.figure(figsize=(40,20))
plot_importance(model,max_num_features=100)
plt.rcParams["figure.figsize"] = (20,100)
plt.show()

Adjust (20,100) to enlarge or reduce image size

like image 26
Jheel Patel Avatar answered Nov 15 '22 19:11

Jheel Patel