This repository has been archived on 2024-06-23. You can view files and clone it, but cannot push or open issues or pull requests.
mathdeptv2/工具v2/生成直方图代码.py

106 lines
4.4 KiB
Python

from PySide6.QtWidgets import QWidget, QApplication, QFileDialog
from Ui_生成直方图代码 import Ui_Form
import re
class MyWindow(QWidget,Ui_Form):
def __init__(self):
super().__init__()
self.setupUi(self)
self.bind()
def bind(self):
self.pushButton_generatexlist.clicked.connect(self.generateXList)
self.pushButton_exec.clicked.connect(self.exec)
def generateXList(self):
self.xmin = int(self.lineEdit_xmin.text())
self.xmax = int(self.lineEdit_xmax.text())
self.xwidth = int(self.lineEdit_xwidth.text())
self.xlist = list(range(self.xmin,self.xmax,self.xwidth))
output = ""
for x in self.xlist:
output += f"({x},)[]\n"
self.plainTextEdit_yinfo.setPlainText(output)
def folding(self):
if self.xmin <= self.xwidth:
foldingstr = ""
else:
midpoint = 1/2 * (self.xstart + self.xmin)
dist = (midpoint - self.xstart) / 6
foldingstr = ""
height = 0.2/self.yscale
foldingstr += f" -- ({(self.xstart+dist*4):.2f},0)"
foldingstr += f" -- ({(self.xstart+dist*5):.2f},{height:.4f})"
foldingstr += f" -- ({(self.xstart+dist*7):.2f},{-height:.4f})"
foldingstr += f" -- ({(self.xstart+dist*8):.2f},0)"
return foldingstr
def exec(self):
xlabel = self.lineEdit_xlabel.text()
coordinateinfo = self.plainTextEdit_yinfo.toPlainText()
if self.xmin < self.xwidth: #计算x轴的起点坐标
self.xstart = 0
else:
self.xstart = self.xmin - self.xwidth
self.xmax = self.xlist[-1] + self.xwidth
self.xend = self.xmax + self.xwidth #计算x轴的终点坐标
self.xscale = 5/(self.xend-self.xstart) #计算x放缩比例
# print(self.xscale)
self.coordinatelist = re.findall(r"\(([\d\.]+),([\d\.]+)\)\[([\S]*)\]",coordinateinfo) #计算坐标信息
for cindex in range(len(self.coordinatelist)):
x,y,label = self.coordinatelist[cindex]
if label.strip() == "":
self.coordinatelist[cindex] = (x,y,y)
self.coordinatestr_raw = []
for coord in self.coordinatelist:
x,y,label = coord
self.coordinatestr_raw.append(f"{x}/{y}")
self.coordinatestr = ",".join(self.coordinatestr_raw)
# print(self.coordinatelist)
self.dasheddict = {} #计算以纵坐标为key的虚线信息, 内容为(x,label)
for coord in self.coordinatelist:
x,y,label = coord
if not y in self.dasheddict:
self.dasheddict[y] = (x,label)
else:
if x > self.dasheddict[y][0] and label == y:
self.dasheddict[y] = (x,self.dasheddict[y][1])
elif x > self.dasheddict[y][0] and label != y:
self.dasheddict[y] = (x,label)
self.dashedstr_raw = []
for y in self.dasheddict:
x,label = self.dasheddict[y]
self.dashedstr_raw.append(f"{x}/{y}/{label}")
self.dashedstr = ",".join(self.dashedstr_raw)
# print(self.dasheddict)
ymax = max([float(i) for i in self.dasheddict.keys()])
self.yscale = 3/ymax #3是y的长度, 计算y放缩比例
self.yend = ymax + 1/float(self.yscale)
latexcode = f"""\\begin{{center}}
\\begin{{tikzpicture}}[>=latex, xscale = {self.xscale:.4f}, yscale = {self.yscale:.4f}]
\\draw [->] ({self.xstart},0) {self.folding()}-- ({self.xend},0) node [below right] {{{xlabel}}};
\\draw [->] ({self.xstart},0) -- ({self.xstart},{self.yend:.2f}) node [left] {{$\\dfrac{{\\text{{频率}}}}{{\\text{{组距}}}}$}};
\\draw ({self.xstart},0) node [below] {{$0$}};
\\foreach \\i/\\j in {{{self.coordinatestr}}}
{{\\draw (\\i,0) node [below] {{$\\i$}} --++ (0,\\j) --++ ({self.xwidth},0) --++ (0,-\\j);}};
\\draw ({self.xmax},0) node [below] {{${self.xmax}$}};
\\foreach \i/\j/\k in {{{self.dashedstr}}}
{{\\draw [dashed] (\\i,\\j) -- ({self.xstart},\\j) node [left] {{$\\k$}};}};
\\end{{tikzpicture}}
\\end{{center}}"""
latexcode = re.sub(r"\n\s+","\n",latexcode)
self.plainTextEdit_latexcode.setPlainText(latexcode)
if __name__ == '__main__':
app = QApplication([])
windows = MyWindow()
windows.show()
app.exec()