Python 如何将线集合与pyplot一起使用

Python 如何将线集合与pyplot一起使用,python,matplotlib,Python,Matplotlib,运行代码时,错误应该非常明显,只需在黑色空间中单击鼠标并将其移动即可。我不确定线段是如何创建的,我从库中提取了它,这有点让人困惑。如何使线段沿散点图绘制?当我看到正在生成的段数组时,我相信它正在创建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 matplotl

运行代码时,错误应该非常明显,只需在黑色空间中单击鼠标并将其移动即可。我不确定线段是如何创建的,我从库中提取了它,这有点让人困惑。如何使线段沿散点图绘制?当我看到正在生成的段数组时,我相信它正在创建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)

谢谢如果你能帮我弄清楚为什么散点图在点(0,0)处初始化,即使我指定了一个不包含这些点的数组,我也会给你加分。