13

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

Ger
  • 9,076
  • 10
  • 37
  • 48
  • You might still have data from a previous plot in your figure. If you do `plt.clf()` and `plt.close()` it will be cleared. – Lee May 27 '14 at 12:17
  • Ok, it works but I lose axes dimensions. I edit the post. – Ger May 27 '14 at 13:32
  • Do you only want to see the smoothed picture? I am not sure what the question is. –  May 27 '14 at 13:39
  • Yes, I only want the smoothed one. But as you can see axes scale are not the same before and after `imshow`. – Ger May 27 '14 at 14:17
  • Try looking at answers here: http://stackoverflow.com/q/18696122/1461850 – Lee May 27 '14 at 14:25

3 Answers3

17

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

mwaskom
  • 46,693
  • 16
  • 125
  • 127
  • Not 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 Mar 24 '22 at 22:49
  • Made 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') ``` – Dmytro Oliinychenko Mar 24 '22 at 23:02
  • `shade=True` is now replaced by `fill=True` – romain gal Feb 19 '23 at 11:19
1

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])
Community
  • 1
  • 1
Lee
  • 29,398
  • 28
  • 117
  • 170
0

You need to add the 'extent' parameter to you imshow command. imshow accepts a grid of arbitrary values but does not know the dimensions.

skytaker
  • 4,159
  • 1
  • 21
  • 31