Line Chart
In order to show data dyanmically, it’s necessary to use interactive mode of matplotlib . The following codes show a sine line dynamically.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 import matplotlib.pyplot as pltimport numpy as npimport timefrom math import *count = 0 time_list = [] data_list = [] def main (): plt.ion() plt.figure(1 ) for :i range (2000 ): plt.clf() time_list.append(0.1 * i) data_list.append(sin(0.1 * i)) plt.plot(time_list, data_list, '-r' ) plt.pause(0.01 ) if __name__ == '__main__' : main()
Scatter Graph
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import matplotlib.pyplot as pltimport numpy as npimport timefrom math import *def main (): plt.ion() plt.figure(1 ) for i in range (2000 ): plt.scatter(i * 0.1 , sin(i * 0.1 )) plt.pause(0.01 ) if __name__ == '__main__' : main()
Receive and Show Data from ROS Topic
In my ROS project, I need to receive messages from topic and analyze them, so I write down the following codes to remind myself how to finish the whole procedure.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 import rospyfrom std_msgs.msg import String, Float32import matplotlib.pyplot as pltimport numpy as npimport timefrom math import *count = 0 angle_list = [] time_list = [] def draw_angle_callback (angle ): global angle_list, count, time_list if len (angle_list) > 300 : angle_list.pop(0 ) time_list.pop(0 ) angle_list.append(angle.data) time_list.append(count) count += 1 def main (): rospy.init_node("draw_steering_angle" , anonymous=True ) steering_angle_sub = rospy.Subscriber("angle" , Float32, draw_angle_callback) plt.ion() plt.figure(1 ) while not rospy.is_shutdown(): plt.clf() plt.ylim((-100 , 100 )) plt.plot(time_list, angle_list, 'r' , linestyle="--" ) plt.pause(0.01 ) rospy.spin() if __name__ == "__main__" : try : main() except rospy.ROSInterruptException: pass
References: