Thanks in advance for your precious help !!! I fail to embed matplotlib in tkinter. Can you guide me?
I have imported all the correct modules that are to say matplotlib.pyplot, matplotlib.dates, FigureCanvasTkAgg, NavigationToolbar2Tk, key_press_handler, Figure, etc..
and then...
root = tk.Tk()
root.wm_title("Embedding in Tk")
def bytespdate2num(fmt, encoding ='utf-8'):
strconverter = mdates.strpdate2num(fmt)
def bytesconverter(b):
s = b.decode(encoding)
return strconverter(s)
return bytesconverter
def graph_data(stock):
fig = plt.figure()
ax1 = plt.subplot2grid((1,1), (0,0))
url_stock = 'https://pythonprogramming.net/yahoo_finance_replacement'
source_code = urllib.request.urlopen(url_stock).read().decode()
stock_data = []
source_split = source_code.split('\n')
for line in source_split[1:]:
line_split = line.split(',')
if len(line_split) == 7:
if 'values' not in line and 'labels' not in line:
stock_data.append(line)
date, closep, highp, lowp, openp, adj_closep, volume = np.loadtxt(stock_data, delimiter =',', unpack= True, converters={0: bytespdate2num('%Y-%m-%d')})
ax1.plot_date(date, closep, '-', label ='closing price')
ax1.axhline(closep[0], color='k', linewidth = 2)
ax1.fill_between(date, closep, closep[0], where=(closep > closep[0]), facecolor='g', alpha=0.5)
ax1.fill_between(date, closep, closep[0], where=(closep < closep[0]), facecolor ='r', alpha = 0.5)
ax1.xaxis.label.set_color('c')
ax1.yaxis.label.set_color('r')
ax1.set_yticks([0,100,200,300,400,500,600,700,800,900,1000])
for label in ax1.xaxis.get_ticklabels():
label.set_rotation(45)
ax1.grid(True, color= 'r', linestyle='-', linewidth=0.5)
plt.subplots_adjust(left = 0.09, bottom =0.18, right= 0.94, top= 0.95, wspace=0.2, hspace=0)
plt.title('stock')
plt.xlabel('dates')
plt.ylabel('price')
plt.legend()
plt.show()
here is where things block I think
canvas = FigureCanvasTkAgg(fig, master= root) # A tk.DrawingArea.
canvas.draw()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
toolbar = NavigationToolbar2Tk(canvas, root)
toolbar.update()
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
graph_data('EBAY')
tk.mainloop()
Thank you again ;)