0

I have a sensor_msgs/PointCloud2 with [x,y,z] and how can I plot it in real-time in matplotlib like this code here. I already changed the type from Odometry to pointcloud2 but I don't know what to change in odom_callback or how to change the code in order to plot it in matplotlib. Can someone has an idea how to plot pointcloud2 in matplotlib

import matplotlib.pyplot as plt
import rospy
import tf
from sensor_msgs.msg import PointCloud2
from tf.transformations import quaternion_matrix
import numpy as np
from matplotlib.animation import FuncAnimation


class Visualiser:
    def __init__(self):
        self.fig, self.ax = plt.subplots()
        self.ln, = plt.plot([], [], 'ro')
        self.x_data, self.y_data = [] , []

    def plot_init(self):
        self.ax.set_xlim(0, 10000)
        self.ax.set_ylim(-7, 7)
        return self.ln
    
    def getYaw(self, pose):
        quaternion = (pose.orientation.x, pose.orientation.y, pose.orientation.z,
                pose.orientation.w)
        euler = tf.transformations.euler_from_quaternion(quaternion)
        yaw = euler[2] 
        return yaw   

    def odom_callback(self, msg):
        yaw_angle = self.getYaw(msg.pose.pose)
        self.y_data.append(yaw_angle)
        x_index = len(self.x_data)
        self.x_data.append(x_index+1)
    
    def update_plot(self, frame):
        self.ln.set_data(self.x_data, self.y_data)
        return self.ln


rospy.init_node('publisher_node')
vis = Visualiser()
sub = rospy.Subscriber('/scan3dd', PointCloud2, vis.odom_callback)

ani = FuncAnimation(vis.fig, vis.update_plot, init_func=vis.plot_init)
plt.show(block=True) 
  • This question doesn’t really make a lot of sense. *How* are you wanting to plot the data. Odometery and pointclouds are totally different types of data so you can’t really plot them the same. – BTables Apr 12 '22 at 15:09
  • @BTables the code is from the odemetry type and I use it as reference since I am using pointcloud. Is there a way I can edit the code so that it can be used for pointcloud? or are there other ways to plot pointcloud data in real tiem in matplotlib? – user18465277 Apr 12 '22 at 15:35
  • What I mean is it doesn’t make sense to say “plotting a pointcloud”. What do you want the visualization to actually look like? The code you provided is plotting just yaw from odometry. Pointclouds are 3 dimensional while yaw is 1. – BTables Apr 12 '22 at 18:45
  • Okay I guess I was not clear with my question, sorry. The code here is just an example of plotting an odometry in matplotlib. I am looking for similar function that can plot pointcloud data from a matplotlib. Is it possible? – user18465277 Apr 13 '22 at 01:49
  • The code you provided is not an example of plotting odometry. It plots yaw relative to time. This is a scalar quantity, which pointcloud data is not. So in this case is it not possible, and does not make sense to, plot pointcloud data. Are you looking to visualize the pointcloud in 3d space? – BTables Apr 13 '22 at 04:14
  • Yes I want to visualize point cloud in 3D space in matplotlib. Do you know how to do it? – user18465277 Apr 13 '22 at 06:51
  • That is outside the scope of what matplotlib is designed for. So no, it’s not really possible for what you want. For pointcloud visualization in ROS you should use [Rviz](http://wiki.ros.org/rviz/UserGuide) – BTables Apr 13 '22 at 18:11
  • ah okay so it is not possible. I already used rviz for visualizing the point cloud data. I have a follow up question, can I embed rviz in a pyqt5? – user18465277 Apr 14 '22 at 05:12

0 Answers0