Smoothed 2D histogram using matplotlib and imshow

I try to do a 2D histogram plot and to obtain a “smooth” picture by a sort of interpolation. Thus I do the following combining plt.hist2d and plt.imshow

import matplotlib.pyplot as plt
import numpy as np

data = np.loadtxt("parametre_optMC.dat", skiprows=50, usecols=(1,2))

h, x, y, p = plt.hist2d(data[:,0], data[:,1], bins = 20)
plt.imshow(h, origin = "lower", interpolation = "gaussian")
plt.savefig("test.pdf")

As you can see on the picture below, the two plots are superimposed and that is the problem for which I need some help

enter image description here

Adding clf works but I lose axes dimenions :

import matplotlib.pyplot as plt
import numpy as np

data = np.loadtxt("parametre_optMC.dat", skiprows=50, usecols=(1,2))

h, x, y, p = plt.hist2d(data[:,0], data[:,1], bins = 20)
plt.clf()
plt.imshow(h, origin = "lower", interpolation = "gaussian")
plt.savefig("test.pdf")

enter image description here

Best answer

Perhaps it would be better to plot a kernel density estimate?

import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt

data = np.random.multivariate_normal([0, 0], [(1, .6), (.6, 1)], 100)
f, ax = plt.subplots(figsize=(7, 7))
sns.kdeplot(data, shade=True, ax=ax)

enter image description here