1

我需要帮助,我试图弄清楚为什么我在 mplfinance 中选择的样式在我将其绘制到 tkinter 画布图时不起作用。

这是我的代码:

from tkinter import *
import datetime as dt
import yfinance as yf
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
import mplfinance as mpf

gui = Tk()
gui.geometry("800x700")
stock = 'SPY'
end = dt.datetime.now()
start = end - dt.timedelta(days=6)
df = yf.download(stock.upper(), start, end, interval = '5m')

def Plot_data(type='candle', grid= '-', style='nightclouds'):
    fig = plt.Figure(figsize=(7,5), dpi=100)
    chart = fig.add_subplot(111)
    chart_canvas = FigureCanvasTkAgg(fig, master=gui)
    chart_canvas.get_tk_widget().place(relx = .05, rely=.25)
    s = mpf.make_mpf_style(base_mpf_style=style, gridstyle=grid)
    mpf.plot(df, ax= chart, type = type, style = s)

Plot_data()
mainloop()



#Plotting it without tkinter and without the lines "" fig = plt.Figure()/
#chart = fig.add_subplot(111)"" works just fine,
#this is the customization I want in the tkinter window. Thank you.


def Plot_data(type='candle', grid= '', style='nightclouds'):
    s = mpf.make_mpf_style(base_mpf_style=style, gridstyle=grid)
    mpf.plot(df, type = type, style = s)

Plot_data()```
4

1 回答 1

0

您在 mplfinance 中选择的样式不起作用,因为您在 mplfinance 外部创建了 Figure 对象。

而不是fig = plt.Figure(figsize=(7,5), dpi=100),尝试

def Plot_data(type='candle', grid= '-', style='nightclouds'):
    s = mpf.make_mpf_style(base_mpf_style=style, gridstyle=grid)
    fig = mpf.figure(figsize=(7,5), dpi=100, style=s)

然后,您可以从对 的调用中删除style=kwarg mpf.plot()

于 2021-08-15T03:32:10.570 回答