ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

kill python listener which opened matplotlib figure [closed]

asked 2013-04-16 23:36:59 -0500

Hunk gravatar image

updated 2014-01-28 17:16:13 -0500

ngrennan gravatar image

Hello,

i wrote a visualization with matplotlib in python.

import matplotlib.pyplot as plt


def callback(data):
do something
plt.show()

def listener():
rospy.Subscriber('topic', data, callback)
rospy.spin()

 Main function.
if __name__ == '__main__':
# Initialize the node and name it.

   rospy.init_node('pylistener', anonymous = True)
   # Go to the main loop.
   listener()

i want to print my figure. The problem is when he get a message he stop till i close the figure. That is fine. But when i want to close the program with ctrl+c the program crash and i have to close the terminal.

It is possible to kill the node and the python program after i close the figure?

Or what i can do against this crashing? It is really annoying that i have to close the terminal and restart it.

thank you for your help and sry i am new on python

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by SL Remy
close date 2016-09-08 12:34:06.584438

Comments

Also, where do you define data?

SL Remy gravatar image SL Remy  ( 2013-04-19 10:01:32 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2013-04-17 02:30:39 -0500

SL Remy gravatar image

updated 2013-04-17 10:10:40 -0500

My guess is that you're sending more than just one data packet, thus attempting plot more than once. That's the reason why it hangs.

ps -ef in another terminal will show you the PID for the program and a kill PID should stop the process.

In any case, to get past the core issue, I'd suggest using savefig saving files with a unique name, appending timestamp is one approach, and the you can print at your leisure. You should use clf after you save the figure though.

edit flag offensive delete link more

Comments

yes i send more then one message. It is not possible to kill the node and the python program after i received the first one and I closed the figure?

Hunk gravatar image Hunk  ( 2013-04-17 03:00:08 -0500 )edit

if you kill the node then the data that you're also killing the subscriber to the data that trying to plot.

SL Remy gravatar image SL Remy  ( 2013-04-17 04:36:53 -0500 )edit

My suggestion is to use savefig/clf instead of using show(), since that method "block[s] until the figures have been closed"

SL Remy gravatar image SL Remy  ( 2013-04-17 04:45:19 -0500 )edit

I only want to see one plot. So it doesn't matter if my plot get more data. When i save my figure i get every time i receive the message a new identical file.

Hunk gravatar image Hunk  ( 2013-04-18 23:20:42 -0500 )edit

Is the published data actually changing? (rostopic echo -p /yourtopic)...

SL Remy gravatar image SL Remy  ( 2013-04-19 09:57:40 -0500 )edit

Question Tools

Stats

Asked: 2013-04-16 23:36:59 -0500

Seen: 1,821 times

Last updated: Apr 17 '13