简体   繁体   English

在二维直方图中的点之间画一条线

[英]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.我已经绘制了一个二维直方图,需要 plot 一些点覆盖这个直方图并在它们之间画一条线。 I already tried plt.plot() but neither the points nor lines appear in the plot.我已经尝试过plt.plot()但点和线都没有出现在 plot 中。 If I use plt.scatter() now the points appear, but I still need to connect the points with a line.如果我现在使用plt.scatter()点会出现,但我仍然需要用线连接点。

My plot is below:我的 plot 如下:

在此处输入图像描述

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: (我忘了说,但我只想 plot 一些点,在这种情况下是 200,不是全部)。我使用的代码是:

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()

I do not know what you have in mind, but specifying the plot method's marker argument yields dots connected by lines:我不知道你在想什么,但是指定 plot 方法的标记参数会产生由线连接的点:

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()

在此处输入图像描述 As for the connection order: Just order your array, the points are going to be connected according to their position in the array.至于连接顺序:只需订购您的阵列,点将根据阵列中的 position 进行连接。

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()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM