3D Histogram off-centred

32 Views Asked by At

I am plotting a histogram of a wavefunction and my code seems to be consistently plotting the peak slightly off-centred from the origin (which is where it should be). This shift also always seems to be in the same direction, however I can't confirm this yet.

This may be due to my code, which I am looking into, but as I have a 1D version which works very well (I have attached am image of this for reference). I would be quite surprised if this was the issue as it is not a very difficult extension of the problem to 2D, but of course I still can't rule this out.

Essentially I need to be able to rule out that the histogram plotting code is the problem, any help on this would be greatly appreciated.

xpos and ypos are 1D arrays of positions that have been generated by a metropolis algorithm. I also have plots of residuals on the graphs that I have left out of the code here for simplicity 2D histogram of metropololis, 3D hist

"Generate 3D histogram"

hist, xedges, yedges = np.histogram2d(xpos, ypos, bins=bins)
Norm = np.max(Z)/np.max(hist) * hist
#print(xedges)
#print(yedges)
#print(Norm)

"Plotting 3D Histogram (with residuals)"
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})

x, y = np.meshgrid(xedges[:-1]+xedges[1:], yedges[:-1]+yedges[1:])
x = x.flatten()/2
y = y.flatten()/2
z = np.zeros_like(x)

dx = xedges[1] - xedges[0]
dy = yedges[1] - yedges[0]
dz = Norm.flatten()

cmap = cm.get_cmap('viridis')
max_height = np.max(dz)
min_height = np.min(dz)
rgba = [cmap((k-min_height)/max_height) for k in dz]

ax.bar3d(x, y, z, dx, dy, dz, color=rgba, zsort='average')
ax.plot_surface(X, Y, Z, cmap=cm.coolwarm, alpha=0.3, linewidth=0, antialiased=False)


plt.xlabel("x")
plt.ylabel("y")
ax.set_xlim([-3, 3])
ax.set_ylim([-3, 3])
ax.set_zlabel('|' + chr(968) + '|' + chr(178))
#ax.zaxis.set_major_formatter('{x:.02f}')
ax.tick_params(axis='z', labelcolor='red')
#plt.figtext(0.5, 0.01, txt, wrap=True, horizontalalignment='center', fontsize=12)
fig.savefig(dir + '\\Images\\3Dhist_' + name + '-' + str(t_f) + 's.png')
plt.show()
0

There are 0 best solutions below