Я полагаю, что нужно что-то сделать с интеграцией функции рисунка с .gca() и . gcf(), так как мне не приходится иметь дело с Fig(), когда у меня есть отдельное окно matplotlib. Я пробовал сделать:
Код: Выделить всё
fig.plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
fig.plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=7))
fig.plt.gcf().autofmt_xdate(rotation=30)
Затем я попытался удалить .plt и оставить только .fig, однако я также получил:
AttributeError: объект «Рисунок» не имеет атрибута «gcf»
Мой следующий код того, что я пытаюсь сделать, находится ниже:
< pre class="lang-py Prettyprint-override">
Код: Выделить всё
def matplot(self,parent):
company = yf.Ticker('aapl')
stock_historical = yf.download('aapl', start='2021-08-13', end='2021-10-15', interval='1d')
get_y = stock_historical
opening_days = get_y['Open']
y_axiz = opening_days.values.tolist()
y_axis = []
for day in opening_days:
y_axis.append(round(day, 2))
stock_historical.reset_index(inplace=True, drop=False)
data = []
for i in stock_historical['Date']:
data.append(i)
x_axis = []
for date in data:
parsed_data = date.to_pydatetime()
convert_date = str(parsed_data.strftime('%Y/%m/%d'))
x_axis.append(convert_date)
#print(x_axis)
x = [dt.datetime.strptime(d, '%Y/%m/%d').date() for d in x_axis]
# tkinter frame slave of root
fig = Figure(figsize=(4, 4), dpi=100)
new_frame = Frame(self.detailed_frame,width=150,height=150)
new_frame.pack()
test_label = Label(new_frame,text='hello',font='Arial 16 bold')
test_label.pack()
# x_axis shenanigans not wanting to work
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=7))
plt.gcf().autofmt_xdate(rotation=30)
fig.add_subplot(111).plot(x, y_axis)
# embed into tkinter
canvas = FigureCanvasTkAgg(fig, master = new_frame)
canvas.draw()
get_widz = canvas.get_tk_widget()
get_widz.pack(side=TOP, fill=BOTH, expand=1)
toolbar = NavigationToolbar2Tk(canvas, self.detailed_frame)
toolbar.update()
canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
Подробнее здесь: https://stackoverflow.com/questions/695 ... in-tkinter