首页 > 代码库 > python 下 tinker、matplotlib 混合编程示例一个
python 下 tinker、matplotlib 混合编程示例一个
该例是实现了 Tinker 嵌入 matplotlib 所绘制的蜡烛图(k 线),数据是从 csv 读入的。花一下午做的,还很粗糙,仅供参考。python 代码如下:
import matplotlibimport matplotlib.dates as mdatesimport pandas as pdfrom matplotlib.figure import Figurefrom matplotlib.finance import candlestick_ohlcfrom matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAggfrom matplotlib.backend_bases import key_press_handlerfrom numpy import arange, sin, pimatplotlib.use(‘TkAgg‘)import sysif sys.version_info[0] < 3: import Tkinter as Tkelse: import tkinter as Tkroot = Tk.Tk()root.wm_title("CNY exchange rate")
root.iconbitmap(‘my_logo.ico‘)# read datadata = pd.read_csv("data.csv", index_col=0, parse_dates=True)data = data.reset_index()data[‘Date2‘] = data[‘Date‘].apply(lambda d: mdates.date2num(d.to_pydatetime()))tuples = [tuple(x) for x in data[[‘Date2‘,‘Open‘,‘High‘,‘Low‘,‘Close‘]].values]# drawArea setupdrawArea = Figure(figsize=(10, 5), dpi=100)ax = drawArea.add_axes([0.1, 0.2, 0.85, 0.7])ax.spines[‘right‘].set_color(‘none‘)ax.spines[‘top‘].set_color(‘none‘)ax.xaxis.set_ticks_position(‘bottom‘)ax.yaxis.set_ticks_position(‘left‘)ax.tick_params(axis=‘both‘, direction=‘out‘, width=2, length=8, labelsize=12, pad=8)ax.spines[‘left‘].set_linewidth(2)ax.spines[‘bottom‘].set_linewidth(2)ax.set_xticklabels(data[‘Date2‘], rotation=45, horizontalalignment=‘right‘)ax.xaxis_date()ax.xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m-%d"))ax.set_ylabel(‘Price ($)‘, size=17)candlestick_ohlc(ax, tuples, width=.5, colorup=‘r‘, colordown=‘g‘);# a tk.canvascanvas = FigureCanvasTkAgg( drawArea, master=root)canvas.show()canvas.get_tk_widget().grid(row=1, columnspan=4)toolbar = NavigationToolbar2TkAgg(canvas, root)toolbar.update()toolbar.grid(row=2, columnspan=4)#canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)def _refresh(): ax.clear() data = pd.read_csv("data2.csv", index_col=0, parse_dates=True) data = data.reset_index() data[‘Date2‘] = data[‘Date‘].apply(lambda d: mdates.date2num(d.to_pydatetime())) tuples = [tuple(x) for x in data[[‘Date2‘,‘Open‘,‘High‘,‘Low‘,‘Close‘]].values] ax.spines[‘right‘].set_color(‘none‘) ax.spines[‘top‘].set_color(‘none‘) ax.xaxis.set_ticks_position(‘bottom‘) ax.yaxis.set_ticks_position(‘left‘) ax.tick_params(axis=‘both‘, direction=‘out‘, width=2, length=8, labelsize=12, pad=8) ax.spines[‘left‘].set_linewidth(2) ax.spines[‘bottom‘].set_linewidth(2) ax.set_xticklabels(data[‘Date2‘], rotation=45, horizontalalignment=‘right‘) ax.xaxis_date() ax.xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m-%d")) ax.set_ylabel(‘Price ($)‘, size=17) candlestick_ohlc(ax, tuples, width=.5, colorup=‘r‘, colordown=‘g‘); canvas.show()Tk.Label(root,text=‘Money‘).grid(row=0,column=0)defaultVar = Tk.StringVar(root)defaultVar.set("CNY") # default valuemoneyList = Tk.OptionMenu(root, defaultVar, "CNY", "USD", "EUR")moneyList.grid(row=0, column=1)btnRefresh = Tk.Button(master=root, text=‘Refresh‘, command=_refresh)btnRefresh.grid(row=0, column=3)Tk.mainloop()
可以用来调试的数据一例,命名为 data.csv,与上面文件同目录即可:
Date,Open,High,Low,Close2010-01-04,30.49,30.64,30.34,30.572010-02-04,30.50,31.64,20.34,25.572010-03-04,30.49,30.64,30.34,30.572010-04-04,30.49,30.64,30.34,30.572010-05-04,30.49,30.64,30.34,30.572010-06-04,30.49,30.64,30.34,30.572010-07-04,30,30.64,30.34,30.572010-08-04,20.49,30.64,30.34,30.572010-09-04,10.49,30.64,30.34,30.57
python 下 tinker、matplotlib 混合编程示例一个
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。