python 下 tinkermatplotlib 混合编程示例一个

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 下 tinkermatplotlib 混合编程示例一个相关的知识,希望对你有一定的参考价值。

该例是实现了 Tinker 嵌入 matplotlib 所绘制的蜡烛图(k 线),数据是从 csv 读入的。花一下午做的,还很粗糙,仅供参考。python 代码如下:

import matplotlib
import matplotlib.dates as mdates

import pandas as pd

from matplotlib.figure import Figure
from matplotlib.finance import candlestick_ohlc
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.backend_bases import key_press_handler

from numpy import arange, sin, pi

matplotlib.use(TkAgg)

import sys
if sys.version_info[0] < 3:
    import Tkinter as Tk
else:
    import tkinter as Tk

root = Tk.Tk()
root.wm_title("CNY exchange rate")
root.iconbitmap(‘my_logo.ico‘) # read data data
= 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 setup drawArea = 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.canvas canvas = 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 value moneyList = 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,Close
2010-01-04,30.49,30.64,30.34,30.57
2010-02-04,30.50,31.64,20.34,25.57
2010-03-04,30.49,30.64,30.34,30.57
2010-04-04,30.49,30.64,30.34,30.57
2010-05-04,30.49,30.64,30.34,30.57
2010-06-04,30.49,30.64,30.34,30.57
2010-07-04,30,30.64,30.34,30.57
2010-08-04,20.49,30.64,30.34,30.57
2010-09-04,10.49,30.64,30.34,30.57

 

以上是关于python 下 tinkermatplotlib 混合编程示例一个的主要内容,如果未能解决你的问题,请参考以下文章

windows和linux下 Python2,Python3 的环境及安装

windows下如何安装Python的库?

windows下安装python包

Windows下安装python2和python3双版本

Linux下安装Python3

小白学 Python(13):基础数据结构(字典)(下)