0

I am working on the following code from this link. I have a question: in the mentioned link the following code lead to a plot. But running this code does not plot any thing. what should be added?

import numpy as np
import scipy.stats as st
import seaborn as sns


mus = np.array([5, 5])
sigmas = np.array([[1, .9], [.9, 1]])


def circle(x, y):
    return (x-1)**2 + (y-2)**2 - 3**2


def pgauss(x, y):
    return st.multivariate_normal.pdf([x, y], mean=mus, cov=sigmas)


def metropolis_hastings(p, iter=1000):
    x, y = 0., 0.
    samples = np.zeros((iter, 2))

    for i in range(iter):
        x_star, y_star = np.array([x, y]) + np.random.normal(size=2)
        if np.random.rand() < p(x_star, y_star) / p(x, y):
            x, y = x_star, y_star
        samples[i] = np.array([x, y])

    return samples


if __name__ == '__main__':
    samples = metropolis_hastings(circle, iter=10000)
    sns.jointplot(samples[:, 0], samples[:, 1])

    samples = metropolis_hastings(pgauss, iter=10000)
    sns.jointplot(samples[:, 0], samples[:, 1])

Thank you for your help.

Bob
  • 39
  • 1
  • 10
  • `plt.show()` The second question should be asked separately. – Mr. T Apr 15 '18 at 18:17
  • @Mr.T Thank you. but there is no `plt` in this code. – Bob Apr 15 '18 at 18:19
  • @Mr.T Yes I have seen this link before. but its sampling like my question could not be plot. I am new with seaborn. Could you tell me what lines must be added? Thank you – Bob Apr 15 '18 at 18:26
  • Oh, sorry. The common convention is `import matplotlib.pyplot as plt`. – Mr. T Apr 15 '18 at 18:27
  • @Mr.T Using this again do not plot anything. I don't know why – Bob Apr 15 '18 at 18:32
  • 1
    Does `plt.plot([0,2,3,1]); plt.show()` generate a graph? – Mr. T Apr 15 '18 at 18:40
  • @Mr.T Yes. I haven't seen this before. I have to use `plt.plot()` and `plt.show()` both together. – Bob Apr 15 '18 at 18:45
  • Maybe you should start with [this tutorial](https://matplotlib.org/tutorials/introductory/pyplot.html#sphx-glr-tutorials-introductory-pyplot-py) before trying to use seaborn, which is mainly a facade for matplotlib. – Mr. T Apr 15 '18 at 18:47
  • @Mr.T Yes sure. for using this example I have to use seaborn for the first time in my life. – Bob Apr 15 '18 at 18:49

1 Answers1

1

As reported by @Mr. T and also in Seaborn plots not showing up, you should add plt.show(). plt has to be imported from matplotlib.

So your code should read:

import numpy as np
import scipy.stats as st
import seaborn as sns
import matplotlib.pyplot as plt

mus = np.array([5, 5])
sigmas = np.array([[1, .9], [.9, 1]])


def circle(x, y):
    return (x-1)**2 + (y-2)**2 - 3**2


def pgauss(x, y):
    return st.multivariate_normal.pdf([x, y], mean=mus, cov=sigmas)


def metropolis_hastings(p, iter=1000):
    x, y = 0., 0.
    samples = np.zeros((iter, 2))

    for i in range(iter):
        x_star, y_star = np.array([x, y]) + np.random.normal(size=2)
        if np.random.rand() < p(x_star, y_star) / p(x, y):
            x, y = x_star, y_star
        samples[i] = np.array([x, y])

    return samples


if __name__ == '__main__':
    samples = metropolis_hastings(circle, iter=10000)
    sns.jointplot(samples[:, 0], samples[:, 1])

    samples = metropolis_hastings(pgauss, iter=10000)
    sns.jointplot(samples[:, 0], samples[:, 1])
    plt.show()
jcgiret
  • 728
  • 7
  • 12