import matplotlib.pyplot as plt
import numpy as np
from astropy.visualization import simple_norm
from photutils.background import Background2D
from photutils.datasets import make_100gaussians_image
from scipy.ndimage import rotate

data = make_100gaussians_image()
ny, nx = data.shape
y, x = np.mgrid[:ny, :nx]
gradient = x * y / 5000.0
data2 = data + gradient
data3 = rotate(data2, -45.0)
coverage_mask = (data3 == 0)
bkg3 = Background2D(data3, (15, 15), filter_size=(3, 3),
                    coverage_mask=coverage_mask, fill_value=0.0,
                    exclude_percentile=50.0)
norm = simple_norm(bkg3.background, 'sqrt', percent=99.5)
fig, ax = plt.subplots()
ax.imshow(bkg3.background, norm=norm, origin='lower')
ax.set_title('Estimated Background')