def __init__(self): wx.Frame.__init__(self, None, wx.ID_ANY, title='First Chart', size=(800, 700)) datafile = matplotlib.get_example_data('goog.npy') r = np.load(datafile).view(np.recarray) datesFloat = matplotlib.dates.date2num(r.date) figure = Figure() xMaxDatetime = r.date[len(r.date) - 1] xMinDatetime = r.date[0] xMaxFloat = datesFloat[len(datesFloat) - 1] xMinFloat = datesFloat[0] yMin = min(r.adj_close) // 5 * 5 yMax = (1 + max(r.adj_close) // 5) * 5 master = figure.add_subplot(211) master.plot(datesFloat, r.adj_close) master.xaxis.set_minor_locator(mdates.MonthLocator()) master.xaxis.set_major_locator( mdates.MonthLocator(bymonth=(1, 4, 7, 10))) master.xaxis.set_major_formatter(mdates.DateFormatter('%b-%y')) master.set_xlim(datesFloat[120], datesFloat[120] + 92) master.yaxis.set_minor_locator(mtickers.MultipleLocator(50)) master.yaxis.set_major_locator(mtickers.MultipleLocator(100)) master.set_ylim(yMin, yMax) master.set_position([0.05, 0.20, 0.92, 0.75]) master.xaxis.grid(True, which='minor') master.yaxis.grid(True, which='minor') slave = figure.add_subplot(212, yticks=[]) slave.plot(datesFloat, r.adj_close) slave.xaxis.set_minor_locator(mdates.MonthLocator()) slave.xaxis.set_major_locator(mdates.YearLocator()) slave.xaxis.set_major_formatter(mdates.DateFormatter('%b-%y')) slave.set_xlim(xMinDatetime, xMaxDatetime) slave.set_ylim(yMin, yMax) slave.set_position([0.05, 0.05, 0.92, 0.10]) rectangle = mpatches.Rectangle((datesFloat[120], yMin), 92, yMax - yMin, facecolor='yellow', alpha=0.4) slave.add_patch(rectangle) canvas = FigureCanvas(self, -1, figure) drag = DraggableRectangle(rectangle, master, xMinFloat, xMaxFloat - 92) drag.connect()
def __init__(self): wx.Frame.__init__(self, None, wx.ID_ANY, title='First Chart', size=(800, 700)) datafile = matplotlib.get_example_data('goog.npy') r = np.load(datafile).view(np.recarray) datesFloat = matplotlib.dates.date2num(r.date) figure = Figure() xMaxDatetime = r.date[len(r.date)-1] xMinDatetime = r.date[0] xMaxFloat = datesFloat[len(datesFloat)-1] xMinFloat = datesFloat[0] yMin = min(r.adj_close) // 5 * 5 yMax = (1 + max(r.adj_close) // 5) * 5 master = figure.add_subplot(211) master.plot(datesFloat, r.adj_close) master.xaxis.set_minor_locator(mdates.MonthLocator()) master.xaxis.set_major_locator(mdates.MonthLocator(bymonth=(1,4,7,10))) master.xaxis.set_major_formatter(mdates.DateFormatter('%b-%y')) master.set_xlim(datesFloat[120], datesFloat[120]+92) master.yaxis.set_minor_locator(mtickers.MultipleLocator(50)) master.yaxis.set_major_locator(mtickers.MultipleLocator(100)) master.set_ylim(yMin, yMax) master.set_position([0.05,0.20,0.92,0.75]) master.xaxis.grid(True, which='minor') master.yaxis.grid(True, which='minor') slave = figure.add_subplot(212, yticks=[]) slave.plot(datesFloat, r.adj_close) slave.xaxis.set_minor_locator(mdates.MonthLocator()) slave.xaxis.set_major_locator(mdates.YearLocator()) slave.xaxis.set_major_formatter(mdates.DateFormatter('%b-%y')) slave.set_xlim(xMinDatetime, xMaxDatetime) slave.set_ylim(yMin, yMax) slave.set_position([0.05,0.05,0.92,0.10]) rectangle = mpatches.Rectangle((datesFloat[120], yMin), 92, yMax-yMin, facecolor='yellow', alpha = 0.4) slave.add_patch(rectangle) canvas = FigureCanvas(self, -1, figure) drag = DraggableRectangle(rectangle, master, xMinFloat, xMaxFloat - 92) drag.connect()
import datetime import numpy as np import matplotlib import matplotlib.pyplot as plt import matplotlib.dates as mdates import matplotlib.mlab as mlab years = mdates.YearLocator() # every year months = mdates.MonthLocator() # every month yearsFmt = mdates.DateFormatter('%Y') # load a numpy record array from yahoo csv data with fields date, # open, close, volume, adj_close from the mpl-data/example directory. # The record array stores python datetime.date as an object array in # the date column datafile = matplotlib.get_example_data('goog.npy') r = np.load(datafile).view(np.recarray) #print r fig = plt.figure() ax = fig.add_subplot(111) ax.plot(r.date, r.adj_close) #line = matplotlib.lines.Line2D([2009,2009], [100,400], color='red', linestyle='-.') # format the ticks ax.xaxis.set_major_locator(years) ax.xaxis.set_major_formatter(yearsFmt) ax.xaxis.set_minor_locator(months) datemin = datetime.date(r.date.min().year, 1, 1) datemax = datetime.date(r.date.max().year+1, 1, 1)
import datetime import numpy as np import matplotlib import matplotlib.pyplot as plt import matplotlib.dates as mdates import matplotlib.mlab as mlab years = mdates.YearLocator() # every year months = mdates.MonthLocator() # every month yearsFmt = mdates.DateFormatter('%Y') # load a numpy record array from yahoo csv data with fields date, # open, close, volume, adj_close from the mpl-data/example directory. # The record array stores python datetime.date as an object array in # the date column datafile = matplotlib.get_example_data('goog.npy') r = np.load(datafile).view(np.recarray) #print r fig = plt.figure() ax = fig.add_subplot(111) ax.plot(r.date, r.adj_close) #line = matplotlib.lines.Line2D([2009,2009], [100,400], color='red', linestyle='-.') # format the ticks ax.xaxis.set_major_locator(years) ax.xaxis.set_major_formatter(yearsFmt) ax.xaxis.set_minor_locator(months) datemin = datetime.date(r.date.min().year, 1, 1) datemax = datetime.date(r.date.max().year + 1, 1, 1)
import datetime import numpy as np import matplotlib import matplotlib.dates as dates import matplotlib.ticker as ticker import matplotlib.pyplot as plt fh = matplotlib.get_example_data("aapl.npy") r = np.load(fh) fh.close() r = r[-250:] # get the last 250 days fig = plt.figure() ax = fig.add_subplot(111) ax.plot(r.date, r.adj_close) ax.xaxis.set_major_locator(dates.MonthLocator()) ax.xaxis.set_minor_locator(dates.MonthLocator(bymonthday=15)) ax.xaxis.set_major_formatter(ticker.NullFormatter()) ax.xaxis.set_minor_formatter(dates.DateFormatter("%b")) for tick in ax.xaxis.get_minor_ticks(): tick.tick1line.set_markersize(0) tick.tick2line.set_markersize(0) tick.label1.set_horizontalalignment("center") imid = len(r) / 2 ax.set_xlabel(str(r.date[imid].year)) plt.show()
# # # but this doesn't help center the label between ticks. One solution # is to "face it". Use the minor ticks to place a tick centered # between the major ticks. Here is an example that labels the months, # centered between the ticks import datetime import numpy as np import matplotlib import matplotlib.dates as dates import matplotlib.ticker as ticker import matplotlib.pyplot as plt # load some financial data; apple's stock price fh = matplotlib.get_example_data('aapl.npy') r = np.load(fh); fh.close() r = r[-250:] # get the last 250 days fig = plt.figure() ax = fig.add_subplot(111) ax.plot(r.date, r.adj_close) ax.xaxis.set_major_locator(dates.MonthLocator()) ax.xaxis.set_minor_locator(dates.MonthLocator(bymonthday=15)) ax.xaxis.set_major_formatter(ticker.NullFormatter()) ax.xaxis.set_minor_formatter(dates.DateFormatter('%b')) for tick in ax.xaxis.get_minor_ticks(): tick.tick1line.set_markersize(0)