如何以更动态的方式绘制字典的内容?



我的问题如下,我正在输入一个脚本来散点图,但我认为我的代码可以用更动态或python的方式编写。

cam_0_pos_pts = np.asarray(pcd.select_by_index(cam_points_final['cam_0']['positive_point_ids'].tolist()).points)
cam_1_pos_pts = np.asarray(pcd.select_by_index(cam_points_final['cam_1']['positive_point_ids'].tolist()).points)
cam_2_pos_pts = np.asarray(pcd.select_by_index(cam_points_final['cam_2']['positive_point_ids'].tolist()).points)
cam_3_pos_pts = np.asarray(pcd.select_by_index(cam_points_final['cam_3']['positive_point_ids'].tolist()).points)
cam_0_neg_pts = np.asarray(pcd.select_by_index(cam_points_final['cam_0']['negative_point_ids'].tolist()).points)
cam_1_neg_pts = np.asarray(pcd.select_by_index(cam_points_final['cam_1']['negative_point_ids'].tolist()).points)
cam_2_neg_pts = np.asarray(pcd.select_by_index(cam_points_final['cam_2']['negative_point_ids'].tolist()).points)
cam_3_neg_pts = np.asarray(pcd.select_by_index(cam_points_final['cam_3']['negative_point_ids'].tolist()).points)
ax.scatter(cam_0_pos_pts[:,0], cam_0_pos_pts[:,1], cam_0_pos_pts[:,2], c = 'blue', label = 'Cam 0_positive_dot')
ax.scatter(cam_1_pos_pts[:,0], cam_1_pos_pts[:,1], cam_1_pos_pts[:,2], c = 'brown', label = 'Cam 1_positive_dot')
ax.scatter(cam_2_pos_pts[:,0], cam_2_pos_pts[:,1], cam_2_pos_pts[:,2], c = 'orange', label = 'Cam 2_positive_dot')
ax.scatter(cam_3_pos_pts[:,0], cam_3_pos_pts[:,1], cam_3_pos_pts[:,2], c = 'azure', label = 'Cam 3_positive_dot')
ax.scatter(cam_0_neg_pts[:,0], cam_0_neg_pts[:,1], cam_0_neg_pts[:,2], c = 'slateblue', label = 'Cam 0_negative_dot')
ax.scatter(cam_1_neg_pts[:,0], cam_1_neg_pts[:,1], cam_1_neg_pts[:,2], c = 'firebrick', label = 'Cam 1_negative_dot')
ax.scatter(cam_2_neg_pts[:,0], cam_2_neg_pts[:,1], cam_2_neg_pts[:,2], c = 'wheat', label = 'Cam 2_negative_dot')
ax.scatter(cam_3_neg_pts[:,0], cam_3_neg_pts[:,1], cam_3_neg_pts[:,2], c = 'lightcyan', label = 'Cam 3_negative_dot')
plt.show()

谁能建议一个更好的方法来写这个?

一个简单的改进是使用for循环:

pos_colors = ['blue','brown','orange','azure']
neg_colors = ['slateblue','firebrick','wheat','lightcyan']
for i in range(4):
cam_pos_pts = np.asarray(pcd.select_by_index(cam_points_final['cam_'+str(i)]['positive_point_ids'].tolist()).points)
cam_neg_pts = np.asarray(pcd.select_by_index(cam_points_final['cam_'+str(i)]['negative_point_ids'].tolist()).points)
ax.scatter(cam_pos_pts[:,0], cam_pos_pts[:,1], cam_pos_pts[:,2], c = pos_colors[i], label = 'Cam '+str(i)+'_positive_dot')
ax.scatter(cam_neg_pts[:,0], cam_neg_pts[:,1], cam_neg_pts[:,2], c = neg_colots[i], label = 'Cam '+str(i)+'_negative_dot')
plt.show()

最新更新