Suppose I have a scatterplot with some kind of line (least squares regression line, knn regression line, etc.) through it, like this.
I want to shade the upper region of the plot reddish, and the lower region of the plot blueish, to give an indication of how my line is doing as a classifier for the points. Similar to my mimic example with this effect is this plot from Elements of Statistical Learning (Hastie et al), (Chapter 2, page 13).
How can I achieve this effect with Matplotlib?
I know how to set rectangular regions of a plot to be different colors with axhspan
and axvspan
(see this answer), but have been struggling to set different plot colors based on regions above and below a line.
Code to replicate my current mock plot
import numpy as np
import matplotlib.pyplot as plt
plt.style.use('seaborn-notebook')
np.random.seed(17)
grp1_x = np.random.normal(1, 1, 100)
grp1_y = np.random.normal(3, 1, 100)
grp2_x = np.random.normal(1.2, 1, 100)
grp2_y = np.random.normal(1.2, 1, 100)
########################################
## least squares plot
plt.scatter(grp1_x, grp1_y,
lw = 1,
facecolors = 'none',
edgecolors = 'firebrick')
plt.scatter(grp2_x, grp2_y,
lw = 1,
facecolors = 'none',
edgecolors = 'steelblue')
plt.tick_params(
axis = 'both',
which = 'both',
bottom = 'off',
top = 'off',
labelbottom = 'off',
right = 'off',
left = 'off',
labelleft = 'off')
full_x = np.concatenate([grp1_x, grp2_x])
full_y = np.concatenate([grp1_y, grp2_y])
m, c = np.linalg.lstsq(np.vstack([full_x,
np.ones(full_x.size)]).T,
full_y)[0]
plt.plot(full_x, m*full_x + c, color='black')
plt.show()