简体   繁体   English

Matplotlib中心轴上的plot_surface轴区域

[英]Matplotlib center plot_surface on axis area

I am trying to create a 3d surface plot but the result is not aligned to the origin: 我正在尝试创建3d表面图,但结果未与原点对齐: 在此处输入图片说明

You can easily see on the Z-projection that it's a little off on the X and Y axes. 您可以轻松地在Z投影上看到它在X和Y轴上略有偏离。 My data is a matrix where the first two columns are my X's and Y's and the 3rd column is my Z. This is how I compute the values: 我的数据是一个矩阵,其中前两列是我的X和Y的第3列是我的Z.我这是怎么计算的值:

bins = np.linspace(0, 1, 1000)
indices = np.digitize(data[:, :2], bins, right=True)
X, Y = np.meshgrid(bins, bins, indexing='ij')
Q = np.zeros_like(X)
C = np.zeros_like(X)
d = data[:, 2]
for idx in range(0, len(indices)):
    (i,j) = indices[idx]
    Q[i, j] += d[idx]
    C[i, j] += 1
C[C == 0] = 1 # avoid division by zero errors
Q /= C
return (X, Y, Q, C)

Plotting code: 绘图代码:

fig = plt.figure(figsize=(30, 15))
ax = fig.add_subplot(121, projection='3d')
ax.set_xlim3d(-0.5, 1)
ax.set_ylim3d(0, 1.5)
ax.set_zlim3d(-1.5, 1)
Q = ndi.gaussian_filter(Q, sigma=10)
ax.plot_surface(X, Y, Q, cmap=cm.coolwarm, lw=lw, rstride=rs, cstride=cs, alpha=a, antialiased=True)
ax.contourf(X, Y, Q, zdir='z', offset=-1.5, cmap=cm.coolwarm)
ax.contourf(X, Y, Q, zdir='x', offset=-0.5, cmap=cm.coolwarm)
ax.contourf(X, Y, Q, zdir='y', offset=+1.5, cmap=cm.coolwarm)

My question is, am I missing any calculation to ensure the surface is aligned to the origin? 我的问题是,是否缺少任何计算以确保曲面与原点对齐? thanks. 谢谢。

The surface is aligned to the origin. 表面对齐到原点。 But the origin is "hanging in the air". 但是起源是“悬在空中”。 Or in other words, the bottom pane is slightly (~0.05 units) below the contourf plot, which makes it appear as if the plot was misaligned. 换句话说,底部窗格在contourf图的下方略微(〜0.05单位),使其看起来好像该图未对齐。

You may want to look at this question: Removing axes margins in 3D plot use one of the two answers to have the panes aligned properly. 您可能想看一下这个问题: 在3D图中删除轴边距使用两个答案之一来使窗格正确对齐。

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

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