Python under Tinker, matplotlib mixed Programming Example One

Source: Internet
Author: User

The example is a candlestick (K-line) drawn with the Tinker embedded matplotlib, and the data is read from the CSV. It's a rough afternoon, and it's just for reference. The Python code is as follows:

Import Matplotlibimport matplotlib.dates asMdatesimport Pandas asPD frommatplotlib.figure Import figure frommatplotlib.finance Import CANDLESTICK_OHLC frommatplotlib.backends.backend_tkagg import Figurecanvastkagg, Navigationtoolbar2tkagg frommatplotlib.backend_bases Import Key_press_handler fromnumpy import Arange, sin, Pimatplotlib.use ('Tkagg') Import sysifsys.version_info[0] <3: Import Tkinter asTkElse: Import Tkinter asTkroot=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) forXinchdata[['Date2','Open',' High',' Low','Close']].values]# Drawarea Setupdrawarea= Figure (Figsize= (Ten,5), dpi= -) 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= A, Pad=8) ax.spines[' Left'].set_linewidth (2) ax.spines['Bottom'].set_linewidth (2) ax.set_xticklabels (data['Date2'], rotation= $, horizontalalignment=' Right') ax.xaxis_date () Ax.xaxis.set_major_formatter (mdates. Dateformatter ("%y-%m-%d")) Ax.set_ylabel ('Price ($)', size= -) 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) forXinchdata[['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= A, Pad=8) ax.spines[' Left'].set_linewidth (2) ax.spines['Bottom'].set_linewidth (2) ax.set_xticklabels (data['Date2'], rotation= $, horizontalalignment=' Right') ax.xaxis_date () Ax.xaxis.set_major_formatter (mdates. Dateformatter ("%y-%m-%d")) Ax.set_ylabel ('Price ($)', size= -) 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") #defaultvaluemoneylist= 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 ()

One example of data that can be debugged is named Data.csv, which is the same directory as the file above:

Date,open,high,low,close .- on-Geneva,30.49,30.64,30.34,30.57 .- Geneva-Geneva,30.50,31.64,20.34,25.57 .-Geneva-Geneva,30.49,30.64,30.34,30.57 .-Geneva-Geneva,30.49,30.64,30.34,30.57 .- to-Geneva,30.49,30.64,30.34,30.57 .- .-Geneva,30.49,30.64,30.34,30.57 .- --Geneva, -,30.64,30.34,30.57 .- ,-Geneva,20.49,30.64,30.34,30.57 .- the-Geneva,10.49,30.64,30.34,30.57

Python under Tinker, matplotlib mixed Programming Example One

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.