передо мной стоит задача:
Создать класс с 3 методами
1 метод genPoints записывает в файл x значения от 0 до 99, в файл y рандомные двузначные значения.
2 метод drawPlot строит график по x и y из файлов.
3 метод scrPlot сохраняет график ввиде картинки.
У меня проблема в связи между методом 2 и 3. Я конечно наговнокодил так, что в методе drawPlot я создаю график и отображаю его, а в scrPlot я делаю тоже самое, но не отображаю, а сохраняю.
Вопрос: как передать график в метод scrPlot, чтобы в методе было мало строк кода (чтобы было что-то на подобие такого plt.savefig('foo.png') без повторного plt.plot и сбора из текстового файла координат)
import matplotlib.pyplot as plt
import random as rand
class GDS_Plot:
def __init__(self):
pass
def genPoints(self):
xFile = open("x", "w")
for i in range(0,100,1):
xFile.write(str(i))
xFile.write("\n")
yFile = open("y", "w")
for i in range(0,100,1):
yFile.write(str(rand.randint(10, 99)))
yFile.write("\n")
def drawPlot(self):
xFile = open("x", "r")
xVal = xFile.read()
xVal_list = xVal.split('\n')
xVal_list.pop();
xFile.close()
for i in range(0, 100, 1):
xVal_list[i] = int(xVal_list[i])
# print(xVal_list)
yFile = open("y", "r")
yVal = yFile.read()
yVal_list = yVal.split('\n')
yVal_list.pop();
yFile.close()
for i in range(0, 100, 1):
yVal_list[i] = int(yVal_list[i])
plt.plot(xVal_list, yVal_list)
plt.show()
def scrPlot(self):
xFile = open("x", "r")
xVal = xFile.read()
xVal_list = xVal.split('\n')
xVal_list.pop();
xFile.close()
for i in range(0, 100, 1):
xVal_list[i] = int(xVal_list[i])
# print(xVal_list)
yFile = open("y", "r")
yVal = yFile.read()
yVal_list = yVal.split('\n')
yVal_list.pop();
yFile.close()
for i in range(0, 100, 1):
yVal_list[i] = int(yVal_list[i])
# print(yVal_list)
plt.plot(xVal_list, yVal_list)
plt.savefig('foo.png')
Plot = GDS_Plot()
Plot.genPoints()
Plot.drawPlot()
Plot.scrPlot()
.