我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用matplotlib.widgets.SpanSelector()。
def get_QT_interval(self, fig, ax, ax1, ax2, line2, qt_average_waveform, qt_all_spikes): """ This function allows the user to select the QT interval for a region of data. input: fig(plt.fig) ax, ax1, ax2(plt.fig.subplot) line2(plt.fig.subplot.plot([float])) qt_average_waveform([float]) qt_all_spikes([MCS_Spike]) """ ax.set_title(self.title) ax.plot(qt_average_waveform) for spike in qt_all_spikes: ax1.plot(spike.voltage_data) span = SpanSelector(ax, self.onselect, 'horizontal', useblit=True, rectprops=dict(alpha=0.5, facecolor='red')) plt.show(block=False) a = raw_input("Press any key... ") qt_start_point = self.indmin qt_end_point = self.indmax return qt_start_point, qt_end_point
def get_CV_region(self, fig, ax, ax_stim, ax1, ax2, line1, line2, channel_data_1, channel_data_2, stim_data): """ This function allows the user to select a region for the """ ax.set_title(self.title) ax.plot(channel_data_1) ax.plot(channel_data_2) ax_stim.plot(stim_data) span = SpanSelector(ax, self.onselect, 'horizontal', useblit=True, rectprops=dict(alpha=0.5, facecolor='red')) plt.show(block=False) a = raw_input("Press any key... ") start_time = self.channels_to_compare[0].time_data[self.indmin] end_time = self.channels_to_compare[0].time_data[self.indmax] return start_time, end_time
def plot(self): # Prepare the data data = self.locs[self.field] data = data[np.isfinite(data)] bins = lib.calculate_optimal_bins(data, 1000) # Prepare the figure self.figure.clear() self.figure.suptitle(self.field) axes = self.figure.add_subplot(111) axes.hist(data, bins, rwidth=1, linewidth=0) data_range = data.ptp() axes.set_xlim([bins[0] - 0.05*data_range, data.max() + 0.05*data_range]) self.span = SpanSelector(axes, self.on_span_select, 'horizontal', useblit=True, rectprops=dict(facecolor='green', alpha=0.2)) self.canvas.draw()
def plot_all_chan_spectrum(spectrum, bins, *, ax=None, **kwargs): def integrate_to_angles(spectrum, bins, lo, hi): lo_ind, hi_ind = bins.searchsorted([lo, hi]) return spectrum[lo_ind:hi_ind].sum(axis=0) if ax is None: fig, ax = plt.subplots(figsize=(13.5, 9.5)) else: fig = ax.figure div = make_axes_locatable(ax) ax_r = div.append_axes('right', 2, pad=0.1, sharey=ax) ax_t = div.append_axes('top', 2, pad=0.1, sharex=ax) ax_r.yaxis.tick_right() ax_r.yaxis.set_label_position("right") ax_t.xaxis.tick_top() ax_t.xaxis.set_label_position("top") im = ax.imshow(spectrum, origin='lower', aspect='auto', extent=(-.5, 383.5, bins[0], bins[-1]), norm=LogNorm()) e_line, = ax_r.plot(spectrum.sum(axis=1), bins[:-1] + np.diff(bins)) p_line, = ax_t.plot(spectrum.sum(axis=0)) label = ax_t.annotate('[0, 70] kEv', (0, 1), (10, -10), xycoords='axes fraction', textcoords='offset pixels', va='top', ha='left') def update(lo, hi): p_data = integrate_to_angles(spectrum, bins, lo, hi) p_line.set_ydata(p_data) ax_t.relim() ax_t.autoscale(axis='y') label.set_text(f'[{lo:.1f}, {hi:.1f}] keV') fig.canvas.draw_idle() span = SpanSelector(ax_r, update, 'vertical', useblit=True, rectprops={'alpha': .5, 'facecolor': 'red'}, span_stays=True) ax.set_xlabel('channel [#]') ax.set_ylabel('E [keV]') ax_t.set_xlabel('channel [#]') ax_t.set_ylabel('total counts') ax_r.set_ylabel('E [keV]') ax_r.set_xlabel('total counts') ax.set_xlim(-.5, 383.5) ax.set_ylim(bins[0], bins[-1]) ax_r.set_xlim(xmin=0) return spectrum, bins, {'center': {'ax': ax, 'im': im}, 'top': {'ax': ax_t, 'p_line': p_line}, 'right': {'ax': ax_r, 'e_line': e_line, 'span': span}}