I want to shade half of the background of a scatter plot to distinguish two different regimes.
I have tried to use axvspan
to do so. However, I have the problem that whenever I apply the axvspan, matplotlib adds a margin to the latter resulting in an ugly gap between the shading and the axis bounding box. How can I avoid this?
Here's what I have tried so far:
import matplotlib.pyplot as plt
import numpy as np
nsamp = 50
x = np.random.rand(nsamp)
y = np.random.rand(nsamp)
fig, ax = plt.subplots(1, 1, figsize=(8, 8))
ax.scatter(x, y)
ax.use_sticky_edges = True
_, xmax = plt.gca().get_xlim()
ax.axvspan(0.5, xmax=xmax, color="#97999B", zorder=-1)
plt.show()
** Result:**