简体   繁体   English

如何使用 matplotlib 获得稳定的绘图

[英]How to get stable plot using matplotlib

I'm trying to plot 3D poses of hand using matplotlib.我正在尝试使用 matplotlib 绘制手的 3D 姿势。 For every frame of video or we can say if data is changed then plot size(X, Y, Z) also changed with respect to object(hand poses) size and position.对于视频的每一帧,或者我们可以说如果数据发生了变化,那么绘图大小(X、Y、Z)也会随着对象(手部姿势)的大小和位置而变化。 For more detail below are two screenshots with next and previous frame, in screenshots we can see that x, y and z axis are changed as hand pose is changed.下面的更多细节是包含下一帧和上一帧的两个屏幕截图,在屏幕截图中我们可以看到 x、y 和 z 轴随着手部姿势的变化而变化。

图片_1 图片_2

My question is that how to get a stable plot that it's size will not change even object position will be changed.我的问题是如何获得一个稳定的图,即使对象位置会改变,它的大小也不会改变。

As a reference below is another image that shows a stable plot and I'm trying to get like this.下面的参考是另一张显示稳定图的图像,我正在尝试这样做。 As we can see that input frames and object size is changing but plot is with same size.正如我们所看到的,输入帧和对象大小正在变化,但绘图大小相同。 No matter if object size will be changed in plot.无论对象大小是否会在绘图中发生变化。

结果_图像

Below is my plotting code.下面是我的绘图代码。 Kindly take it just as plotting sample, I'm sharing only plotting code because problem is in plotting code.请把它当作绘图示例,我只分享绘图代码,因为问题出在绘图代码上。

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.view_init(elev=20, azim=75)
ax.set_xlabel('X Axis')
ax.set_ylabel('Y Axis')
ax.set_zlabel('Z Axis')
rgb_dict = get_keypoint_rgb(skeleton)
    
for i in range(len(skeleton)): # here is skeleton, just ignore it
    joint_name = skeleton[i]['name']
    pid = skeleton[i]['parent_id']
    parent_joint_name = skeleton[pid]['name']

    x = np.array([kps_3d[i, 0], kps_3d[pid, 0]]) # kps_3d is pose data
    y = np.array([kps_3d[i, 1], kps_3d[pid, 1]])
    z = np.array([kps_3d[i, 2], kps_3d[pid, 2]])

    ax.plot(x, z, -y, c = np.array(rgb_dict[parent_joint_name])/255., linewidth = line_width)
    ax.scatter(kps_3d[i, 0], kps_3d[i, 2], -kps_3d[i, 1], c = np.array(rgb_dict[joint_name]).reshape(1, 3)/255., marker='o')
    ax.scatter(kps_3d[pid, 0], kps_3d[pid, 2], -kps_3d[pid, 1], c = np.array(rgb_dict[parent_joint_name]).reshape(1, 3)/255., marker='o')

As ax.scatter docs show that it is used to get a scatter plot of y vs. x with varying marker size and/or color .正如ax.scatter 文档所示,它用于获得y 与 x 的散点图,其中标记大小和/或颜色不同

Which alternative function I can use to get stable plots?我可以使用哪个替代函数来获得稳定的图?

Looking for some valuable suggestions.寻求一些有价值的建议。

Try set the plot limits by these lines of code:尝试通过这些代码行设置绘图限制:

ax.set_xlim3d(-0.1, 0.1)
ax.set_ylim3d(-0.1, 0.1)
ax.set_zlim3d(-0.1, 0.1)

Adjust the numbers as needed.根据需要调整数字。 You may also need to adjust viewing angle:您可能还需要调整视角:

ax.view_init(elev=28., azim=55)

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

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