date_demo1.py

来自「非原创。很好的python例子」· Python 代码 · 共 52 行

PY
52
字号
#!/usr/bin/env python"""Show how to make date plots in matplotlib using date tick locators andformatters.  See major_minor_demo1.py for more information oncontrolling major and minor ticksAll matplotlib date plotting is done by converting date instances intodays since the 0001-01-01 UTC.  The conversion, tick locating andformatting is done behind the scenes so this is most transparent toyou.  The dates module provides several converter functions date2numand num2dateThis example requires an active internet connection since it usesyahoo finance to get the data for plotting"""from pylab import *from matplotlib.finance import quotes_historical_yahoofrom matplotlib.dates import YearLocator, MonthLocator, DateFormatterimport datetimedate1 = datetime.date( 1995, 1, 1 )date2 = datetime.date( 2004, 4, 12 )years    = YearLocator()   # every yearmonths   = MonthLocator()  # every monthyearsFmt = DateFormatter('%Y')quotes = quotes_historical_yahoo(    'INTC', date1, date2)if not quotes:    raise SystemExitdates = [q[0] for q in quotes]opens = [q[1] for q in quotes]ax = subplot(111)plot_date(dates, opens, '-')# format the ticksax.xaxis.set_major_locator(years)ax.xaxis.set_major_formatter(yearsFmt)ax.xaxis.set_minor_locator(months)ax.autoscale_view()# format the coords message boxdef price(x): return '$%1.2f'%xax.fmt_xdata = DateFormatter('%Y-%m-%d')ax.fmt_ydata = pricegrid(True)show()

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?