Python, Matplotlib: Drawing vertical lines in 3d plot, when data is independent -


i have random walker in (x,y) plane , -log(bivariate gaussian) in (x,y,z) plane. these 2 datasets independent.

i want sample, 5 (x,y) pairs of random walker , draw vertical lines z-axis , terminate vertical line when "meets" bivariate gaussian.

this code far:

import matplotlib mpl import matplotlib.pyplot plt import random import numpy np import seaborn sns import scipy mpl_toolkits.mplot3d import axes3d matplotlib.mlab import bivariate_normal  %matplotlib inline      # data random walk  def randomwalk():      mpl.rcparams['legend.fontsize'] = 10      xyz = []     cur = [0, 0]      _ in range(40):         axis = random.randrange(0, 2)         cur[axis] += random.choice([-1, 1])         xyz.append(cur[:])      # density      x, y = zip(*xyz)     data = np.vstack([x,y])     kde = scipy.stats.gaussian_kde(data)     density = kde(data)       # data bivariate gaussian       = np.linspace(-7.5, 7.5, 40)     b =     x,y = np.meshgrid(a, b)     z = bivariate_normal(x, y)     surprise_z = -np.log(z)      # random points walker , plot z-axis gaussian      m = data[:,np.random.choice(20,5)].t      # plot figure      fig = plt.figure(figsize=(10, 7))      ax = fig.gca(projection='3d')     ax.plot(x, y, 'grey', label='random walk') # walker     ax.scatter(x[-1], y[-1], c='k', marker='o') # end point      ax.legend()      surf = ax.plot_surface(x, y, surprise_z, rstride=1, cstride=1,          cmap = plt.cm.gist_heat_r, alpha=0.1, linewidth=0.1)      #fig.colorbar(surf, shrink=0.5, aspect=7, cmap=plt.cm.gray_r)      in range(5):         ax.plot([m[i,0], m[i,0]],[m[i,1], m[i,1]], [0,10],'k--',alpha=0.8, linewidth=0.5)      ax.set_zlim(0, 50)     ax.set_xlim(-10, 10)     ax.set_ylim(-10, 10) 

which produces

enter image description here

as can see thing i'm struggling how terminate vertical lines when meet appropriate z-value. ideas welcome!

you're letting lines height of 10 using [0,10] z coordinates. can change loop following:

for in range(5):     x = [m[i,0], m[i,0]]     y = [m[i,1], m[i,1]]     z = [0,-np.log(bivariate_normal(m[i,0],m[i,1]))]     ax.plot(x,y,z,'k--',alpha=0.8, linewidth=0.5) 

this takes x , y coordinates each point loop on , calculates height of overlying gaussian point , plots there. here plot linestyle changed emphasize lines relevant question:

enter image description here


Comments