Python live scatterplot in WSL with background image

Viewed 30

I have a ROS node that subscribes to a certain topic 'first' and this returns (x,y) values, which I would like to plot on a graph with a background image. I want to be able to show only the last tuple.

I am running this python script in WSL-Ubuntu-18.04 and have installed VcXsrv in Windows to visualize the graph.

This is the code I am using but I don't know how to plot only the last (x,y) output. At the moment, all the values are plotted and after a while the plotting slows terribly down because of all the points (I guess).

from Tkinter import Canvas
import Tkinter as Tk
import rospy
from std_msgs.msg import Int32MultiArray, String
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from time import sleep
from threading import Thread
import numpy as np
from PIL import ImageTk, Image

def callback(data):
    received = data.data.split(',')
    x  = int(float(received[0]))
    y = int(float(received[1]))

    plt.scatter(x,y)
   
def listener():
    rospy.Subscriber('first', String, callback)
    rospy.spin()

def animate(i):
    pass

def visual():
        root = Tk.Tk()
        label = Tk.Label(root, text="Realtime plot ")
        root.geometry("750x720")
        
        img = plt.imread("back.jpg")
        fig, ax = plt.subplots()
        #mng = plt.get_current_fig_manager()
        #mng.full_screen_toggle()
        ax.imshow(img,extent=[0, 17947, -200, 5330], aspect='equal')
        fig.canvas.draw()
        plt.show()

        plotcanvas = FigureCanvasTkAgg(fig, root)
        plotcanvas.get_tk_widget().grid(column=0, row=0)
        ani = FuncAnimation(fig, animate, interval=100, blit=False)
        
        Tk.mainloop()

t1 = Thread(target=visual)
t1.start()
listener()
t1.join()

I'm also struggling with scaling the image (hence the whole graph) to full screen. I've tried something but only the frame gets bigger

0 Answers
Related