【问题标题】:add Seaborn Scatter plot to tkinter gui将 Seaborn 散点图添加到 tkinter gui
【发布时间】:2022-01-17 05:14:40
【问题描述】:

我想将此散点图添加到我的 tkinter gui。我在我的 jupyter notebook 上运行了它,它可以工作,但我不确定如何将它实现到 tkinter gui。

enter image description here

这是图表应该是的窗口 enter image description here

这是我的 jupyter 笔记本上的代码:

### Import libraries
import seaborn as sns 

import matplotlib.pyplot as plt 

import pandas as pd 

### Import dataset
players_df = pd.read_csv("player_locations.txt") 
players_df 


### Draw Seaborn Scatter Plot to find location between lat and long

sns.scatterplot(x = players_df["longitude"], y = players_df["latitude"], hue = players_df["Player"])

这是我的 gui 上用于分析窗口的代码:

from tkinter import *

import matplotlib

import matplotlib.pyplot as plt

import numpy as np

import csv

import pandas as pd

import seaborn as sns

from matplotlib import animation

from matplotlib.animation import FuncAnimation

from matplotlib import style


players_df = pd.read_csv("player_locations.txt") 
players_df

class addAnalysis(Toplevel):

    def __init__(self):
        Toplevel.__init__(self)
        self.geometry("650x650+620+200")
        self.title("Analysis")
        self.resizable(False,False)
        
        
         #Frames
        self.top=Frame(self,height=150,bg='white')
        self.top.pack(fill=X)
        self.bottomFrame=Frame(self,height=500,bg='#f1dead')
        self.bottomFrame.pack(fill=X)
        
        #heading
        self.heading = Label(self.top, text='Player Analysis', font='arial 25 bold',
                             fg='#000000', bg='white')
        self.heading.place(x=230, y=60)
        
        
        #buttons
        graph_button = Button(self.bottomFrame,text='   Graph  ',
                      font='arial 14 bold',command = self.plot)
        graph_button.place(x=10,y=380)



        exit_button = Button(self.bottomFrame,text='    Exit     ',
                          font='arial 14 bold',command = self.destroy)
        exit_button.place(x=10,y=440)
        
#         this is the part where I implement the scatter plot
    def plot():
         # Draw Seaborn Scatter Plot to find location between lat and long
         scatter = self.sns.scatterplot(x = players_df["longitude"], y = players_df["latitude"], hue = players_df["Player"])
         

【问题讨论】:

    标签: python user-interface tkinter plot seaborn


    【解决方案1】:

    我最近了解到 tk 可以导入 PIL 图像。我需要将带有原始 RGB 数据的 numpy 数组转换为 tk,建议使用 PIL.fromarray() 创建 PIL 图像,然后使用 ImageTk.PhotoImage() 将 PIL 图像导入 tk。我希望这些信息对您有用。

    【讨论】:

    • 我正在提取数据,所以点总是在变化,所以图像看起来不适合这种情况
    • 是的,每次数据元素发生变化时,您都需要从数据中重新创建图像。
    • 如果我不会每次都创建图像,您还能想到其他方法吗?
    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    • 我能想到的唯一方法是添加一个可以编辑像素的帧缓冲区。但是图像必须每隔几毫秒发送一次屏幕(如果您想要实时更新),这也意味着将整个图像转换为可以由 tk 或其他 GUI 模块显示的格式。据我所知(我绝不是专家,我大约 5 个月前开始使用 python)
    猜你喜欢
    • 2019-04-15
    • 2015-01-18
    • 2019-08-17
    • 2019-09-04
    • 2023-04-10
    • 2017-12-04
    • 1970-01-01
    • 2020-12-04
    • 2012-05-18
    相关资源
    最近更新 更多