![](/img/trans.png)
[英]Plot n animated growing line plots on the same graph (matplotlib, python)
[英]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.