Advanced usage using matplotlib

As we mentioned in the previous section, using the functional interface provides great flexibility to evaluate your models, this sections includes some recipes for common tasks that involve the use of the matplotlib API.

Changing plot style

sklearn-evaluation uses whatever configuration matplotlib has, if you want to change the style of the plots easily you can use one of the many styles available:

In [1]: import matplotlib.style

In [2]: matplotlib.style.available
Out[2]: 
['Solarize_Light2',
 '_classic_test_patch',
 '_mpl-gallery',
 '_mpl-gallery-nogrid',
 'bmh',
 'classic',
 'dark_background',
 'fast',
 'fivethirtyeight',
 'ggplot',
 'grayscale',
 'seaborn',
 'seaborn-bright',
 'seaborn-colorblind',
 'seaborn-dark',
 'seaborn-dark-palette',
 'seaborn-darkgrid',
 'seaborn-deep',
 'seaborn-muted',
 'seaborn-notebook',
 'seaborn-paper',
 'seaborn-pastel',
 'seaborn-poster',
 'seaborn-talk',
 'seaborn-ticks',
 'seaborn-white',
 'seaborn-whitegrid',
 'tableau-colorblind10']

The change the style using

In [3]: matplotlib.style.use('ggplot')

Let’s see how a ROC curve looks with the new style:

In [4]: plot.roc(y_true, y_score)
Out[4]: <AxesSubplot:title={'center':'ROC'}, xlabel='False Positive Rate', ylabel='True Positive Rate'>
../_images/roc_ggplot.png

Saving plots

In [5]: ax = plot.roc(y_true, y_score)

In [6]: fig = ax.get_figure()

In [7]: fig.savefig('my-roc-curve.png')

Comparing several models with one plot

In [8]: fig, ax = plt.subplots()

In [9]: plot.roc(y_true, y_score, ax=ax)
Out[9]: <AxesSubplot:title={'center':'ROC'}, xlabel='False Positive Rate', ylabel='True Positive Rate'>

In [10]: plot.roc(y_true, y_score2, ax=ax)
Out[10]: <AxesSubplot:title={'center':'ROC'}, xlabel='False Positive Rate', ylabel='True Positive Rate'>

In [11]: ax.legend(['Model 1', 'Baseline', 'Model 2'])
Out[11]: <matplotlib.legend.Legend at 0x7fc8423dcee0>

In [12]: fig
Out[12]: <Figure size 640x480 with 1 Axes>
../_images/model_comparison.png

Grid plots

In [13]: fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True)

In [14]: plot.roc(y_true, y_score, ax=ax1)
Out[14]: <AxesSubplot:title={'center':'ROC'}, xlabel='False Positive Rate', ylabel='True Positive Rate'>

In [15]: plot.roc(y_true, y_score2, ax=ax2)
Out[15]: <AxesSubplot:title={'center':'ROC'}, xlabel='False Positive Rate', ylabel='True Positive Rate'>

In [16]: ax1.legend(['Model 1'])
Out[16]: <matplotlib.legend.Legend at 0x7fc826683a30>

In [17]: ax2.legend(['Model 2'])
Out[17]: <matplotlib.legend.Legend at 0x7fc8291fa070>

In [18]: fig
Out[18]: <Figure size 640x480 with 2 Axes>
../_images/grid_plot.png

Customizing plots

In [19]: ax = plot.roc(y_true, y_score)

In [20]: ax.set_title('This is a custom title')
Out[20]: Text(0.5, 1.0, 'This is a custom title')

In [21]: ax
Out[21]: <AxesSubplot:title={'center':'This is a custom title'}, xlabel='False Positive Rate', ylabel='True Positive Rate'>
../_images/roc_custom.png