Smoothed 2D histogram using matplotlib and imshow
Solution 1
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)
Solution 2
To your first question:
You need to clear data from a previous plot, putting the following before you plot should do this:
plt.clf()
plt.close()
To your second question:
To change the axis values I'd suggest the extent
parameter (see this answer).
e.g. something like:
plt.imshow(h, origin = "lower", interpolation = "gaussian",extent=[-100,100,-75,75])
Ger
I am associate professor in numerical simulations and physical-chemistry at Pau (France) in Université de Pau et des Pays de l'Adour. I did my phd in Paris XI (Orsay) on the study, by numeric simulations, of the photophysics properties of Fluorescent Proteins.
Updated on June 06, 2022Comments
-
Ger almost 2 years
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
andplt.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
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")
-
Dmytro Oliinychenko about 2 yearsNot working anymore (seaborn 0.11.2), fails with error "If using all scalar values, you must pass an index". Apparently seaborn needs new type of structure for data.
-
Dmytro Oliinychenko about 2 yearsMade it work again with ``` data = np.random.multivariate_normal([0, 0], [(1, .6), (.6, 1)], 100) df = pd.DataFrame(data, columns = ['X','Y']) f, ax = plt.subplots(figsize=(7, 7)) sns.kdeplot(data = df, shade=True, ax=ax, x='X', y='Y') ```