我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用matplotlib.dates.DayLocator()。
def plotCandlestick(symbol, dates, title="Selected data"): quotes = loadStockQuotes(symbol, dates) mondays = WeekdayLocator(MONDAY) # major ticks on the mondays alldays = DayLocator() # minor ticks on the days weekFormatter = DateFormatter('%b %d') # e.g., Jan 12 dayFormatter = DateFormatter('%d') # e.g., 12 fig, ax = plt.subplots() fig.subplots_adjust(bottom=0.2) ax.xaxis.set_major_locator(mondays) ax.xaxis.set_minor_locator(alldays) ax.xaxis.set_major_formatter(weekFormatter) #ax.xaxis.set_minor_formatter(dayFormatter) #plot_day_summary(ax, quotes, ticksize=3) candlestick_ohlc(ax, quotes, width=0.6) ax.xaxis_date() ax.autoscale_view() ax.set_title(title) plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right') plt.show()
def plotCandlestick(symbol, startdate, enddate, title="Selected data"): quotes = loadStockQuotes(symbol, startdate, enddate) print(quotes) mondays = WeekdayLocator(MONDAY) # major ticks on the mondays alldays = DayLocator() # minor ticks on the days weekFormatter = DateFormatter('%b %d') # e.g., Jan 12 # dayFormatter = DateFormatter('%d') # e.g., 12 fig, ax = plt.subplots() fig.subplots_adjust(bottom=0.2) ax.xaxis.set_major_locator(mondays) ax.xaxis.set_minor_locator(alldays) ax.xaxis.set_major_formatter(weekFormatter) #ax.xaxis.set_minor_formatter(dayFormatter) #plot_day_summary(ax, quotes, ticksize=3) candlestick_ohlc(ax, quotes, width=0.6) ax.xaxis_date() ax.autoscale_view() ax.set_title(title) plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right') plt.show()
def plotCandlestick(symbol, start_index, end_index, title="Selected data"): dates = pd.date_range(start_index, end_index) quotes = utl.loadStockQuotes(symbol, dates) mondays = WeekdayLocator(MONDAY) # major ticks on the mondays alldays = DayLocator() # minor ticks on the days weekFormatter = DateFormatter('%b %d') # e.g., Jan 12 dayFormatter = DateFormatter('%d') # e.g., 12 fig, ax = plt.subplots() fig.subplots_adjust(bottom=0.2) ax.xaxis.set_major_locator(mondays) ax.xaxis.set_minor_locator(alldays) ax.xaxis.set_major_formatter(weekFormatter) #ax.xaxis.set_minor_formatter(dayFormatter) #plot_day_summary(ax, quotes, ticksize=3) candlestick_ohlc(ax, quotes, width=0.6) ax.xaxis_date() ax.autoscale_view() plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right') plt.show()
def plot_date_axis(axes): from matplotlib import dates date_fmt = dates.DateFormatter('%d/%b') hour_fmt = dates.DateFormatter('%H:%M') # TODO: set xaxis minor interval dynamically axes.xaxis.set_major_locator(dates.DayLocator(interval=1)) axes.xaxis.set_major_formatter(date_fmt) # less than 5 days and interval of 3 is okay axes.xaxis.set_minor_locator(dates.HourLocator(interval=4)) axes.xaxis.set_minor_formatter(hour_fmt) axes.xaxis.set_tick_params(which='major', pad=15)
def format_ax(self, ax): """ Trying to assign reasonable parameters to the time axis. Parameters ---------- ax: """ # TODO: room for improvement ax.xaxis.set_major_locator(mdates.DayLocator(interval=1)) ax.xaxis.set_major_formatter(self.fmt) locator = mdates.HourLocator(interval=4) locator.MAXTICKS = 5000 ax.xaxis.set_minor_locator(locator) datemin = pd.Timestamp.utcnow() ax.set_xlim(datemin) ax.grid(True)
def setAxisScaleX(self, x_axis_bins): """The setAxisScaleX() method sets the bins for the X axis. Presently, we assume a date-based axis.""" if self.verboseLogging: self.logger.threaddebug(u"Constructing the bins for the X axis.") if x_axis_bins == 'quarter-hourly': plt.gca().xaxis.set_major_locator(mdate.HourLocator(interval=4)) plt.gca().xaxis.set_minor_locator(mdate.HourLocator(byhour=range(0, 24, 96))) if x_axis_bins == 'half-hourly': plt.gca().xaxis.set_major_locator(mdate.HourLocator(interval=4)) plt.gca().xaxis.set_minor_locator(mdate.HourLocator(byhour=range(0, 24, 48))) elif x_axis_bins == 'hourly': plt.gca().xaxis.set_major_locator(mdate.HourLocator(interval=4)) plt.gca().xaxis.set_minor_locator(mdate.HourLocator(byhour=range(0, 24, 24))) elif x_axis_bins == 'hourly_4': plt.gca().xaxis.set_major_locator(mdate.HourLocator(interval=4)) plt.gca().xaxis.set_minor_locator(mdate.HourLocator(byhour=range(0, 24, 8))) elif x_axis_bins == 'hourly_8': plt.gca().xaxis.set_major_locator(mdate.HourLocator(interval=4)) plt.gca().xaxis.set_minor_locator(mdate.HourLocator(byhour=range(0, 24, 4))) elif x_axis_bins == 'hourly_12': plt.gca().xaxis.set_major_locator(mdate.HourLocator(interval=4)) plt.gca().xaxis.set_minor_locator(mdate.HourLocator(byhour=range(0, 24, 2))) elif x_axis_bins == 'daily': plt.gca().xaxis.set_major_locator(mdate.DayLocator(interval=1)) plt.gca().xaxis.set_minor_locator(mdate.HourLocator(byhour=range(0, 24, 6))) elif x_axis_bins == 'weekly': plt.gca().xaxis.set_major_locator(mdate.DayLocator(interval=7)) plt.gca().xaxis.set_minor_locator(mdate.DayLocator(interval=1)) elif x_axis_bins == 'monthly': plt.gca().xaxis.set_major_locator(mdate.MonthLocator(interval=1)) plt.gca().xaxis.set_minor_locator(mdate.DayLocator(interval=1)) elif x_axis_bins == 'yearly': plt.gca().xaxis.set_major_locator(mdate.YearLocator()) plt.gca().xaxis.set_minor_locator(mdate.MonthLocator(interval=12))
def make_dateplot(x, y, outname, ylims=None): fig, ax = plt.subplots() fig.autofmt_xdate() ax.plot_date(x, y, ls='', marker='x') ax.xaxis.set_major_locator(MonthLocator()) ax.xaxis.set_minor_locator(DayLocator()) ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d')) ax.fmt_xdata = DateFormatter('%Y-%m-%d %H:%M:%S') if ylims is not None: ax.set_ylim(ylims) fig.savefig(outname, dpi=200) plt.close(fig)
def _make_chart(df, chartfn, **kwargs): fig = plt.figure() ax1 = plt.subplot2grid((6, 4), (1, 0), rowspan=4, colspan=4) ax1.grid(True) plt.ylabel('Price') plt.setp(plt.gca().get_xticklabels(), visible=False) chartfn(df, ax1) if 'lines' in kwargs: _plot_lines(kwargs['lines']) if 'band' in kwargs: _plot_band(kwargs['band']) if 'events' in kwargs: _plot_events(kwargs['events']) ax2 = plt.subplot2grid((6, 4), (5, 0), sharex=ax1, rowspan=1, colspan=4) volume = df['volume'] pos = df['open'] - df['close'] <= 0 # mask neg = df['open'] - df['close'] > 0 ax2.bar(volume[pos].index, volume[pos], color='red', width=0.4, align='center', alpha=0.5) ax2.bar(volume[neg].index, volume[neg], color='green', width=0.4, align='center', alpha=0.5) # ax2.bar(df.index, df.loc[:, 'volume'],align='center') ax2.xaxis.set_major_locator(mticker.MaxNLocator(12)) ax2.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) if len(df.index) <= 500: ax2.xaxis.set_minor_locator(mdates.DayLocator()) ax2.yaxis.set_ticklabels([]) ax2.grid(True) plt.ylabel('Volume') plt.xlabel('DateTime') plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right') ax3 = plt.subplot2grid((6, 4), (0, 0), sharex=ax1, rowspan=1, colspan=4) if 'tracks' in kwargs: _plot_tracks(kwargs['tracks']) ax3.yaxis.set_ticklabels([]) # ax3.yaxis.tick_right() ax3.grid(True) ax3.xaxis.set_visible(False) ax3.set_ylabel('Observe') plt.subplots_adjust(left=.09, bottom=.18, right=.94, top=0.94, wspace=.20, hspace=0) if 'title' in kwargs: plt.suptitle(kwargs['title']) if 'fname' in kwargs: plt.savefig(kwargs['fname'], bbox_inches='tight') plt.show() # plt.close()