我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用matplotlib.dates.AutoDateLocator()。
def get_locator(self, dmin, dmax): 'Pick the best locator based on a distance.' delta = relativedelta(dmax, dmin) num_days = ((delta.years * 12.0) + delta.months * 31.0) + delta.days num_sec = (delta.hours * 60.0 + delta.minutes) * 60.0 + delta.seconds tot_sec = num_days * 86400. + num_sec if abs(tot_sec) < self.minticks: self._freq = -1 locator = MilliSecondLocator(self.tz) locator.set_axis(self.axis) locator.set_view_interval(*self.axis.get_view_interval()) locator.set_data_interval(*self.axis.get_data_interval()) return locator return dates.AutoDateLocator.get_locator(self, dmin, dmax)
def plot_states_and_var(data, hidden_states, cmap=None, columns=None, by='Activity'): """ Make a plot of the data and the states Parameters ---------- data : pandas DataFrame Data to plot hidden_states: iteretable the hidden states corresponding to the timesteps columns : list, optional Which columns to plot by : str The column to group on """ fig, ax = plt.subplots(figsize=(15, 5)) if columns is None: columns = data.columns df = data[columns].copy() stateseq = np.array(hidden_states) stateseq_norep, durations = rle(stateseq) datamin, datamax = np.array(df).min(), np.array(df).max() y = np.array( [datamin, datamax]) maxstate = stateseq.max() + 1 x = np.hstack(([0], durations.cumsum()[:-1], [len(df.index) - 1])) C = np.array( [[float(state) / maxstate] for state in stateseq_norep]).transpose() ax.set_xlim((min(x), max(x))) if cmap is None: num_states = max(hidden_states) + 1 colormap, cmap = get_color_map(num_states) pc = ax.pcolorfast(x, y, C, vmin=0, vmax=1, alpha=0.3, cmap=cmap) plt.plot(df.as_matrix()) locator = AutoDateLocator() locator.create_dummy_axis() num_index = pd.Index(df.index.map(date2num)) ticks_num = locator.tick_values(min(df.index), max(df.index)) ticks = [num_index.get_loc(t) for t in ticks_num] plt.xticks(ticks, df.index.strftime('%H:%M')[ticks], rotation='vertical') cb = plt.colorbar(pc) cb.set_ticks(np.arange(1./(2*cmap.N), 1, 1./cmap.N)) cb.set_ticklabels(np.arange(0, cmap.N)) # Plot the activities if by is not None: actseq = np.array(data[by]) sca = ax.scatter( np.arange(len(hidden_states)), #data.index, np.ones_like(hidden_states) * datamax, c=actseq, edgecolors='none' ) plt.show() return fig, ax
def line_2subplots(): """ Creates a split plot with one set of x axis labels and two subplots. """ # Save data to redraw plot later save_data('line_2subplots') # set the screen title, size, density fig = plt.figure(title,graph_dimensions,graph_dpi) # do the plot # top half of the graph plot_number 1 nrows = 2 ncols = 1 plot_number = 1 ax = plt.subplot(nrows,ncols,plot_number) plt.title(title) plt.ylabel(ylabel1) plt.grid(which="major") red = 'r' plt.plot(xdatetimes,ylists[0],red) plt.autoscale(tight=True) fig.autofmt_xdate() ax.fmt_xdata = mdates.DateFormatter('%m/%d/%Y %H:%M') datetimefmt = mdates.DateFormatter('') ax.xaxis.set_major_formatter(datetimefmt) # bottom half of the graph plot_number 2 plot_number = 2 ax = plt.subplot(nrows,ncols,plot_number) plt.ylabel(ylabel2) plt.grid(which="major") green='g' plt.plot(xdatetimes,ylists[1],green) plt.autoscale(tight=True) fig.autofmt_xdate() ax.fmt_xdata = mdates.DateFormatter('%m/%d/%Y %H:%M') loc=mdates.AutoDateLocator() datetimefmt = mdates.AutoDateFormatter(loc) ax.xaxis.set_major_formatter(datetimefmt) ax.xaxis.set_major_locator(loc) # subplots_adjust settings vleft = 0.07 # the left side of the subplots of the figure vright = 0.97 # the right side of the subplots of the figure # vbottom = 0.15 # the bottom of the subplots of the figure vbottom = 0.10 # the bottom of the subplots of the figure vtop = 0.95 # the top of the subplots of the figure vwspace = 0.0 # the amount of width reserved for blank space between subplots vhspace = 0.08 # the amount of height reserved for white space between subplots plt.subplots_adjust(left=vleft,right=vright,bottom=vbottom,top=vtop,wspace=vwspace,hspace=vhspace) fileorscreen(title+'.png') return
def line(): """ Creates a single graph with date and time as the x axis and a variable number of plots. """ # Save data to redraw plot later save_data('line') # set the screen title, size, density fig = plt.figure(title,graph_dimensions,graph_dpi) # do the plot plt.title(title) plt.ylabel(ylabel1) plt.grid(which="major") for plot_num in range(len(ylists)): plt.plot(xdatetimes,ylists[plot_num],color=my_colors(plot_num)) # date time formatting ax = plt.axes() fig.autofmt_xdate() ax.fmt_xdata = mdates.DateFormatter('%m/%d/%Y %H:%M') loc=mdates.AutoDateLocator() datetimefmt = mdates.AutoDateFormatter(loc) ax.xaxis.set_major_formatter(datetimefmt) ax.xaxis.set_major_locator(loc) # other formatting plt.legend(ylistlabels,loc='upper left') plt.autoscale(tight=True) # subplots_adjust settings - single plot so zero space between plots vleft = 0.06 # the left side of the subplots of the figure vright = 0.97 # the right side of the subplots of the figure vbottom = 0.12 # the bottom of the subplots of the figure vtop = 0.95 # the top of the subplots of the figure vwspace = 0.0 # the amount of width reserved for blank space between subplots vhspace = 0.0 # the amount of height reserved for white space between subplots plt.subplots_adjust(left=vleft,right=vright,bottom=vbottom,top=vtop,wspace=vwspace,hspace=vhspace) fileorscreen(title+'.png') return