【问题标题】:Plotting with FigureCanvasTkAgg overrides mplfinance custom color and grid使用 FigureCanvasTkAgg 绘图会覆盖 mplfinance 自定义颜色和网格
【发布时间】:2021-08-14 23:33:52
【问题描述】:

我需要帮助,我试图弄清楚为什么我在 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()```

【问题讨论】:

    标签: python matplotlib tkinter mplfinance stock-data


    【解决方案1】:

    您在 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() 的调用中。

    【讨论】:

    • 这完全解决了我的问题。非常感谢!
    • @JorgeAlvarez 很高兴它有帮助。如果是这样,请为答案投票单击答案旁边的复选标记。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 2021-11-02
    • 2017-02-25
    • 2020-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多