繁体   English   中英

Matplotlib 图形标记越来越大

[英]Matplotlib Graph Markers Growing In Size

我正在为 Stepmania 制作解析器,并使用程序编译的数据变成图形。 我试图使散点图在向上移动时不会使点变大,但我不确定如何做到这一点。 我查看了 matplotlib 标记大小修饰符,但它在我的 function 中不起作用,我想我需要有人向我解释一下。 请帮忙。

from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from PIL import ImageTk, Image
import numpy as np
import matplotlib.pyplot as plt
import os
import glob
import matplotlib

directions = []
nameDirections = []
    
root = Tk()
nameDirections = ["left", "down", "up", "right"]
directions = []
#NameDirections are the type of arrows that are being measured. 
#Directions contains the numbers that measure the amount of times each direction is in the text file for a Dance Dance Revolution game code

e = Entry(root, width=50)
e.pack()
root.title("Insert GUI Title Here")
root.geometry('600x400')
root.resizable(False, False)

#Size of GUI


def openFolder():
    folPath = filedialog.askdirectory()
    return folPath

def openSmFile():
    folPath = filedialog.askopenfilename()
    return folPath

#Opens an SM file (Stepmania code file)

def checkDirections():
    folPath = openFolder()
    for fpath in glob.iglob(f'{folPath}/*'):
        if (fpath.endswith('.sm')):
            file = open(fpath, "r")
            lines = []
            lines = file.readlines()

            left = 0
            down = 0
            up = 0
            right = 0
            beats = 0

            for line in lines:
                i = 0
                if not ("," in line or "." in line or "#" in line or ";" in line or "-" in line or line == "" or len(line) != 5):
                    for alpha in line:
                        if i == 0 and alpha != "0":
                            left += 1
                        if i == 1 and alpha != "0":
                            down += 1
                        if i == 2 and alpha != "0":
                            up += 1
                        if i == 3 and alpha != "0":
                            right += 1
                        i += 1
                        beats += 1
            print("The file name is " + str(fpath))
            print("There are " + str(left) + " lefts in this song.")
            print("There are " + str(down) + " downs in this song.")
            print("There are " + str(up) + " ups in this song.")
            print("There are " + str(right) + " rights in this song.")
            print("There are " + str(beats) + " beats.")
            


#Prints number of each type of arrow. This was taken from code I wrote earlier and I just changed it up for the near-identical function below that returns instead
#Because i was too lazy to make a good solution


def graph(thing):

            fpath = openSmFile()
            if (fpath.endswith('.sm')):
                file = open(fpath, "r")
                lines = []
                lines = file.readlines()

                left = 0
                down = 0
                up = 0
                right = 0
            
                for line in lines:
                    i = 0
                    if not ("," in line or "." in line or "#" in line or ";" in line or "-" in line or line == ""):
                        for alpha in line:
                            if i == 0 and alpha != "0":
                                left += 1
                            if i == 1 and alpha != "0":
                                down += 1
                            if i == 2 and alpha != "0":
                                up += 1
                            if i == 3 and alpha != "0":
                                right += 1
                            i += 1
                directions = [left, down, up, right]
                plt.title(str(os.path.basename(fpath)))
                if (thing == "bar"):
                    plt.bar(nameDirections, directions)
                if (thing == "pie"):
                    plt.pie(directions, labels=nameDirections, autopct='%1.1f%%',shadow=True, startangle=90)
                if (thing == "scatter"):
                    plt.scatter(directions, nameDirections)
            else:
                print("This file is not valid.")


def ScatterTime():
            fpath = openSmFile()
            if (fpath.endswith('.sm')):
                file = open(fpath, "r")
                lines = []
                lines = file.readlines()
                beats = []
                arrowDirections = []
                beat = 1
               
                for line in lines:
                    i = 0
                    if not ("," in line or "." in line or "#" in line or ";" in line or "-" in line or line  == "" or ":" in line or len(line) != 5):  
                        for alpha in line:
                            if i == 0:
                                arrowDirections.append("left")
                            if i == 1:
                                arrowDirections.append("down")
                            if i == 2:
                                arrowDirections.append("up")
                            if i == 3: 
                                arrowDirections.append("right")      
                            i += 1
                            if i != 5:
                                if (alpha != "0"):
                                    beats.append(beat)     
                                if (alpha == "0"):
                                    beats.append(0)
                                beat += 1
                                
                    plt.title(str(os.path.basename(fpath)))
                    fullrange = list(range(1, beat))
                    u, ind = np.unique(arrowDirections, return_inverse=True)
                    plt.xticks(range(len(u)), u)
                    plt.scatter(ind, fullrange, s=beats, marker = ".",)
                    
            else:
                print("This file is not valid.")

        #Creates a scatterplot


def testFunction():
    fpath = openSmFile()    
    if (fpath.endswith('.sm')):
        file = open(fpath, "r")
        lines = []
        lines = file.readlines()
        for line in lines:
            i = 0
            if not ("," in line or "." in line or "#" in line or ";" in line or "-" in line or line  == "" or ":" in line or len(line) != 5):  
                print(line)
                print(len(line))
    else:
        print("This file is not valid.")

    #Not relevant. Tests arrays in function

            

  

def bar():
    graph("bar")
    plt.show()
    
#Creates a bar graph


def ILovePie():
    graph("pie")
    plt.show()

def Scatter():
    ScatterTime()
    plt.show()


#Creates a pie graph


barGraph = Button(root, text="Click to show a bar graph", command=bar)
pieGraph = Button(root, text = "Click to show a pie graph", command = ILovePie)
runThrough = Button(root, text="Click to print number of each arrow", command=checkDirections)
scatterGraph = Button(root, text = "Click to show a scatterplot", command = Scatter)
testButton = Button(root, text = "Test", command = testFunction)
    
barGraph.pack()
runThrough.pack()
pieGraph.pack()
scatterGraph.pack()
testButton.pack()
root.mainloop()

#Creates the buttons to show the graphs and the run text command

ScatterTime()中,您有这一行:

plt.scatter(ind, fullrange, s=beats, marker = ".",)

s对应于标记大小,所以无论beats是什么,这就是标记的大小。 请参阅plt.scatter()上的 matplotlib 文档。

如果您从该行中删除s=beats ,则s的值将默认为None并且您的所有标记最终都将是相同的 matplotlib-default 大小。

删除它会让你得到你想要的吗?

抱歉,我不是 Stepmania 玩家,但是如果您尝试在beats不是0时显示一个点,并且这些点显示的大小相同,您可以将beats.append(beat)更改为beats.append(1)

参数设置标记s大小与参数值成比例,例如,如果我有 4 个数据点,并将它们的大小设置为[10,20,30,40] ,我将得到 output 如下所示示例图

在您的代码中, beats值控制标记的大小,将beats值对齐为1将对齐标记的大小

代码:

def ScatterTime():
            fpath = openSmFile()
            if (fpath.endswith('.sm')):
                file = open(fpath, "r")
                lines = []
                lines = file.readlines()
                beats = []
                arrowDirections = []
                beat = 1
               
                for line in lines:
                    i = 0
                    if not ("," in line or "." in line or "#" in line or ";" in line or "-" in line or line  == "" or ":" in line or len(line) != 5):  
                        for alpha in line:
                            if i == 0:
                                arrowDirections.append("left")
                            if i == 1:
                                arrowDirections.append("down")
                            if i == 2:
                                arrowDirections.append("up")
                            if i == 3: 
                                arrowDirections.append("right")      
                            i += 1
                            if i != 5:
                                if (alpha != "0"):
                                    beats.append(1)     
                                if (alpha == "0"):
                                    beats.append(0)
                                beat += 1
                                
                    plt.title(str(os.path.basename(fpath)))
                    fullrange = list(range(1, beat))
                    u, ind = np.unique(arrowDirections, return_inverse=True)
                    plt.xticks(range(len(u)), u)
                    plt.scatter(ind, fullrange, s=beats*10, marker = ".",)
                    
            else:
                print("This file is not valid.")

        #Creates a scatterplot

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM