簡體   English   中英

如何在 pyplot 中使用 Line Collection

[英]How to use Line Collection with pyplot

當您運行代碼時,錯誤應該非常明顯,只需在黑色空間中單擊鼠標並四處移動即可。 我不確定線段是如何創建的,我將它從庫中拉出來,有點令人困惑。 如何沿着我的散點圖 plot 獲得到 plot 的線段? 當我查看正在生成的段數組時,我相信它正在創建成對的 x,x 和 y,y 而不是 x,y 和 x,y

import tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.collections import LineCollection
from matplotlib.colors import ListedColormap
import numpy as np
import random

class Mouse_Game:
    '''Creates a matplotlib canvas object that plot mouse coordinates when animated.'''
    def __init__(self, root, width=400, height=400):
        self.frame = tk.Frame(master=root)
        self.status = False
        self.fig = plt.Figure(dpi=100, facecolor='black')
        self.ax = self.fig.add_axes([0,0,1,1], fc='black')
        self.width = width
        self.height = height
        self.ax.axis('off')

        #set up the canvas object and bind commands
        self.canvas = FigureCanvasTkAgg(self.fig, master=self.frame, resize_callback=self.configure)  # A tk.DrawingArea.
        self.canvas.draw()
        self.canvas.get_tk_widget().pack(side='top', fill='both', expand=True)
        self.fig.canvas.mpl_connect('button_press_event', self.animate)
        self.fig.canvas.mpl_connect('motion_notify_event', self.motion) 

    def animate(self, event):
        #Check if currently running
        if not self.status:
            self.status = True
            self.capture = np.array([(event.xdata, event.ydata)]*40)
        else:
            self.status = False

        #plot a line that follows the mouse around
        while self.status:
            self.ax.clear()
            self.ax.set_xlim(0, self.width)
            self.ax.set_ylim(0, self.height)
            x = self.capture[:,0]
            y = self.capture[:,1]
            ############################################################
            points = self.capture.T.reshape(-1, 1, 2)
            segments = np.concatenate([points[:-1], points[1:]], axis=1)
            ###########################################################
            lc = LineCollection(segments, cmap='magma')
            lc.set_array(x)
            lc.set_linewidth(2)
            self.ax.add_collection(lc)
            self.ax.scatter(x, y, marker='o')
            self.fig.canvas.draw()
            self.fig.canvas.flush_events()

    def motion(self, event):
        if self.status:
            #Append mouse coordinates to array when the mouse moves
            self.capture = self.capture[1:40]
            self.capture = np.append(self.capture, [(event.xdata, event.ydata)], axis=0)

    def configure(self, event):
        #Used to adjust coordinate setting when the screen size changes
        self.width, self.height = self.canvas.get_width_height()

def _quit():
    #destroy window
    root.quit()
    root.destroy()

root = tk.Tk()  
root.wm_title("Mouse Plot")
MG = Mouse_Game(root=root)
MG.frame.pack(expand=True, fill='both')
button = tk.Button(root, text="Quit", command=_quit)
button.pack(side='bottom', pady=10)

tk.mainloop()

我不確定你為什么要轉置數組。 如果你把轉置排除在外,它會工作得很好

points = self.capture.reshape(-1, 1, 2)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM