如何在 pyplot 中使用 Line Collection

臭虫

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Docker容器中使用matplotlib.pyplot?

如何在循环中使用 Collection

如何在MongoDB中使用collection.Aggregate.Project()?

如何在collection_radio_buttons中使用枚举

如何在JavaFX中使用True Type Collection(.ttc)?

如何在tensorflow占位符中使用get_collection

如何在intellij中使用scala.collection.JavaConverters?

如何在pyplot.plot中使用可变数字参数?

由于 figsize 不起作用,如何在 python 中使用 pyplot 使图形的 x 轴更宽

如何使用pyplot在线绘制?

如何在 highchart 的标签中使用 line-height 属性

如何在Python中使用Line Protocol写入influxdb

如何在Android中使用two_line_list_item?

如何在shell脚本中使用oracle $$ PLSQL_LINE?

如何在CKEditor中使用Line Height插件?

如何在pylab(pyplot)中使用阶梯线(阶梯曲线)填充两种不同颜色的区域?

如何在Laravel Collection上使用OR

如何在猫鼬中使用collection.insert()插入自定义ObjectId

如何在Java代码中使用scala.collection.immutable.List

如何在Laravel Eloquent Collection的每种方法中使用break或Continue?

如何在Swift 3的Collection View Controller中使用自定义单元格

如何在Rails中使用嵌套属性编写collection_select

如何在laravel中使用collection合并数组,使之成为key?

在matplotlib中使用pyplot.plot时如何删除圆形标记的轮廓

如何在switch语句中的案例中使用Line.Contains()?

如何在字符串中使用编译时常量 __LINE__ [PHP]

如何在JavaFx中使用箭头键更改Line的起点和终点?

如何在绘图中使用geom_line添加连续线

如何在 pine 腳本 4 中使用繪圖的 style=line