Пример #1
0
 def plot_model_density(self,
                        overlay,
                        sample,
                        xlim=[],
                        ylim=[],
                        line_color="g",
                        figure_size=(7, 8),
                        lognorm=True,
                        filetype="png",
                        resolution=300,
                        filename=None):
     """
     Plots the resulting regression model of the data
     """
     # Plot data
     plot_agency_magnitude_density(self.data, True, sample, xlim, ylim,
                                   figure_size, lognorm)
     # Plot Model
     model_x, model_y, self.standard_deviation = self.retrieve_model()
     title_string = self.model_type.get_string(self.keys[2], self.keys[0])
     plt.plot(model_x,
              model_y,
              line_color,
              linewidth=2.0,
              label=title_string)
     #plt.title(r"{:s}".format(title_string), fontsize=14)
     plt.legend(loc=2, frameon=False)
     if filename:
         utils._save_image(filename, filetype, resolution)
     if not overlay:
         plt.show()
def plot_agency_magnitude_pair(data, overlay=False, xlim=[], ylim=[],
        marker="o", figure_size=(7, 8), filetype="png", resolution=300,
        filename=None):
    """
    Plots the agency magnitude pair
    :param dict data:
        Query result for a particular joint agency-magnitude pair combination
    :param bool overlay:
        Allows another layer to be rendered on top (True) or closes the figure
        for plotting (False)
    :param list xlim:
        Lower and upper bounds for x-axis
    :param list ylim:
        Lower and upper bounds for y-axis
    """
    if not data:
        print "No pairs found - abandoning plot!"
        return
    fig = plt.figure(figsize=figure_size)
    keys = data.keys()
    plt.errorbar(data[keys[0]], data[keys[2]],
                 xerr=data[keys[1]], yerr=data[keys[3]],
                 marker=marker, mfc="b", mec="k", ls="None",
                 ecolor="r")
    plt.xlabel(utils._to_latex(keys[0]), fontsize=16)
    plt.ylabel(utils._to_latex(keys[2]), fontsize=16)
    plt.grid(True)
    if len(xlim) == 2:
        lowx = xlim[0]
        highx = xlim[1]
    else:
        lowx = np.floor(np.min(data[keys[0]]))
        highx = np.ceil(np.max(data[keys[0]]))

    if len(ylim) == 2:
        lowy = ylim[0]
        highy = ylim[1]
    else:
        lowy = np.floor(np.min(data[keys[2]]))
        highy = np.ceil(np.max(data[keys[2]]))

    if lowy < lowx:
        lowx = lowy
    if highy > highx:
        highx = highy
    plt.ylim(lowx, highx)
    plt.xlim(lowx, highx)
    # Overlay 1:1 line
    plt.plot(np.array([lowx, highx]), np.array([lowx, highx]), ls="--",
        color=[0.5, 0.5, 0.5], zorder=1)
    plt.tight_layout()
        
    if filename:
        utils._save_image(filename, filetype, resolution)
    if not overlay:
        plt.show()
    return data
 def plot_magnitude_conversion_model(self, model, overlay, line_color="g",
         filetype="png", resolution=300, filename=None):
     """
     Plots a specific magnitude conversion model (to overlay on top of
     a current figure)
     """
     model_x = np.arange(0.9 * np.min(self.data[self.keys[0]]),
                         1.1 * np.max(self.data[self.keys[0]]),
                         0.01)
     model_y, _ = model.convert_value(model_x, 0.0)
     plt.plot(model_x, model_y, line_color,
              linewidth=2.0,
              label=model.model_name)
     plt.legend(loc=2, frameon=False)
     if filename:
         utils._save_image(filename, filetype, resolution)
     if not overlay:
         plt.show()
 def plot_model_density(self, overlay, sample, xlim=[], ylim=[],
         line_color="g", figure_size=(7, 8), lognorm=True, filetype="png",
         resolution=300, filename=None):
     """
     Plots the resulting regression model of the data
     """
     # Plot data
     plot_agency_magnitude_density(self.data, True, sample, xlim, ylim,
                                   figure_size, lognorm)
     # Plot Model
     model_x, model_y, self.standard_deviation = self.retrieve_model()
     title_string = self.model_type.get_string(self.keys[2], self.keys[0])
     plt.plot(model_x, model_y, line_color,
              linewidth=2.0,
              label=title_string)
     #plt.title(r"{:s}".format(title_string), fontsize=14)
     plt.legend(loc=2, frameon=False)
     if filename:
         utils._save_image(filename, filetype, resolution)
     if not overlay:
         plt.show()
Пример #5
0
 def plot_magnitude_conversion_model(self,
                                     model,
                                     overlay,
                                     line_color="g",
                                     filetype="png",
                                     resolution=300,
                                     filename=None):
     """
     Plots a specific magnitude conversion model (to overlay on top of
     a current figure)
     """
     model_x = np.arange(0.9 * np.min(self.data[self.keys[0]]),
                         1.1 * np.max(self.data[self.keys[0]]), 0.01)
     model_y, _ = model.convert_value(model_x, 0.0)
     plt.plot(model_x,
              model_y,
              line_color,
              linewidth=2.0,
              label=model.model_name)
     plt.legend(loc=2, frameon=False)
     if filename:
         utils._save_image(filename, filetype, resolution)
     if not overlay:
         plt.show()
def plot_agency_magnitude_density(data, overlay=False, number_samples=0,
        xlim=[], ylim=[], figure_size=(7, 8), lognorm=True,
        filetype="png", resolution=300, filename=None):
    """

    """
    keys = data.keys()
    if not data:
        print "No pairs found - abandoning plot!"
        return
    
    if len(xlim) == 2:
        lowx = xlim[0]
        highx = xlim[1]
    else:
        lowx = np.floor(np.min(data[keys[0]]))
        highx = np.ceil(np.max(data[keys[0]]))

    if len(ylim) == 2:
        lowy = ylim[0]
        highy = ylim[1]
    else:
        lowy = np.floor(np.min(data[keys[2]]))
        highy = np.ceil(np.max(data[keys[2]]))
    
    if lowy < lowx:
        lowx = lowy
    if highy > highx:
        highx = highy

    xbins = np.linspace(lowx - 0.05, highx + 0.05,
                        ((highx + 0.05 - lowx - 0.05) / 0.1) + 2.0)
    ybins = np.linspace(lowx - 0.05, highx + 0.05, 
                        ((highx + 0.05 - lowx - 0.05) / 0.1) + 2.0)
    density = sample_agency_magnitude_pairs(data, xbins, ybins, number_samples)
    fig = plt.figure(figsize=figure_size)

    if lognorm:
        cmap = deepcopy(matplotlib.cm.get_cmap("jet"))
        data_norm = LogNorm(vmin=0.1, vmax=np.max(density))
    else:
        cmap = deepcopy(matplotlib.cm.get_cmap("jet"))
        cmap.set_under("w")
        data_norm = Normalize(vmin=0.1, vmax=np.max(density))
        #density[density < 1E-15] == np.nan  
    plt.pcolormesh(xbins[:-1] + 0.05, ybins[:-1] + 0.05, density.T,
                   norm=data_norm, cmap=cmap)
    cbar = plt.colorbar()
    cbar.set_label("Number Events", fontsize=16)
    plt.xlabel(utils._to_latex(keys[0]), fontsize=16)
    plt.ylabel(utils._to_latex(keys[2]), fontsize=16)
    plt.grid(True)
    plt.ylim(lowx, highx)
    plt.xlim(lowx, highx)
    # Overlay 1:1 line
    plt.plot(np.array([lowx, highx]), np.array([lowx, highx]), ls="--",
        color=[0.5, 0.5, 0.5], zorder=1)
    plt.tight_layout()
        
    if filename:
        utils._save_image(filename, filetype, resolution)
    if not overlay:
        plt.show()
    return data
Пример #7
0
def plot_agency_magnitude_density(data,
                                  overlay=False,
                                  number_samples=0,
                                  xlim=[],
                                  ylim=[],
                                  figure_size=(7, 8),
                                  lognorm=True,
                                  filetype="png",
                                  resolution=300,
                                  filename=None):
    """

    """
    keys = data.keys()
    if not data:
        print "No pairs found - abandoning plot!"
        return

    if len(xlim) == 2:
        lowx = xlim[0]
        highx = xlim[1]
    else:
        lowx = np.floor(np.min(data[keys[0]]))
        highx = np.ceil(np.max(data[keys[0]]))

    if len(ylim) == 2:
        lowy = ylim[0]
        highy = ylim[1]
    else:
        lowy = np.floor(np.min(data[keys[2]]))
        highy = np.ceil(np.max(data[keys[2]]))

    if lowy < lowx:
        lowx = lowy
    if highy > highx:
        highx = highy

    xbins = np.linspace(lowx - 0.05, highx + 0.05,
                        ((highx + 0.05 - lowx - 0.05) / 0.1) + 2.0)
    ybins = np.linspace(lowx - 0.05, highx + 0.05,
                        ((highx + 0.05 - lowx - 0.05) / 0.1) + 2.0)
    density = sample_agency_magnitude_pairs(data, xbins, ybins, number_samples)
    fig = plt.figure(figsize=figure_size)

    if lognorm:
        cmap = deepcopy(matplotlib.cm.get_cmap("jet"))
        data_norm = LogNorm(vmin=0.1, vmax=np.max(density))
    else:
        cmap = deepcopy(matplotlib.cm.get_cmap("jet"))
        cmap.set_under("w")
        data_norm = Normalize(vmin=0.1, vmax=np.max(density))
        #density[density < 1E-15] == np.nan
    plt.pcolormesh(xbins[:-1] + 0.05,
                   ybins[:-1] + 0.05,
                   density.T,
                   norm=data_norm,
                   cmap=cmap)
    cbar = plt.colorbar()
    cbar.set_label("Number Events", fontsize=16)
    plt.xlabel(utils._to_latex(keys[0]), fontsize=16)
    plt.ylabel(utils._to_latex(keys[2]), fontsize=16)
    plt.grid(True)
    plt.ylim(lowx, highx)
    plt.xlim(lowx, highx)
    # Overlay 1:1 line
    plt.plot(np.array([lowx, highx]),
             np.array([lowx, highx]),
             ls="--",
             color=[0.5, 0.5, 0.5],
             zorder=1)
    plt.tight_layout()

    if filename:
        utils._save_image(filename, filetype, resolution)
    if not overlay:
        plt.show()
    return data
Пример #8
0
def plot_agency_magnitude_pair(data,
                               overlay=False,
                               xlim=[],
                               ylim=[],
                               marker="o",
                               figure_size=(7, 8),
                               filetype="png",
                               resolution=300,
                               filename=None):
    """
    Plots the agency magnitude pair
    :param dict data:
        Query result for a particular joint agency-magnitude pair combination
    :param bool overlay:
        Allows another layer to be rendered on top (True) or closes the figure
        for plotting (False)
    :param list xlim:
        Lower and upper bounds for x-axis
    :param list ylim:
        Lower and upper bounds for y-axis
    """
    if not data:
        print "No pairs found - abandoning plot!"
        return
    fig = plt.figure(figsize=figure_size)
    keys = data.keys()
    plt.errorbar(data[keys[0]],
                 data[keys[2]],
                 xerr=data[keys[1]],
                 yerr=data[keys[3]],
                 marker=marker,
                 mfc="b",
                 mec="k",
                 ls="None",
                 ecolor="r")
    plt.xlabel(utils._to_latex(keys[0]), fontsize=16)
    plt.ylabel(utils._to_latex(keys[2]), fontsize=16)
    plt.grid(True)
    if len(xlim) == 2:
        lowx = xlim[0]
        highx = xlim[1]
    else:
        lowx = np.floor(np.min(data[keys[0]]))
        highx = np.ceil(np.max(data[keys[0]]))

    if len(ylim) == 2:
        lowy = ylim[0]
        highy = ylim[1]
    else:
        lowy = np.floor(np.min(data[keys[2]]))
        highy = np.ceil(np.max(data[keys[2]]))

    if lowy < lowx:
        lowx = lowy
    if highy > highx:
        highx = highy
    plt.ylim(lowx, highx)
    plt.xlim(lowx, highx)
    # Overlay 1:1 line
    plt.plot(np.array([lowx, highx]),
             np.array([lowx, highx]),
             ls="--",
             color=[0.5, 0.5, 0.5],
             zorder=1)
    plt.tight_layout()

    if filename:
        utils._save_image(filename, filetype, resolution)
    if not overlay:
        plt.show()
    return data