0
$\begingroup$

Rosanswers logo

From what I can tell the answer is no. But I didn't want to reproduce anything if it already existed or if there was a clever combination of tools/msgs that I wasn't sharp enough to think up.


Originally posted by Asomerville on ROS Answers with karma: 2743 on 2011-05-19

Post score: 3

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

I've never come across anything in ROS itself but assuming you've installed the rxtools package (rx stack) then you have matplotlib installed since it's a dependency (used by rxplot) and you can use its pyplot.hist command.

As for how to get the data to plot, there are lots of ways but you're looking for a quick, one-off solution you could use rosh (you probably want to check this page for usage details). It could look something like this though:

# pop up a histogram of the field `bar` from the 
# last 100 messages on the topic `foo`:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([m.bar for m in topics.foo[:100]])
plt.hist(x)
plt.show()

Originally posted by Patrick Bouffard with karma: 2264 on 2011-05-19

This answer was ACCEPTED on the original site

Post score: 5

$\endgroup$
0
$\begingroup$
#!/usr/bin/env python

import rospy
from geometry_msgs.msg import PointStamped
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
from collections import deque

class HistogramPlotter:
def __init__(self):
    self.data = deque(maxlen=10000)

def callback(self, msg):
    # Append the new data point
    self.data.append(msg.point.x)
    rospy.loginfo("Current delay: %.3f", self.data[-1])

    # Plot histogram and fit a GEV distribution
    if len(self.data) >= 100 and msg.header.seq % 30 == 0:
        self.plot_histogram()

def plot_histogram(self):

    # Plot histogram
    plt.clf()
    plt.hist(self.data, bins=50, density=True, alpha=0.7)

    # Fit a normal distribution to the data
    mu, std = norm.fit(self.data)
    xmin, xmax = plt.xlim()
    x = np.linspace(xmin, xmax, 100)
    p = norm.pdf(x, mu, std)

    # Overlay the fitted curve
    plt.plot(x, p, 'k', linewidth=2)
    plt.title(f"Histogram of /frontCamera/downDelay/point/x (Data Length: {len(self.data)})")
    plt.xlabel("Delay")
    plt.ylabel("Density")
    plt.text(xmax * 0.8, np.max(p) * 0.8, f"Mean: {mu:.3f}, Std: {std:.3f}", fontsize=12)

    # Show plot
    plt.pause(0.0001)

def main():
    rospy.init_node('histogram_plotter', anonymous=True)
    plotter = HistogramPlotter()
    rospy.Subscriber("/frontCamera/downDelay", PointStamped, plotter.callback, queue_size = 100)
    plt.ion()  # Turn on interactive mode for live plotting
    plt.show()
    rospy.spin()

if __name__ == '__main__':
    main()
$\endgroup$
1
  • $\begingroup$ Could you edit your answer and explain what your code does? $\endgroup$ Apr 2 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.