示例#1
0
文件: plotting.py 项目: jeicher/nmrpy
 def __init__(self, fid):
     if not Plot._is_flat_iter(fid.data): 
         raise ValueError('data must be flat iterable.')
     if fid.data == [] or fid.data == None:
         raise ValueError('data must exist.')
     self.fid = fid
     self.fig = pylab.figure(figsize=[15, 7.5])
     self.phases = numpy.array([0.0, 0.0])
     self.y = 0.0
     self.ax = self.fig.add_subplot(111)
     self.ax.plot(self.fid.data, color='k', linewidth=1.0)
     self.ax.hlines(0, 0, len(self.fid.data)-1)
     self.ax.set_xlim([0, len(self.fid.data)])
     xtcks = numpy.linspace(0,1,10)*len(self.fid.data)
     xtcks[-1] = xtcks[-1]-1
     self.ax.set_xticks(xtcks)
     self.ax.set_xlabel('PPM (%.2f MHz)'%(self.fid._params['reffrq']))
     self.ax.set_xticklabels([numpy.round(self.fid._ppm[int(i)], 1) for i in xtcks])
     ylims = numpy.array([-6, 6])*numpy.array([max(self.ax.get_ylim())]*2)
     self.ax.set_ylim(ylims)
     self.ax.grid()
     self.visible = True
     self.canvas = self.ax.figure.canvas
     self.canvas.mpl_connect('motion_notify_event', self.onmove)
     self.canvas.mpl_connect('button_press_event', self.press)
     self.canvas.mpl_connect('button_release_event', self.release)
     self.pressv = None
     self.buttonDown = False
     self.prev = (0, 0)
     self.ax.text(0.05 *self.ax.get_xlim()[1],0.7 *self.ax.get_ylim()[1],'phasing\nleft - zero-order\nright - first order')
     cursor = Cursor(self.ax, useblit=True, color='k', linewidth=0.5)
     cursor.horizOn = False
     pylab.show()
示例#2
0
def guided_ringdown_fit(data_x, data_y):
    """
    Guided fit of a ringdown. Takes *data_x* and *data_y* as ``pint``
    Quantities with dimensions of time and voltage, respectively. Plots the
    data and asks user to manually crop to select the region to fit.

    It then does a rough linear fit to find initial parameters and performs
    a nonlinear fit.

    Finally, it plots the data with the curve fit overlayed and returns the
    full-width at half-max (FWHM) with units.
    """
    # Have user choose crop points
    plt.plot(data_x, data_y)
    cursor = Cursor(plt.gca(), useblit=True)
    cursor.horizOn = False
    (x1, y1), (x2, y2) = _ginput(2)
    plt.close()

    # Crop the data
    i1 = searchsorted(data_x.magnitude, x1)
    i2 = searchsorted(data_x.magnitude, x2)
    data_x = data_x[i1:i2]
    data_y = data_y[i1:i2]

    # Set t0 = 0 so that the amplitude 'a' doesn't blow up if we
    # have a large time offset
    t = data_x - data_x[0]
    amp = data_y / u.V

    def decay(x, a, b, c):
        return a * exp(b * x) + c

    # Do linear fit to get initial parameter estimate
    a0, b0, c0 = _linear_fit_decay(t.magnitude, amp.to('').magnitude)

    # Do nonlinear fit
    popt, pcov = curve_fit(decay,
                           t.magnitude,
                           amp.magnitude,
                           p0=[a0, b0, c0],
                           maxfev=2000)
    a, b, c = popt
    tau = Q_(-1 / b, t.units)
    FWHM = (1 / (2 * pi * tau)).to('MHz')

    fit = a * exp(-t / tau) + c

    t.ito('ns')
    plt.plot(t, fit, 'b-', lw=2, zorder=3)
    plt.plot(t, amp, 'gx')
    plt.title('Ringdown fit')
    plt.xlabel('Time (ns)')
    plt.ylabel('Transmission (arb. units)')
    plt.legend(['Fitted Curve', 'Data Trace'])
    plt.show()
    return FWHM
示例#3
0
def guided_ringdown_fit(data_x, data_y):
    """
    Guided fit of a ringdown. Takes *data_x* and *data_y* as ``pint``
    Quantities with dimensions of time and voltage, respectively. Plots the
    data and asks user to manually crop to select the region to fit.

    It then does a rough linear fit to find initial parameters and performs
    a nonlinear fit.

    Finally, it plots the data with the curve fit overlayed and returns the
    full-width at half-max (FWHM) with units.
    """
    # Have user choose crop points
    plt.plot(data_x, data_y)
    cursor = Cursor(plt.gca(), useblit=True)
    cursor.horizOn = False
    (x1, y1), (x2, y2) = _ginput(2)
    plt.close()

    # Crop the data
    i1 = searchsorted(data_x.magnitude, x1)
    i2 = searchsorted(data_x.magnitude, x2)
    data_x = data_x[i1:i2]
    data_y = data_y[i1:i2]

    # Set t0 = 0 so that the amplitude 'a' doesn't blow up if we
    # have a large time offset
    t = data_x - data_x[0]
    amp = data_y / u.V

    def decay(x, a, b, c):
        return a*exp(b*x) + c

    # Do linear fit to get initial parameter estimate
    a0, b0, c0 = _linear_fit_decay(t.magnitude, amp.to('').magnitude)

    # Do nonlinear fit
    popt, pcov = curve_fit(decay, t.magnitude, amp.magnitude, p0=[a0, b0, c0], maxfev=2000)
    a, b, c = popt
    tau = Q_(-1/b, t.units)
    FWHM = (1 / (2*pi*tau)).to('MHz')

    fit = a * exp(-t/tau) + c

    t.ito('ns')
    plt.plot(t, fit, 'b-', lw=2, zorder=3)
    plt.plot(t, amp, 'gx')
    plt.title('Ringdown fit')
    plt.xlabel('Time (ns)')
    plt.ylabel('Transmission (arb. units)')
    plt.legend(['Fitted Curve', 'Data Trace'])
    plt.show()
    return FWHM
示例#4
0
文件: windows.py 项目: gabraganca/S4
def choose_windows(spectrum, wstart, wend):
    """
    Choose the windows interactively

    Parameters
    ----------

    spectrum: array;
        Bi-dimensional array containing the spectrum. First column should be
        wavelength and the second, flux.

    wstart: int, float;
        Starting wavelength.

    wend: int, float;
        Ending wavelength.
    """

    #Define function that will obtain the cursor x position
    def onselect(vmin, vmax):
        """ Function that will obtain the cursor x position"""
        windows.append(float(Decimal("%.2f" % vmin)))
        windows.append(float(Decimal("%.2f" % vmax)))
        plot_windows(windows)
        plt.draw()
    #Create a plot so the user can choose the windows
    windows = []                            #Set an empty window
    axis = plt.subplot(111)
    axis.plot(spectrum[:, 0], spectrum[:, 1], 'k-')
    axis.hlines(1, wstart, wend, color = 'b', linestyles = 'dashed')
    axis.set_xlim([wstart, wend])
    axis.set_ylim([min(subselect_spectra                     \
                           (spectrum, wstart, wend)[1]) - 0.2, 1.05])
    axis.set_xlabel(r'Wavelength $(\AA)$')
    axis.set_ylabel('Normalized Flux')
    span = SpanSelector(axis, onselect, 'horizontal', minspan = 0.05)
    # Plot a vertical line at cursor position
    cursor = Cursor(axis, useblit = True, color = 'red', linewidth = 1 )
    cursor.horizOn = False

    plt.show()
    plt.clf()
    
    return windows
示例#5
0
from matplotlib.widgets import Cursor
import pylab

fig = pylab.figure(figsize=(8, 6))
ax = fig.add_axes([0.075, 0.25, 0.9, 0.725], axisbg='#FFFFCC')

x, y = 4 * (pylab.rand(2, 100) - .5)
ax.plot(x, y, 'o')
ax.set_xlim(-2, 2)
ax.set_ylim(-2, 2)

# set useblit = True on gtkagg for enhanced performance
cursor = Cursor(ax, useblit=True, color='red', linewidth=2)

cursor.horizOn = False
pylab.show()
示例#6
0
        s.values.pop(s.gamma)
        s.values.pop(s.v)


def my_selector(event):
    draw_arrow(event.xdata ,event.ydata)
    plt.draw()

def click(label):
    global lab_frame_toggle
    global force_toggle
    if label == "vehicle frame":
        lab_frame_toggle = not lab_frame_toggle
    else:
        force_toggle=  not force_toggle


lab_frame_toggle=False
force_toggle=False
rax = plt.axes([0.03, 0.1, 0.2, 0.15])
check = CheckButtons(rax, ('vehicle frame','show force'), (False,False))
check.on_clicked(click)
# set useblit = True on gtkagg for enhanced performance
cursor = Cursor(ax, useblit=True)
cursor.horizOn=False
cursor.vertOn=False
plt.ion()
plt.connect('button_press_event', my_selector)
#plt.connect('motion_notify_event', my_selector)
plt.show()