Search code examples
pythonmatplotlibplothistogram

Draw a line between points in a 2D histogram


I need to draw a line between point in my analysis.

I have plotted a 2D histogram, and need to plot some points overlaying this histogram and draw a line between them. I already tried plt.plot() but neither the points nor lines appear in the plot. If I use plt.scatter() now the points appear, but I still need to connect the points with a line.

My plot is below:

enter image description here

Any tips in how can I connect those red dots? (I forgot to say it, but i just want to plot some points, in this case 200, not all of them).And the code i used is:

import numpy as np
import matplotlib.pyplot as plt
from numpy import zeros,empty,array,loadtxt,dot,linspace

f = [0]
yy = [0]
jjj=0
def leapfrog(x, v, gradient, timestep, trajectory_length):
    v -= 0.5 * timestep * gradient(x)
    for _ in range(trajectory_length - 1):
        x += timestep * v
        v -= timestep * gradient(x)
    x += timestep * v
    v -= 0.5 * timestep * gradient(x)
    #f.append(x)
    
    
    
    return x, v
    
def sample_HMC(x_old, log_prob, log_prob_gradient, timestep, trajectory_length):
    # switch to physics mode!
    def E(x): return -log_prob(x)
    def gradient(x): return -log_prob_gradient(x)
    def K(v): return 0.5 * np.sum(v ** 2)
    def H(x, v): return K(v) + E(x)

    # Metropolis acceptance probability, implemented in "logarithmic space"
    # for numerical stability:
    def log_p_acc(x_new, v_new, x_old, v_old):
        return min(0, -(H(x_new, v_new) - H(x_old, v_old)))

    # give a random kick to particle by drawing its momentum from p(v)
    v_old = np.random.normal(size=x_old.shape)

    # approximately calculate position x_new and momentum v_new after
    # time trajectory_length  * timestep
    x_new, v_new = leapfrog(x_old.copy(), v_old.copy(), gradient,
                            timestep, trajectory_length)
                            
                           

    # accept / reject based on Metropolis criterion
    accept = np.log(np.random.random()) < log_p_acc(x_new, v_new, x_old, v_old)

    # we consider only the position x (meaning, we marginalize out v)
    f.append(x_new)
    #yy.append(v_new)
    if accept:       
        return accept, x_new
    else:
        return accept, x_old
        
def build_HMC_chain(init, timestep, trajectory_length, n_total, log_prob, gradient):
    n_accepted = 0
    chain = [init]

    for _ in range(n_total):
        accept, state = sample_HMC(chain[-1].copy(), log_prob, gradient,
                                   timestep, trajectory_length)
        chain.append(state)
        n_accepted += accept

    acceptance_rate = n_accepted / float(n_total)

    return chain, acceptance_rate
    
def log_prob(x): return -0.5 * np.sum(x ** 2)

def log_prob_gradient(x): return -x

chain, acceptance_rate = build_HMC_chain(np.array([5.0, 1.0]), 1.5, 10, 10000,
                                         log_prob, log_prob_gradient)
print("Acceptance rate: {:.3f}".format(acceptance_rate))

k=0
data = zeros(10001, float)
for item in chain:
    data[k] = chain[k][0]
    k=k+1
print(data)

k=0
datay = zeros(10001, float)
for item in chain:
    datay[k] = chain[k][1]
    k=k+1
print(data)

k=0
plt.hist2d(data,datay,100)
for i in range(200):
    #plt.scatter(data[i],datay[i])
    #plt.plot(data[i],datay[i])
    plt.plot(data[i],datay[i],'.r-')
#plt.ylim(-4.9,4.9)
#plt.xlim(-4.9,4.9)
plt.show()

Solution

  • I do not know what you have in mind, but specifying the plot method's marker argument yields dots connected by lines:

    import numpy as np
    import matplotlib.pyplot as plt
    
    x = np.random.normal(size=5000)
    y = x * 1.5 + np.random.normal(size=5000)
    x_p = [2,-2,-2,2]
    y_p = [2,2,-2,-2]
    x_p = np.append(x_p, x_p[0])
    y_p = np.append(y_p, y_p[0])
    
    plt.hist2d(x,y, bins=(50, 50))
    plt.plot(x_p,y_p,marker='o')
    plt.show()
    

    enter image description here As for the connection order: Just order your array, the points are going to be connected according to their position in the array.

    import numpy as np
    import matplotlib.pyplot as plt
    
    x = np.random.normal(size=50000)
    y = x * 1.5 + np.random.normal(size=50000)
    x_p = [2,-2,-2,2]
    y_p = [2,-2,2,-2]
    x_p = np.append(x_p, x_p[0])
    y_p = np.append(y_p, y_p[0])
    
    data = np.random.random((100,100))
    plt.hist2d(x,y, bins=(50, 50))
    plt.plot(x_p,y_p,marker='o')
    plt.show()
    

    enter image description here