1

I need help, I am trying to figure out why the style i chose in mplfinance is not working when I plot it into a tkinter canvas plot.

This is my code:

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()```

1 Answers1

0

The the style you chose in mplfinance is not working because you have created the Figure object external to mplfinance.

Instead of fig = plt.Figure(figsize=(7,5), dpi=100), try

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)

You can then remove the style= kwarg from the call to mpf.plot().

Daniel Goldfarb
  • 6,937
  • 5
  • 29
  • 61