-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpygraph1.2.py
More file actions
239 lines (189 loc) · 6.08 KB
/
pygraph1.2.py
File metadata and controls
239 lines (189 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!-- coding: utf-8 --
import sys
import re
from PyQt5 import QtWidgets, QtCore
from numpy import *
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.style as stl
import matplotlib.ticker as tck
from polytofunction import polytofunction
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setupUI()
self.count = 0
def setupUI(self):
self.setGeometry(200, 300, 410, 500)
self.inputText = QtWidgets.QLineEdit("", self)
self.inputText.setGeometry(QtCore.QRect(30, 30, 300, 30))
self.statusBar = QtWidgets.QStatusBar(self)
self.setStatusBar(self.statusBar)
# self.inputText.textChanged.connect(self.inputTextChanged)
# self.showAxisCheckBox = QtWidgets.QCheckBox("축 보이기", self)
# self.showAxisCheckBox.setGeometry(QtCore.QRect(40, 70, 150, 30))
# self.showAxisCheckBox.stateChanged.connect(self.showAxisCheckBoxChanged)
self.showGridCheckBox = QtWidgets.QCheckBox("격자 보이기", self)
self.showGridCheckBox.setGeometry(QtCore.QRect(40, 70, 150, 30))
self.showGridCheckBox.stateChanged.connect(self.showGridCheckBoxChanged)
self.piAxisCheckBox = QtWidgets.QCheckBox("x축 단위 π", self)
self.piAxisCheckBox.setGeometry(QtCore.QRect(200, 70, 150, 30))
self.graphLabel = QtWidgets.QLabel("", self)
self.graphLabel.setGeometry(QtCore.QRect(30, 120, 390, 400))
self.graphLabel.setAlignment(QtCore.Qt.AlignTop)
self.okButton = QtWidgets.QPushButton("OK", self)
self.okButton.setGeometry(QtCore.QRect(340, 30, 40, 30))
self.okButton.clicked.connect(self.okButtonClicked)
self.object_labels = []
self.function_samples = ['f', 'g', 'h']
self.valuable_samples = ['a', 'b', 'c', 'd', 'e']
self.function_count = 0
self.valuable_count = 0
self.label_text = ""
self.fig = plt.figure(figsize=(9, 9))
self.ax = self.fig.add_subplot(1,1,1)
self.piAxisCheckBox.stateChanged.connect(self.piXAxis)
def functionLabelArray(self, text):
pass
def graph2dInit(self):
plt.xlim(-5, 5)
plt.ylim(-5, 5)
axisShow(self.ax)
self.count += 1
def showGridCheckBoxChanged(self):
if self.count > 0:
if self.showGridCheckBox.isChecked()==True:
gridShow(self.ax)
else: gridHide(self.ax)
def showAxisCheckBoxChanged(self):
if self.count > 0:
if self.showAxisCheckBox.isChecked()==True:
axisShow(self.ax)
plt.show()
else:
axisHide(self.ax)
plt.show()
def okButtonClicked(self):
if self.count == 0:
self.graph2dInit()
graph_text = self.inputText.text()
graph = graphJudge(graph_text)
if graph == 'x':
self.label_text += makeLabelText(self.function_count,
self.function_samples, len(self.function_samples))
self.label_text += ("(x) = "+ graph_text + "\n")
self.graphLabel.setText(self.label_text)
self.function_count += 1
drawGraphX(graph_text, self.ax)
elif graph == 'xy':
self.label_text += graph_text+"\n"
self.graphLabel.setText(self.label_text)
graph_text = polytofunction(graph_text)[:-1]
drawGraphXY(graph_text, self.ax)
def piXAxis(self):
if self.piAxisCheckBox.isChecked:
# self.ax.xaxis.set_major_formatter(tck.FormatStrFormatter(r'%g$\pi$'))
# self.ax.xaxis.set_major_locator(tck.MultipleLocator(base=1.0))
# plt.style.use("ggplot")
xtics = arange(-30*pi, 30*pi, pi)
xticlabel = []
for xt in arange(-30, 30, 1):
xticlabel.append('$'+str(xt)+r'\pi$')
plt.xticks(xtics, xticlabel)
else:
self.ax.xaxis.set_major_formatter(tck.FormatStrFormatter(r'%g'))
self.ax.xaxis.set_major_locator(tck.MultipleLocator(base=pi))
plt.style.use("ggplot")
def makeLabelText(count, samples, lens):
if count < lens:
return samples[count]
else:
return samples[count%lens] + str(int(count/lens))
# def inputTextChanged(self):
# if self.count > 0:
# graph_text = self.inputText.text()
# drawGraph(graph_text, self.ax)
def axisShow(ax):
ax.spines['left'].set_position(('data', 0))
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position(('data', 0))
ax.spines['top'].set_color('none')
ax.spines['left'].set_smart_bounds(True)
ax.spines['bottom'].set_smart_bounds(True)
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
def axisHide(ax):
pass
def gridShow(ax):
ax.grid()
def gridHide(ax):
ax.grid(False)
def drawGraphX(graph_text, ax):
x = arange(-100, 100, 0.01)
# if graphPiJudge: x = arange(-pi*100, pi*100, 0.01*pi)
# else: x = arange(-100, 100, 0.01)
# if len(self.ax.lines) > 0:
# self.ax.lines.pop(0)
try: y = eval(graph_text.replace("^", "**"))
except: print('error')
# if graphPiJudge:
# plt.plot(x/pi, y)
# plt.ylim(-5*pi, 5*pi)
# piXAxis(plt, ax)
# else:
# plt.plot(x, y)
plt.plot(x, y)
plt.show()
def drawGraphXY(graph_text, ax):
x = arange(-100, 100, 0.01)
# if graphPiJudge: x = arange(-pi*100, pi*100, 0.01*pi)
# else: x = arange(-100, 100, 0.01)
try:
y = eval(graph_text.replace("^", "**"))
ym = eval("(-1)*("+(graph_text.replace("^", "**"))+")")
except: print('error')
# if graphPiJudge:
# plt.plot(x/pi, y)
# plt.plot(x/pi, ym)
# plt.ylim(-5*pi, 5*pi)
# piXAxis(plt, ax)
# else:
# plt.plot(x, y)
# plt.plot(x, ym)
plt.plot(x, y)
plt.plot(x, ym)
plt.show()
def graphPiJudge(graph_text):
repi = re.compile('.*[pi]+.*')
resin = re.compile('.*[sin]+.*')
recos = re.compile('.*[cos]+.*')
retan = re.compile('.*[tan]+.*')
if repi.match(graph_text) or resin.match(graph_text) or recos.match(graph_text) or retan.match(graph_text):
return True
else:
return False
def graphJudge(graph_text):
#define graphing regural extension
rex = re.compile('.*[x]+.*')
rey = re.compile('.*[y]+.*')
rez = re.compile('.*[z]+.*')
ren = re.compile('.*[0-9]+.*')
reu = re.compile('^[a-zA-Z]+[0-9]*.*')
if graph_text == "":
return ""
elif rez.match(graph_text): #valuable xyz
return "xyz"
elif rey.match(graph_text): #valuable xy
return "xy"
elif rex.match(graph_text): #valuable x
return "x"
elif ren.match(graph_text): #number
return "n"
elif reu.match(graph_text): #user valuable
return "u"
else: return ""
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
mainwindow = MainWindow()
mainwindow.show()
app.exec_()