def write_pil(a, out):
    comp = 6
    nrm = norm if norm != None else plt.Normalize(filter(a.min()),
                                                  filter(a.max()))
    img = ((2**depth - 1) * jet(nrm(filter(a)))).astype(np.uint8)
    i = Image.fromarray(img, 'RGBA')
    i.save(out, compress_level=comp, bits=depth)
Ejemplo n.º 2
0
def plot_global(xx,
                yy,
                data,
                data_projection_code,
                cmin,
                cmax,
                ax,
                plot_type='pcolormesh',
                show_colorbar=False,
                cmap='jet',
                show_grid_lines=True,
                show_grid_labels=True,
                levels=20):

    if show_grid_lines:
        gl = ax.gridlines(crs=ccrs.PlateCarree(),
                          linewidth=1,
                          color='black',
                          draw_labels=show_grid_labels,
                          alpha=0.5,
                          linestyle='--')
    else:
        gl = []

    if data_projection_code == 4326:  # lat lon does nneed to be projected
        data_crs = ccrs.PlateCarree()
    else:
        data_crs = ccrs.epsg(data_projection_code)

    if plot_type == 'pcolormesh':
        p = ax.pcolormesh(xx,
                          yy,
                          data,
                          transform=data_crs,
                          vmin=cmin,
                          vmax=cmax,
                          cmap=cmap)
    elif plot_type == 'contourf':
        p = ax.contourf(xx,
                        yy,
                        data,
                        levels,
                        transform=data_crs,
                        vmin=cmin,
                        vmax=cmax,
                        cmap=cmap)
    else:
        raise ValueError(
            'plot_type  must be either "pcolormesh" or "contourf"')

    ax.coastlines('110m', linewidth=0.8)
    ax.add_feature(cfeature.LAND)

    cbar = []
    if show_colorbar:
        sm = plt.cm.ScalarMappable(cmap=cmap, norm=plt.Normalize(cmin, cmax))
        sm._A = []
        cbar = plt.colorbar(sm, ax=ax)

    return p, gl, cbar
Ejemplo n.º 3
0
def trajectory_vel_with_occupancy_grid(map, file_ptahs):
    occupancy_grid, start, goal = get_occupancy_grid(map)
    #plt.style.use('ggplot')
    labels = []
    for fp in file_ptahs:
        # data_path = np.genfromtxt(fp, delimiter=',')
        # plt.plot(data_path[:, 0], data_path[:, 1], color = "r" if "rrt" in fp else "b")
        # labels.append("RRT" if "rrt" in fp else "Wavefront")
        
        data_trajectory = np.genfromtxt(fp, delimiter=',')

        fig, axs = plt.subplots()
        velocities = data_trajectory[:, 2]
        # Create a continuous norm to map from data points to colors
        norm = plt.Normalize(velocities.min(), velocities.max())
        points = np.array([data_trajectory[:, 0], data_trajectory[:, 1]]).T.reshape(-1, 1, 2)
        segments = np.concatenate([points[:-1], points[1:]], axis=1)
        lc = LineCollection(segments, cmap="Reds" if "rrt" in fp else "Blues", norm=norm)
        # Set the values used for colormapping
        lc.set_array(velocities)
        lc.set_linewidth(2)
        line = axs.add_collection(lc)
        plt.colorbar(line, ax=axs)
        occupancy_grid.draw()
        plt.show()
    #plt.title('Planned trajectories')
    plt.legend(labels)
    occupancy_grid.draw()
    plt.show()
Ejemplo n.º 4
0
    def __call__(self, i):
        if i % 3 == 0:
            step = "predict"
        if i % 3 == 1:
            step = "correct"
        if i % 3 == 2:
            step = "resample"
            
        particles, weights, lines_coordinates, (x, y), measurements, (x_true, y_true), step = self.moveState(step)
        
        self.particlesScatter.set_offsets(np.c_[[p.x for p in particles], [p.y for p in particles]])
        normcolor = plt.Normalize(np.min(weights), np.max(weights))
        self.particlesScatter.set_array(normcolor(weights))
        self.lines.set_segments(lines_coordinates)
        self.positionEst.set_data(x, y)
        self.positionTrue.set_data(x_true, y_true)
        if step == 'correct':
            sizes = np.array([m for m in measurements.values()])
            self.circles._widths = np.asarray(sizes).ravel()
            self.circles._heights = np.asarray(sizes).ravel()
            self.circles.set_edgecolor('black')
        else:
            self.circles.set_edgecolor('none')
        self.text.set_text("STEP: " + step)

        return (self.particlesScatter, self.lines, self.positionEst, self.positionTrue, self.circles, self.text, ) 
Ejemplo n.º 5
0
def plot_global(xx,yy, data, 
                data_projection_code,
                cmin, cmax, ax, 
                plot_type = 'pcolormesh', 
                show_colorbar=False, 
                cmap=None, 
                show_grid_lines = True,
                show_grid_labels = True,
      		        grid_linewidth = 1, 
                custom_background = False,
                background_name = [],
                background_resolution = [],
                levels=20):

    # assign cmap default
    if cmap is None:
        if cmin*cmax<0:
            cmap = 'RdBu_r'
        else:
            cmap = 'viridis'

    if show_grid_lines :
        gl = ax.gridlines(crs=ccrs.PlateCarree(), 
                          linewidth=1, color='black', 
                          draw_labels = show_grid_labels,
                          alpha=0.5, linestyle='--', zorder=102)
    else:
        gl = []
        
    if data_projection_code == 4326: # lat lon does nneed to be projected
        data_crs =  ccrs.PlateCarree()
    else:
        data_crs =ccrs.epsg(data_projection_code)
     
    if custom_background:
        ax.background_img(name=background_name, resolution=background_resolution)
  
    if plot_type == 'pcolormesh':
        p = ax.pcolormesh(xx, yy, data, transform=data_crs, 
                          vmin=cmin, vmax=cmax, cmap=cmap)
    elif plot_type =='contourf':
        p = ax.contourf(xx, yy, data, levels, transform=data_crs,
                        vmin=cmin, vmax=cmax, cmap=cmap)
    else:
        raise ValueError('plot_type  must be either "pcolormesh" or "contourf"') 
                         
    
    if not custom_background:     
        ax.add_feature(cfeature.LAND, zorder=100)
        
    ax.coastlines('110m', linewidth=grid_linewidth, zorder=101)
        
    cbar = []
    if show_colorbar:
        sm = plt.cm.ScalarMappable(cmap=cmap, norm=plt.Normalize(cmin,cmax))
        sm._A = []
        cbar = plt.colorbar(sm,ax=ax)
    
    return p, gl, cbar 
Ejemplo n.º 6
0
def surf(z, cmap='jet', ax=None, x=None, y=None, c=None, **kwargs):
    '''
  Creates an equivalent "surf" plot, like in matlab

  Parameters
  ----------
  z : :class:`numpy.ndarray`
      The z data
  cmap :
      The colormap used to color based on z height. Default: jet
  ax : :class:`matplotlib.axes.Axes`
      The axes to draw on. Default: gca()
  x : :class:`numpy.ndarray`, optional
      The x coordinate used to draw. Default uses :func:`numpy.meshgrid`
  y : :class:`numpy.ndarray`, optional
      The y coordinate used to draw. Default uses :func:`numpy.meshgrid`
  c : :class:`numpy.ndarray`, optional
      A custom array that is fed into the colormap for coloring. Default uses
      ``z``
  **kwargs : dict
      Additional keyword arguments passed to :meth:`mpl_toolkits.mplot3d.axes3d.Axes3D.plot_surface`


  By default, :meth:`mpl_toolkits.mplot3d.axes3d.Axes3D.plot_surface` does not
  draw the entire mesh, it downsamples it to 50 points instead (for
  efficiency). To disable downsampling, consider setting ``rstride`` and
  ``cstride`` to ``1``.

  Shading is also disabled by default
  '''
    if x is None and y is None:
        x, y = np.meshgrid(range(np.shape(z)[1]), range(np.shape(z)[0]))
    elif x is None:
        x, _ = np.meshgrid(range(np.shape(z)[1]), range(np.shape(z)[0]))
    elif y is None:
        _, y = np.meshgrid(range(np.shape(z)[1]), range(np.shape(z)[0]))

    if c is None:
        c = z

    kwargs['shade'] = kwargs.pop('shade', False)

    if ax is None:
        ax = plt.gca(projection='3d')

    scalarMap = mpl.cm.ScalarMappable(norm=plt.Normalize(vmin=c.min(),
                                                         vmax=c.max()),
                                      cmap=cmap)

    # outputs an array where each C value is replaced with a corresponding color value
    c_colored = scalarMap.to_rgba(c)

    surf = ax.plot_surface(x, y, z, facecolors=c_colored, **kwargs)

    return surf
Ejemplo n.º 7
0
def _add_features_to_axis(ax, p, cmin, cmax,
                          cmap = 'jet',
                          show_coastline=True,
                          show_colorbar=True,
                          show_land=True, 
                          show_grid_lines=True,
                          show_grid_labels=True,
                          show_coastline_over_data = True,
                          show_land_over_data = True,
                          grid_linewidth = 1,
                          grid_linestyle = '--',
                          colorbar_label = None):
                                     
    
    if show_land:
        if show_land_over_data:
        # place land over the data
            zorder = 75
        else:
        # place land under the data
            zorder = 25
        
        ax.add_feature(cfeature.LAND, zorder=zorder)
          
    if show_coastline:
        # place the coastline over land and over the data (default zorder 50)
        if show_coastline_over_data:
            zorder = 85
        else:
            # place coastline over land but under data
            zorder = 35
        
        ax.coastlines(linewidth=0.8, zorder=zorder)
        
    gl = []
    if show_grid_lines :
        # grid lines go over everything (zorder 110)
        gl = ax.gridlines(crs=ccrs.PlateCarree(),
                          linewidth=grid_linewidth, 
                          color='black',
                          draw_labels = show_grid_labels,
                          alpha=0.5, 
                          linestyle=grid_linestyle,
                          zorder=110)
        
    cbar = []
    if show_colorbar:
        sm = plt.cm.ScalarMappable(cmap=cmap, norm=plt.Normalize(cmin,cmax))
        sm._A = []
        cbar = plt.colorbar(sm,ax=ax)
        #cbar = plt.colorbar(p, ax=ax)
        if type(colorbar_label) is str:
            cbar.set_label(colorbar_label)
        
    return gl, cbar
Ejemplo n.º 8
0
def df_to_emotion_histogram(df,
                            palette=plt.cm.Pastel1,
                            emotion_column='emotion',
                            verbose=False):
    """ Take a dataset like ArtEmis and return a histogram over the emotion choices made by the annotators.
    :param df: dataframe carrying dataset
    :param palette: matplotlib color palette, e.g., plt.cm.jet
    :param emotion_column: (str) indicate which column of the dataframe carries the emotion
    :return: a list carrying the resulting histogram figure.
    """
    hist_vals = []
    for emotion in ARTEMIS_EMOTIONS:
        hist_vals.append(sum(df[emotion_column] == emotion) / len(df))

    norm = plt.Normalize(min(hist_vals), max(hist_vals))
    colors = palette(norm(hist_vals))

    s = pd.DataFrame({"emotions": ARTEMIS_EMOTIONS, "vals": hist_vals})
    s.set_index("emotions", drop=True, inplace=True)
    plt.figure()
    s.index.name = None
    ax = s.plot.bar(grid=True,
                    figsize=(12, 4),
                    color=colors,
                    fontsize=16,
                    rot=45,
                    legend=False,
                    ec="k")
    ax.set_ylabel('Percentage of data', fontsize=15)

    for rec, col in zip(ax.patches, colors):
        rec.set_color(col)

    plt.tight_layout()
    res = [plt.gcf()]

    plt.figure()
    s = df[emotion_column].apply(positive_negative_else).value_counts() / len(
        df)

    if verbose:
        print('Pos-Neg-Else, percents:', s.round(3))

    ax = s.plot.bar(grid=True,
                    figsize=(8, 4),
                    fontsize=16,
                    rot=45,
                    legend=False,
                    color='gray')
    ax.set_xticklabels(['positive', 'negative', 'else'])
    plt.tight_layout()
    res.append(plt.gcf())

    return res
def write_png(a, out):
    comp = 6
    writer = png.Writer(size=a.shape[::-1],
                        alpha=True,
                        bitdepth=depth,
                        compression=comp)
    nrm = norm if norm != None else plt.Normalize(filter(a.min()),
                                                  filter(a.max()))
    img = ((2**depth - 1) * jet(nrm(filter(a)))).astype(np.uint8)
    img = img.reshape(img.shape[0], img.shape[1] * img.shape[2])
    out_file = open(out, 'wb')
    writer.write(out_file, img)
    out_file.close()
Ejemplo n.º 10
0
    def __init__(self, fig, ax, configuration, anchors):
        self.fig = fig
        self.ax = ax
        self.conf = configuration
        self.anchors = anchors
        
        ax.set(title = "Particle filter state", xlabel = "$x$, meters", ylabel = "$y$, meters")

        conf = self.conf
        self.robot = Robot(conf['robot_start_x'], conf['robot_start_y'], conf['robot_start_theta'])
        self.particleFilter = ParticleFilter(
            conf['MIN_X'], conf['MAX_X'], 
            conf['MIN_Y'], conf['MAX_Y'], 
            conf['PARTICLES_NUM'])
        
        self.ax.set(aspect = "equal", xlim = (conf['MIN_X'], conf['MAX_X']), ylim=(conf['MIN_Y'], conf['MAX_Y']))

        normcolor = plt.Normalize(0.0, 0.01)
        self.particlesScatter = self.ax.scatter(
            [p.x for p in self.particleFilter.particles], [p.y for p in self.particleFilter.particles], 
            c=np.arange(conf['PARTICLES_NUM']), marker='.', alpha=0.5, cmap=plt.cm.rainbow, norm=normcolor, s=0.5,
            label='particles')
        cb = fig.colorbar(plt.cm.ScalarMappable(cmap=plt.cm.rainbow), ax = ax, shrink=0.8)
        cb.set_label('Relative particle weight')
        
        self.anchorsScatter = self.ax.scatter([a.x for a in anchors], [a.y for a in anchors], 
                                              color='red', marker='x', label='anchors')
        
        lines_coordinates = [[(0.0, 0.0), (0.0, 0.0)]]
        lc = mc.LineCollection(lines_coordinates, linewidths=1)
        self.lines = self.ax.add_collection(lc)
        
        self.positionEst, = self.ax.plot([0.0], [0.0], marker='x', markersize=9, color="green", alpha=0.98,
                                        label='Estimated position')
        self.positionTrue, = self.ax.plot([0.0], [0.0], marker='+', markersize=9, color="black",
                                        label='True position')
        
        self.text = ax.text(conf['MIN_X'] + (conf['MIN_X'] + conf['MAX_X']) * 0.5, 
                            conf['MIN_Y'] + (conf['MIN_Y'] + conf['MAX_Y']) * 0.05, "")


        centers = np.array([[a.x, a.y] for a in anchors])
        cl = mc.EllipseCollection([1.] * len(anchors), [1.] * len(anchors), [1.] * len(anchors), 
            offsets=centers, transOffset=ax.transData, units='xy', facecolors='none', color='black', alpha=0.5)
        self.circles = ax.add_collection(cl)
        self.circles.set_facecolor('none')
        
        self.ax.legend(loc = 'upper left')
Ejemplo n.º 11
0
def plot_3D_zinflc(sim):
    """
    3D plot of the infiltration field map

    """
    isvegc = sim.isvegc
    dx = sim.dx
    ncol = isvegc.shape[0]
    nrow = isvegc.shape[1]
    xc = np.arange(0, ncol*dx, dx)  + dx/2
    yc = np.arange(0, nrow*dx, dx)  + dx/2
    xc, yc = np.meshgrid(xc, yc)


    xc = xc.T
    yc = yc.T

    fig = plt.figure( figsize = (10, 5))
    ax = fig.add_subplot(111, projection='3d')
    # Get rid of colored axes planes`
    ax.xaxis.pane.fill = False
    ax.yaxis.pane.fill = False
    ax.zaxis.pane.fill = False
    ax.xaxis.pane.set_edgecolor('w')
    ax.yaxis.pane.set_edgecolor('w')
    ax.zaxis.pane.set_edgecolor('w')
    ax.set_xticks([], []);
    ax.set_zticks([], []);
    ax.set_yticks([], []);

    ax.grid(False)


    # # Plot the surface with face colors taken from the array we made.
    norm = plt.Normalize()
    colors = cmocean.cm.deep(norm(sim.zinflc ))
    ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))

    im = ax.plot_surface(xc, yc+1 ,yc, facecolors = colors , rstride = 1, cstride = 1,
                           linewidth=0,antialiased=True, shade=False)

    ax.view_init(25, 195)
    return im
Ejemplo n.º 12
0
def plot_grid_search_scores(grid_search_cv,
                            vmin=None,
                            vmax=None,
                            fig_name=None,
                            show_plot=True):
    """Draw heatmap of the validation accuracy as a function of x and y

    Keyword arguments:
        grid_search ... sklearn.grid_search.GridSearchCV object, fitted estimator
    """

    try:
        y_name, x_name = grid_search_cv.grid_scores_[0][0].keys()
    except:
        raise Exception('Number of parameters must be 2')

    x_range = grid_search_cv.param_grid[x_name]
    y_range = grid_search_cv.param_grid[y_name]
    scores = [x[1] for x in grid_search_cv.grid_scores_]
    scores = np.array(scores).reshape(len(y_range), len(x_range))

    fig = plt.figure(figsize=(8, 6))
    plt.rc('text', usetex=True)
    plt.rc('font', family='serif', serif='Times New Roman')
    plt.rc('pgf', texsystem='pdflatex')

    plt.subplots_adjust(left=.2, right=0.95, bottom=0.15, top=0.95)
    plt.imshow(scores,
               interpolation='nearest',
               cmap=plt.cm.hot,
               norm=plt.Normalize(vmin=vmin, vmax=vmax))
    plt.xlabel(x_name, size=20)
    plt.ylabel(y_name, size=20)
    plt.colorbar()
    plt.xticks(np.arange(len(x_range)), x_range.round(5), rotation=45, size=10)
    plt.yticks(np.arange(len(y_range)), y_range.round(5), size=10)
    plt.title('Validation Accuracy', size=22)
    if fig_name is not None:
        plt.savefig(fig_name)
    if not show_plot:
        plt.close(fig)
    plt.show()
Ejemplo n.º 13
0
def colorline(x,
              y,
              z=None,
              cmap='copper',
              norm=plt.Normalize(0.0, 1.0),
              linewidth=2,
              alpha=1.0,
              ax=None):
    """
    Adapted from :
    https://stackoverflow.com/questions/36074455/python-matplotlib-with-a-line-color-gradient-and-colorbar
    
    http://nbviewer.ipython.org/github/dpsanders/matplotlib-examples/blob/master/colorline.ipynb
    http://matplotlib.org/examples/pylab_examples/multicolored_line.html
    Plot a colored line with coordinates x and y
    Optionally specify colors in the array z
    Optionally specify a colormap, a norm function and a line width
    """

    # Default colors equally spaced on [0,1]:
    if z is None:
        z = np.linspace(0.0, 1.0, len(x))

    # Special case if a single number:
    # to check for numerical input -- this is a hack
    if not hasattr(z, "__iter__"):
        z = np.array([z])

    z = np.asarray(z)

    segments = make_segments(x, y)
    lc = mcoll.LineCollection(segments,
                              array=z,
                              cmap=custom_cmap,
                              norm=norm,
                              linewidth=linewidth,
                              alpha=alpha)
    if ax == None:
        ax = plt.gca()
    ax.add_collection(lc)

    return lc
Ejemplo n.º 14
0
    def update_map(self):
        if self.index < self.size:
            self.save_map_v.update_frame_no(self.index)
            cmap = plt.cm.jet
            norm = plt.Normalize(0, np.max(self.confirmed_raw[:, self.index]))
            patches = [
                Polygon(shape,
                        True,
                        color=cmap(
                            norm(
                                int(self.confirmed_data[get_country[
                                    info['WB_A2']]][self.index])))) for info,
                shape in zip(self.map.countries_info, self.map.countries)
                if info['WB_A2'] in self.countries_codes
                if get_country[info['WB_A2']] in self.countries_names
            ]

            if self.index != 0:
                self.pc.remove()
                self.cax.cla()
            else:
                self.timer.stop()

            self.pc = PatchCollection(patches,
                                      match_original=True,
                                      edgecolor='k',
                                      linewidths=1.,
                                      zorder=2)
            self.ax.add_collection(self.pc)
            self.ax.set_title(f'cases in {self.dates[self.index]}')
            self.sm = plt.cm.ScalarMappable(cmap=cmap, norm=norm)
            self.sm.set_array(list(self.confirmed_raw[:, self.index]))
            self.fig.colorbar(self.sm,
                              ax=self.ax,
                              cax=self.cax,
                              orientation='vertical')
            self.fig.canvas.draw_idle()
            self.fig.savefig(f"imgs/img{0}_{self.index}.jpeg")
            self.index += 1
        else:
            self.stop_start(0)
Ejemplo n.º 15
0
def plot_3D_veg(sim): 
    """
    3D plot of the vegetation field
    """
    fig = plt.figure( figsize = (15, 7))
    ax = fig.add_subplot(111, projection='3d')

    # Get rid of colored axes planes
    ax.xaxis.pane.fill = False
    ax.yaxis.pane.fill = False
    ax.zaxis.pane.fill = False
    ax.xaxis.pane.set_edgecolor('w')
    ax.yaxis.pane.set_edgecolor('w')
    ax.zaxis.pane.set_edgecolor('w')
    ax.set_xticks([], []);
    ax.set_zticks([], []);
    ax.set_yticks([], []);
    # plt.axis('off')
    ax.grid(False)

    isvegc = sim.isvegc
    dx = sim.dx
    ncol = isvegc.shape[0]
    nrow = isvegc.shape[1]
    xc = np.arange(0, ncol*dx, dx)  + dx/2
    yc = np.arange(0, nrow*dx, dx)  + dx/2
    xc, yc = np.meshgrid(xc, yc)

    xc = xc.T
    yc = yc.T

    #Plot the surface with face colors taken from the array we made.
    norm = plt.Normalize()
    colors = cm.Greens(norm(sim.isvegc ))
    ax.w_xaxis.set_pane_color((1.0, 1.0, 1.0, 1.0))

    im = ax.scatter(xc[ sim.isvegc == 1], yc[ sim.isvegc == 1],
            yc[ sim.isvegc == 1], c = 'g',  marker='o',  s = 20, alpha =1)

    ax.view_init(20, 195)
Ejemplo n.º 16
0
def plot_velocity(occ_grid, file_path, file_trajectory):
    data_path = np.genfromtxt(file_path, delimiter=',')
    data_trajectory = np.genfromtxt(file_trajectory, delimiter=',')

    fig, axs = plt.subplots()
    plt.plot(data_path[:, 0], data_path[:, 1], 'gray', label='path', zorder=9)
    velocities = data_trajectory[:, 2]
    # Create a continuous norm to map from data points to colors
    norm = plt.Normalize(velocities.min(), velocities.max())
    points = np.array([data_trajectory[:, 0],
                       data_trajectory[:, 1]]).T.reshape(-1, 1, 2)
    segments = np.concatenate([points[:-1], points[1:]], axis=1)
    lc = LineCollection(segments, cmap='Blues', norm=norm, zorder=10)
    # Set the values used for colormapping
    lc.set_array(velocities)
    lc.set_linewidth(2)
    line = axs.add_collection(lc)
    plt.colorbar(line, ax=axs)

    occ_grid.draw()

    plt.show()
Ejemplo n.º 17
0
def plot2D(x, y, z, zmin= None, zmax= None, xlim=None, ylim=None, nx = 200, ny = 200):

  if zmin == None:
    zmin = min(z)

  if zmax == None:
    zmax = max(z)

  for i in range(len(z)):
    z[i] = min(z[i], zmax)
    z[i] = max(z[i], zmin)

  xi = np.linspace(min(x), max(x), nx)
  yi = np.linspace(min(y), max(y), ny)
  zi = plt.mlab.griddata(x, y, z, xi, yi)

  plt.contourf(xi, yi, zi, 32, cmap=plt.cm.jet) #, norm=plt.Normalize(zmin, zmax))
  plt.contourf(xi, yi, zi, 32, norm=plt.Normalize(zmin, zmax))
  if 1 == 1:

    if (xlim == None):
      plt.xlim(-6.0, +6.0)
    else:
      plt.xlim(xlim[0], xlim[1]);

    if (ylim == None):
      plt.ylim(-6.0, +6.0)
    else:
      plt.ylim(ylim[0], ylim[1]);

  else:
    plt.xlim(-0.5, +0.5)
    plt.ylim(-0.5, +0.5)


  plt.colorbar()
  plt.show()
Ejemplo n.º 18
0
def choroplethMap(df,col,title,cmap='OrRd',figsize=(15,8)):
    '''
    This function creates choropleth map of input feature
    Inpute Parameters:
        df:Input DataFrame
        col:Feature Name from dataframe to plot choropleth map
        title:  Title of choropleth map 
        cmap: (Optional) Color map Default:'OrRd'
        figsize: (Optional) figure size Default:(15,8)
    Returns:
        Choropleth Map
    '''
    fig, ax = plt.subplots(figsize=figsize)
    df.plot(column=col,ax=ax, cmap=cmap, edgecolor='black',linewidth=0.5)
    for idx, row in df.iterrows():
        plt.annotate(text=row['STATE_ABBR'], xy=row['coords'], horizontalalignment='center',size=8)
    ax.set_title(title, fontsize=16)
    ax.set_axis_off()
    # Create colorbar    
    vmin = df[col].min()
    vmax = df[col].max()
    sm = plt.cm.ScalarMappable(cmap=cmap, norm=plt.Normalize(vmin=vmin, vmax=vmax))
    sm._A = []
    fig.colorbar(sm)
Ejemplo n.º 19
0
def StellarMassVsOrbitalDistanceVsT90(host, sats, BB, rest, rad, path=''):
    x, y, c, s, S = [[[], []], [[], []], [[], []], [[], []], [[], []]]
    for h in sats:
        if h not in BB:
            rel = sats[h]['center'] - host[sats[h]['Host']]['center']
            wrap(rel, scale=1, boxsize=25e3)
            if sats[h]['Quenched']:
                x[0].append(np.linalg.norm(rel))
                y[0].append(np.log10(sats[h]['Mstar']))
                c[0].append(t_at_sffrac(sats[h]['CumSFH'], .9))
                s[0].append(float(sats[h]['Rvir']))
            else:
                x[1].append(np.linalg.norm(rel))
                y[1].append(np.log10(sats[h]['Mstar']))
                c[1].append(t_at_sffrac(sats[h]['CumSFH'], .9))
                s[1].append(float(sats[h]['Rvir']))
    M = max([max(s[0]), max(s[1])])
    a = 0
    while a < len(s[0]):
        S[0].append((4 + (s[0][a] / M) * 10)**2)
        a += 1
    a = 0
    while a < len(s[1]):
        S[1].append((4 + (s[1][a] / M) * 10)**2)
        a += 1
    f, ax = plt.subplots(1, 1, figsize=(10, 8))
    ax.set_xlim([0, 400])
    ax.set_ylim([6.9, 10.6])
    ax.scatter(0,
               0,
               s=8**2,
               marker='D',
               edgecolor='.5',
               facecolor='None',
               label='Quenched')
    ax.scatter(0,
               0,
               s=10**2,
               edgecolor='.5',
               facecolor='None',
               label='Star Forming')
    ax.tick_params(labelsize=15,
                   direction='in',
                   length=5,
                   width=1,
                   top=True,
                   right=True)
    ax.set_xlabel(r'Distance to Host [kpc]', fontsize=25)
    ax.set_ylabel(r'Log(M$_{*}$) [M$_{\odot}$]', fontsize=25)
    norm = plt.Normalize(3, 13)
    p = ax.scatter(x[0],
                   y[0],
                   c=c[0],
                   cmap='viridis',
                   s=S[0],
                   norm=norm,
                   marker='D',
                   edgecolor='.5',
                   linewidth=.5)
    ax.scatter(x[1],
               y[1],
               c=c[1],
               cmap='viridis',
               s=S[1],
               norm=norm,
               edgecolor='.5',
               linewidth=.5)
    cbar = f.colorbar(p, cax=f.add_axes([.91, .11, .03, .77]))
    cbar.set_label(r'$\tau_{90}$ [Gyr]', fontsize=25)
    cbar.ax.tick_params(labelsize=15, length=3)
    ax.legend(loc='upper right', prop={'size': 15}, ncol=1, frameon=False)
    f.savefig(path + 'StellarMassVsOrbitalDistanceVsT90.' + rest + '.' + rad +
              '.png',
              bbox_inches='tight',
              pad_inches=.1)
    f.savefig(path + 'pdf/StellarMassVsOrbitalDistanceVsT90.' + rest + '.' +
              rad + '.pdf',
              bbox_inches='tight',
              pad_inches=.1)
    plt.close()
Ejemplo n.º 20
0
def plot_pstereo(xx,yy, data,
                 data_projection_code, \
                 lat_lim,
                 cmin, cmax, ax,
                 plot_type = 'pcolormesh',
                 show_colorbar=False,
                 circle_boundary = False,
           grid_linewidth = 1,
           grid_linestyle = '--',
                 cmap='jet',
                 show_grid_lines=False,
                 custom_background = False,
                 background_name = [],
                 background_resolution = [],
                 levels = 20,
                 less_output=True):

    if isinstance(ax.projection, ccrs.NorthPolarStereo):
        ax.set_extent([-180, 180, lat_lim, 90], ccrs.PlateCarree())
        if not less_output:
            print('North Polar Projection')
    elif isinstance(ax.projection, ccrs.SouthPolarStereo):
        ax.set_extent([-180, 180, -90, lat_lim], ccrs.PlateCarree())
        if not less_output:
            print('South Polar Projection')
    else:
        raise ValueError(
            'ax must be either ccrs.NorthPolarStereo or ccrs.SouthPolarStereo')

    if not less_output:
        print('lat_lim: ', lat_lim)

    if circle_boundary:
        theta = np.linspace(0, 2 * np.pi, 100)
        center, radius = [0.5, 0.5], 0.5
        verts = np.vstack([np.sin(theta), np.cos(theta)]).T
        circle = mpath.Path(verts * radius + center)
        ax.set_boundary(circle, transform=ax.transAxes)

    if show_grid_lines:
        gl = ax.gridlines(crs=ccrs.PlateCarree(),
                          linewidth=grid_linewidth,
                          color='black',
                          alpha=0.5,
                          linestyle=grid_linestyle)
    else:
        gl = []

    if data_projection_code == 4326:  # lat lon does nneed to be projected
        data_crs = ccrs.PlateCarree()
    else:
        # reproject the data if necessary
        data_crs = ccrs.epsg(data_projection_code)

    if custom_background:
        ax.background_img(name=background_name,
                          resolution=background_resolution)

    p = []
    if plot_type == 'pcolormesh':
        p = ax.pcolormesh(xx, yy, data, transform=data_crs, \
                          vmin=cmin, vmax=cmax, cmap=cmap)

    elif plot_type == 'contourf':
        p = ax.contourf(xx, yy, data, levels, transform=data_crs,  \
                 vmin=cmin, vmax=cmax, cmap=cmap)

    else:
        raise ValueError(
            'plot_type  must be either "pcolormesh" or "contourf"')

    if not custom_background:
        ax.add_feature(cfeature.LAND)
    ax.coastlines('110m', linewidth=0.8)

    cbar = []
    if show_colorbar:
        sm = plt.cm.ScalarMappable(cmap=cmap, norm=plt.Normalize(cmin, cmax))
        sm._A = []
        cbar = plt.colorbar(sm, ax=ax)

    return p, gl, cbar
Ejemplo n.º 21
0
    def plot(self, block=False, period=False, fap=None, gls=True, data=True, residuals=True):
        """
        Create a plot.

        Parameters
        ----------
        period : boolean
            The periodogram is plotted against log(Period).
        fap : float, list
            Plots the FAP levels.
        gls : boolean
            Plots the GLS periodogram.
        data : boolean
            Plots the data.
        residuals : boolean
            Plots the residuals.

        Returns
        -------
        fig : mpl.figure
            A figure which can be modified.
        """
        try:
            import matplotlib
            import matplotlib.pylab as mpl
        except ImportError:
            raise(ImportError("Could not import matplotlib.pylab."))

        fbest, T0 = self.best["f"], self.best["T0"]

        fig = mpl.figure()
        fig.canvas.set_window_title('GLS periodogram')
        fig.subplots_adjust(hspace=0.05, wspace=0.04, right=0.97, bottom=0.09, top=0.84)
        fs = 10   # fontsize

        nrow = gls + data + residuals
        plt, plt1, plt2, plt3, plt4 = [None] * 5

        if gls:
           # Periodogram
           plt = fig.add_subplot(nrow, 1, 1)
           plt.tick_params(direction='in')
           if period:
              plt.set_xscale("log")
              plt.set_xlabel("Period P")
           else:
              plt.set_xlabel("Frequency $f$")

           plt.set_ylabel(self.label["ylabel"])
           plt.plot(1/self.f if period else self.f, self.power, 'b-', linewidth=.5)
           # mark the highest peak
           plt.plot(1/fbest if period else fbest, self.power[self.p.argmax()], 'r.', label="$1/f = %f$" % (1/fbest))

           x2tics = 1 / np.array([0.5, 1, 2, 3, 5, 10, 20., 100])
           mx2tics = 1 / np.array([0.75, 1.5, 2.5, 4, 15, 40, 60., 80, 100])
           def tick_function(X):
              return ["%g" % (1/z) for z in X]

           plt.tick_params(direction='in', which='both', top=True, right=True)
           plt.minorticks_on()
           plt.autoscale(enable=True, axis='x', tight=True)
           if not period:
              ax2 = plt.twiny()
              ax2.tick_params(direction='in', which='both')
              ax2.format_coord = lambda x,y: "x=%g, x2=%g, y=%g"% (x, 1/x, y)
              ax2.set_xticks(x2tics)
              ax2.set_xticks(mx2tics, minor=True)
              ax2.set_xticklabels(tick_function(x2tics))
              ax2.set_xlim(plt.get_xlim())
              ax2.set_xlabel("Period")
              plt.tick_params(top=False)

           if fap is not None:
              if isinstance(fap, float):
                 fap = [fap]
              n = max(1, len(fap)-1)   # number of dash types
              for i,fapi in enumerate(fap):
                 plt.axhline(self.powerLevel(fapi), linewidth=0.5, color='r', dashes=(8+32*(n-i)/n,8+32*i/n), label="FAP = %s%%"%(fapi*100))
           plt.legend(numpoints=1, fontsize=fs, frameon=False)

        # Data and model
        col = mpl.cm.rainbow(mpl.Normalize()(self.t))
        def plot_ecol(plt, x, y):
           # script for scatter plot with errorbars and time color-coded
           datstyle = dict(color=col, marker='.', edgecolor='k', linewidth=0.5, zorder=2)
           if self.e_y is not None:
              errstyle = dict(yerr=self.e_y, marker='', ls='', elinewidth=0.5)
              if matplotlib.__version__ < '2.' :
                 errstyle['capsize'] = 0.
                 datstyle['s'] = 8**2   # requires square size !?
              else:
                 errstyle['ecolor'] = col
              _, _, (c,) = plt.errorbar(x, y, **errstyle)
              if matplotlib.__version__ < '2.':
                 c.set_color(col)
           plt.scatter(x, y, **datstyle)

        def phase(t):
           #return (t-T0)*fbest % 1
           return (t-T0) % (1/fbest)

        if data:
           # Time series
           tt = arange(self.t.min(), self.t.max(), 0.01/fbest)
           ymod = self.sinmod(tt)
           plt1 = fig.add_subplot(nrow, 2, 2*gls+1)
           plt1.set_ylabel("Data")
           if residuals:
              mpl.setp(plt1.get_xticklabels(), visible=False)
           else:
              plt1.set_xlabel("Time")
           plot_ecol(plt1, self.t, self.y)
           plt1.plot(tt, ymod, 'k-', zorder=0)

           # Phase folded data
           tt = arange(T0, T0+1/fbest, 0.01/fbest)
           yy = self.sinmod(tt)
           plt2 = fig.add_subplot(nrow, 2, 2*gls+2, sharey=plt1)
           mpl.setp(plt2.get_yticklabels(), visible=False)
           if residuals:
              mpl.setp(plt2.get_xticklabels(), visible=False)
           else:
              plt2.set_xlabel("Phase")
           plot_ecol(plt2, phase(self.t), self.y)
           xx = phase(tt)
           ii = np.argsort(xx)
           plt2.plot(xx[ii], yy[ii], 'k-')
           plt2.format_coord = lambda x,y: "x=%g, x2=%g, y=%g"% (x, x*fbest, y)

        if residuals:
           # Time serie of residuals
           yfit = self.sinmod()
           yres = self.y - yfit
           plt3 = fig.add_subplot(nrow, 2, 2*(gls+data)+1, sharex=plt1)
           plt3.set_xlabel("Time")
           plt3.set_ylabel("Residuals")
           plot_ecol(plt3, self.t, yres)
           plt3.plot([self.t.min(), self.t.max()], [0,0], 'k-')

           # Phase folded residuals
           plt4 = fig.add_subplot(nrow, 2, 2*(gls+data)+2, sharex=plt2, sharey=plt3)
           plt4.set_xlabel("Phase")
           mpl.setp(plt4.get_yticklabels(), visible=False)
           plot_ecol(plt4, phase(self.t), yres)
           plt4.plot([0,1/fbest], [0,0], 'k-')
           plt4.format_coord = lambda x,y: "x=%g, x2=%g, y=%g"% (x, x*fbest, y)

        for x in fig.get_axes()[2:]:
           x.tick_params(direction='in', which='both', top=True, right=True)
           x.minorticks_on()
           x.autoscale(enable=True, tight=True)

        if hasattr(mpl.get_current_fig_manager(), 'toolbar'):
            # check seems not needed when "TkAgg" is set
            try:
                mpl.get_current_fig_manager().toolbar.pan()
            except:
                pass # e.g. Jupyter
        #t = fig.canvas.toolbar
        #mpl.ToggleTool(mpl.wx_ids['Pan'], False)

        fig.tight_layout()   # to get the left margin
        marleft = fig.subplotpars.left * fig.get_figwidth() * fig.dpi / fs
        def tighter():
           # keep margin tight when resizing
           xdpi = fs / (fig.get_figwidth() * fig.dpi)
           ydpi = fs / (fig.get_figheight() * fig.dpi)
           fig.subplots_adjust(bottom=4.*ydpi, top=1-ydpi-4*gls*ydpi, right=1-1*xdpi, wspace=4*xdpi, hspace=4*ydpi, left=marleft*xdpi)
           if gls and (residuals or data):
              # gls plot needs additional space for x2axis
              fig.subplots_adjust(top=1-8*ydpi)
              if matplotlib.__version__ < '2.':
                 ax2.set_position(plt.get_position().translated(0,4*ydpi))
              plt.set_position(plt.get_position().translated(0,4*ydpi))

        #fig.canvas.mpl_connect("resize_event", lambda _: (fig.tight_layout()))
        fig.canvas.mpl_connect("resize_event", lambda _: (tighter()))
        fig.show()
        if block:
           print("Close the plot to continue.")
           # needed when called from shell
           mpl.show()
        else:
           # avoids blocking when: import test_gls
           mpl.ion()
        # mpl.show(block=block) # unexpected keyword argument 'block' in older matplotlib
        return fig
Ejemplo n.º 22
0
def plot_proj_to_latlon_grid(lons,
                             lats,
                             data,
                             projection_type='robin',
                             plot_type='pcolormesh',
                             user_lon_0=0,
                             user_lat_0=None,
                             lat_lim=50,
                             parallels=None,
                             levels=20,
                             cmap=None,
                             dx=.25,
                             dy=.25,
                             show_colorbar=False,
                             show_grid_lines=True,
                             show_grid_labels=False,
                             show_coastline=True,
                             show_land=True,
                             grid_linewidth=1,
                             grid_linestyle='--',
                             subplot_grid=None,
                             less_output=True,
                             custom_background=False,
                             background_name=[],
                             background_resolution=[],
                             radius_of_influence=100000,
                             **kwargs):
    """Generate a plot of llc data, resampled to lat/lon grid, on specified
    projection.

    Parameters
    ----------
    lons, lats, data : xarray DataArray    :
        give the longitude, latitude values of the grid, and the 2D field to
        be plotted

    projection_type : string, optional
        denote the type of projection, see Cartopy docs.
        options include
            'robin' - Robinson
            'PlateCarree' - flat 2D projection
            'LambertConformal'
            'Mercator'
            'EqualEarth'
            'Mollweide'
            'AlbersEqualArea'
            'cyl' - Lambert Cylindrical
            'ortho' - Orthographic
            'stereo' - polar stereographic projection, see lat_lim for choosing
            'InterruptedGoodeHomolosine'
                North or South

    user_lon_0 : float, optional, default 0 degrees
        denote central longitude

    user_lat_0 : float, optional,
        denote central latitude (for relevant projections only, see Cartopy)

    lat_lim : int, optional, default 50 degrees
        for stereographic projection, denote the Southern (Northern) bounds for
        North (South) polar projection or cutoff for LambertConformal projection

    parallels : float, optional,
        standard_parallels, one or two latitudes of correct scale
        (for relevant projections only, see Cartopy docs)

    levels : int, optional
        number of contours to plot

    cmap : string or colormap object, optional
        denotes to colormap. Default is 'viridis' for data without sign change,
        and 'RdBu_r' for "diverging" data (i.e. positive and negative)

    dx, dy : float, optional
        latitude, longitude spacing for grid resampling

    show_colorbar : logical, optional, default False
        show a colorbar or not,

    show_grid_lines : logical, optional, default True
        True only possible for some cartopy projections

    show_grid_labels: logical, optional, default False
        True only possible for some cartopy projections

    grid_linewidth : float, optional, default 1.0
        width of grid lines

    grid_linestyle : string, optional, default = '--'
        pattern of grid lines,

    cmin, cmax : float, optional
        minimum and maximum values for colorbar, default is: min/max of data
        if no sign change, otherwise cmax = max(abs(data)), cmin = -cmax
        i.e. centered about zero

    subplot_grid : dict or list, optional
        specifying placement on subplot as
            dict:
                {'nrows': rows_val, 'ncols': cols_val, 'index': index_val}

            list:
                [nrows_val, ncols_val, index_val]

            equates to

                matplotlib.pyplot.subplot(
                    row=nrows_val, col=ncols_val,index=index_val)

    less_output : string, optional
        debugging flag, don't print if True

    radius_of_influence : float, optional.  Default 100000 m
        the radius of the circle within which the input data is search for
        when mapping to the new grid
    """

    cmap, (cmin, cmax) = assign_colormap(data, cmap)

    # default
    circle_boundary = False

    for key in kwargs:
        if key == "cmin":
            cmin = kwargs[key]
        elif key == "cmax":
            cmax = kwargs[key]
        elif key == "circle_boundary":
            circle_boundary = kwargs[key]
            print('circle boundary', circle_boundary)

        else:
            print("unrecognized argument ", key)

    #%%
    # To avoid plotting problems around the date line, lon=180E, -180W
    # plot the data field in two parts, A and B.
    # part 'A' spans from starting longitude to 180E
    # part 'B' spans the from 180E to 360E + starting longitude.
    # If the starting  longitudes or 0 or 180 it is a special case.
    if user_lon_0 > -180 and user_lon_0 < 180:
        A_left_limit = user_lon_0
        A_right_limit = 180
        B_left_limit = -180
        B_right_limit = user_lon_0
        center_lon = A_left_limit + 180

        if not less_output:
            print('-180 < user_lon_0 < 180')

    elif user_lon_0 == 180 or user_lon_0 == -180:
        A_left_limit = -180
        A_right_limit = 0
        B_left_limit = 0
        B_right_limit = 180
        center_lon = 0

        if not less_output:
            print('user_lon_0 ==-180 or 180')

    else:
        raise ValueError('invalid starting longitude')

    #%%
    # the number of degrees spanned in part A and part B
    num_deg_A = int((A_right_limit - A_left_limit) / dx)
    num_deg_B = int((B_right_limit - B_left_limit) / dx)

    # find the longitudal limits of part A and B
    lon_tmp_d = dict()
    if num_deg_A > 0:
        lon_tmp_d['A'] = [A_left_limit, A_right_limit]

    if num_deg_B > 0:
        lon_tmp_d['B'] = [B_left_limit, B_right_limit]

    if projection_type == 'stereo' and user_lat_0 == None:
        if lat_lim > 0:
            user_lat_0 = 90
        else:
            user_lat_0 = -90

    # Make projection axis
    ax = _create_projection_axis(projection_type, user_lon_0, user_lat_0,
                                 parallels, lat_lim, subplot_grid, less_output)

    #%%
    # loop through different parts of the map to plot (if they exist),
    # do interpolation and plot
    f = plt.gcf()
    if not less_output:
        print('len(lon_tmp_d): ', len(lon_tmp_d))

    for key, lon_tmp in lon_tmp_d.items():

        new_grid_lon, new_grid_lat, data_latlon_projection = \
            resample_to_latlon(lons, lats, data,
                               -90+dy, 90-dy, dy,
                               lon_tmp[0], lon_tmp[1], dx,
                               mapping_method='nearest_neighbor',
                               radius_of_influence = radius_of_influence)

        if isinstance(ax.projection, ccrs.NorthPolarStereo) or \
           isinstance(ax.projection, ccrs.SouthPolarStereo) :
            p, gl, cbar = \
                plot_pstereo(new_grid_lon,
                             new_grid_lat,
                             data_latlon_projection,
                             4326, lat_lim,
                             cmin, cmax, ax,
                             plot_type = plot_type,
                             show_colorbar=False,
                             show_coastline = show_coastline,
                             show_land = show_land,
                             circle_boundary=circle_boundary,
                             cmap=cmap,
                             show_grid_lines=False,
                             show_grid_labels = False,
                             custom_background = custom_background,
                             background_name = background_name,
                             background_resolution = background_resolution,
                             less_output=less_output)

        else:  # not polar stereo
            p, gl, cbar = \
                plot_global(new_grid_lon,
                            new_grid_lat,
                            data_latlon_projection,
                            4326,
                            cmin, cmax, ax,
                            plot_type = plot_type,
                            show_colorbar = False,
                            show_coastline = show_coastline,
                            show_land = show_land,
                            cmap=cmap,
                            show_grid_lines = False,
                            show_grid_labels = False,
                            custom_background = custom_background,
                            background_name = background_name,
                            background_resolution = background_resolution,
                            less_output=less_output)

    if show_land:
        ax.add_feature(cfeature.LAND, zorder=100)

    if show_coastline:
        ax.add_feature(cfeature.COASTLINE, linewidth=0.5, zorder=101)

    if show_grid_lines:
        if not less_output:
            print('-- pre plot_pstereo or plot_pstereo', show_grid_lines,
                  show_grid_labels)

        gl2 = ax.gridlines(crs=ccrs.PlateCarree(),
                           linewidth=grid_linewidth,
                           color='black',
                           alpha=0.5,
                           linestyle=grid_linestyle,
                           draw_labels=show_grid_labels,
                           zorder=110)
        #ax.gridlines(draw_labels=True,zorder=110)

    if show_colorbar:
        sm = plt.cm.ScalarMappable(cmap=cmap, norm=plt.Normalize(cmin, cmax))
        sm._A = []
        cbar = plt.colorbar(sm, ax=ax)
        label = ''
        if type(data) == xr.DataArray:
            if 'long_name' in data.attrs:
                label = data.long_name
            elif data.name is not None:
                label = data.name
            if 'units' in data.attrs:
                label += ' [' + data.units + ']'

        cbar.set_label(label)

    #%%
    return f, ax, p, cbar, new_grid_lon, new_grid_lat, data_latlon_projection, gl
Ejemplo n.º 23
0
def plot_proj_to_latlon_grid(lons,
                             lats,
                             data,
                             projection_type='robin',
                             plot_type='pcolormesh',
                             user_lon_0=0,
                             lat_lim=50,
                             levels=20,
                             cmap='jet',
                             dx=.25,
                             dy=.25,
                             show_colorbar=False,
                             show_grid_lines=True,
                             show_grid_labels=True,
                             grid_linewidth=1,
                             grid_linestyle='--',
                             subplot_grid=None,
                             less_output=True,
                             custom_background=False,
                             background_name=[],
                             background_resolution=[],
                             **kwargs):
    """Generate a plot of llc data, resampled to lat/lon grid, on specified 
    projection.

    Parameters
    ----------
    lons, lats, data : xarray DataArray    : 
        give the longitude, latitude values of the grid, and the 2D field to 
        be plotted
    projection_type : string, optional
        denote the type of projection, options include
            'robin' - Robinson
            'PlateCaree' - flat 2D projection
            'Mercator'
            'cyl' - Lambert Cylindrical
            'ortho' - Orthographic
            'stereo' - polar stereographic projection, see lat_lim for choosing
            'InterruptedGoodeHomolosine'
                North or South
    user_lon_0 : float, optional, default 0 degrees
        denote central longitude
    lat_lim : int, optional
        for stereographic projection, denote the Southern (Northern) bounds for 
        North (South) polar projection
    levels : int, optional
        number of contours to plot
    cmap : string or colormap object, optional
        denotes to colormap
    dx, dy : float, optional
        latitude, longitude spacing for grid resampling
    show_colorbar : logical, optional, default False
	show a colorbar or not,
    show_grid_lines : logical, optional
        True only possible for Mercator or PlateCarree projections
    grid_linewidth : float, optional, default 1.0
	width of grid lines
    grid_linestyle : string, optional, default = '--'
	pattern of grid lines,
    cmin, cmax : float, optional
        minimum and maximum values for colorbar, default is min/max of data
    subplot_grid : dict or list, optional
        specifying placement on subplot as
            dict:
                {'nrows': rows_val, 'ncols': cols_val, 'index': index_val}

            list:
                [nrows_val, ncols_val, index_val]

            equates to

                matplotlib.pyplot.subplot(
                    row=nrows_val, col=ncols_val,index=index_val)
    less_output : string, optional
        debugging flag, don't print if True
    """

    #%%
    cmin = np.nanmin(data)
    cmax = np.nanmax(data)

    for key in kwargs:
        if key == "cmin":
            cmin = kwargs[key]
        elif key == "cmax":
            cmax = kwargs[key]
        else:
            print("unrecognized argument ", key)

    #%%
    # To avoid plotting problems around the date line, lon=180E, -180W
    # plot the data field in two parts, A and B.
    # part 'A' spans from starting longitude to 180E
    # part 'B' spans the from 180E to 360E + starting longitude.
    # If the starting  longitudes or 0 or 180 it is a special case.
    if user_lon_0 > -180 and user_lon_0 < 180:
        A_left_limit = user_lon_0
        A_right_limit = 180
        B_left_limit = -180
        B_right_limit = user_lon_0
        center_lon = A_left_limit + 180

        if not less_output:
            print('-180 < user_lon_0 < 180')

    elif user_lon_0 == 180 or user_lon_0 == -180:
        A_left_limit = -180
        A_right_limit = 0
        B_left_limit = 0
        B_right_limit = 180
        center_lon = 0

        if not less_output:
            print('user_lon_0 ==-180 or 180')

    else:
        raise ValueError('invalid starting longitude')

    #%%
    # the number of degrees spanned in part A and part B
    num_deg_A = int((A_right_limit - A_left_limit) / dx)
    num_deg_B = int((B_right_limit - B_left_limit) / dx)

    # find the longitudal limits of part A and B
    lon_tmp_d = dict()
    if num_deg_A > 0:
        lon_tmp_d['A'] = [A_left_limit, A_right_limit]

    if num_deg_B > 0:
        lon_tmp_d['B'] = [B_left_limit, B_right_limit]

    # Make projection axis
    (ax, show_grid_labels) = _create_projection_axis(projection_type,
                                                     user_lon_0, lat_lim,
                                                     subplot_grid, less_output)

    #%%
    # loop through different parts of the map to plot (if they exist),
    # do interpolation and plot
    f = plt.gcf()
    if not less_output:
        print('len(lon_tmp_d): ', len(lon_tmp_d))
    for key, lon_tmp in lon_tmp_d.items():

        new_grid_lon, new_grid_lat, data_latlon_projection = \
            resample_to_latlon(lons, lats, data,
                               -90+dy, 90-dy, dy,
                               lon_tmp[0], lon_tmp[1], dx,
                               mapping_method='nearest_neighbor')

        if isinstance(ax.projection, ccrs.NorthPolarStereo) or \
           isinstance(ax.projection, ccrs.SouthPolarStereo) :
            p, gl, cbar = \
                plot_pstereo(new_grid_lon,
                             new_grid_lat,
                             data_latlon_projection,
                             4326, lat_lim,
                             cmin, cmax, ax,
                             plot_type = plot_type,
                             show_colorbar=False,
                             circle_boundary=True,
                             cmap=cmap,
                             show_grid_lines=False,
                             custom_background = custom_background,
                             background_name = background_name,
                             background_resolution = background_resolution,
                             less_output=less_output)

        else:  # not polar stereo
            p, gl, cbar = \
                plot_global(new_grid_lon,
                            new_grid_lat,
                            data_latlon_projection,
                            4326,
                            cmin, cmax, ax,
                            plot_type = plot_type,
                            show_colorbar = False,
                            cmap=cmap,
                   show_grid_lines = False,
                            custom_background = custom_background,
                            background_name = background_name,
                            background_resolution = background_resolution,
                            show_grid_labels = False)

        if show_grid_lines:
            ax.gridlines(crs=ccrs.PlateCarree(),
                         linewidth=grid_linewidth,
                         color='black',
                         alpha=0.5,
                         linestyle=grid_linestyle,
                         draw_labels=show_grid_labels)

        #%%
        # ax.add_feature(cfeature.LAND)
        #ax.add_feature(cfeature.COASTLINE,linewidth=0.5)

    ax = plt.gca()

    if show_colorbar:
        sm = plt.cm.ScalarMappable(cmap=cmap, norm=plt.Normalize(cmin, cmax))
        sm._A = []
        cbar = plt.colorbar(sm, ax=ax)

    #%%
    return f, ax, p, cbar
Ejemplo n.º 24
0
def dailyStockClusters():
    import datetime
    import os
    import numpy as np
    import pandas.io.data as web
    from pandas import DataFrame
    from matplotlib import pylab as pl
    from matplotlib import finance
    from matplotlib.collections import LineCollection

    from sklearn import cluster, covariance, manifold
    ########################################################################
    ###
    ### This example employs several unsupervised learning techniques to
    ### extract the stock market structure from variations in historical quotes.
    ### The quantity that we use is the daily variation in quote price:
    ### quotes that are linked tend to co-fluctuate during a day.
    ###
    ### stocks used are all Nasdaq 100 stocks that have one year of history
    ### from the current date.
    ###
    ### adopted from example at:
    ### http://scikit-learn.org/0.14/auto_examples/applications/plot_stock_market.html
    ###
    ########################################################################
    # Retrieve the data from Internet

    # Choose a time period reasonnably calm (not too long ago so that we get
    # high-tech firms, and before the 2008 crash)
    today = datetime.datetime.now()
    d1 = datetime.datetime(today.year - 1, today.month, today.day)
    d2 = datetime.datetime(today.year, today.month, today.day)

    # input symbols and company names from text file
    companyName_file = os.path.join(os.getcwd(), "symbols", "companyNames.txt")
    with open(companyName_file, "r") as f:
        companyNames = f.read()

    print "\n\n\n"
    companyNames = companyNames.split("\n")
    ii = companyNames.index("")
    del companyNames[ii]
    companySymbolList = []
    companyNameList = []
    symbol_dict = {}
    for iname, name in enumerate(companyNames):
        name = name.replace("amp;", "")
        testsymbol, testcompanyName = name.split(";")
        companySymbolList.append(format(testsymbol, 's'))
        companyNameList.append(format(testcompanyName, 's'))
        if testsymbol != "CASH":
            symbol_dict[testsymbol] = format(testcompanyName, 's')
    print " ... symbol_dict = ", symbol_dict

    symbols = companySymbolList[:]
    names = companyNameList[:]

    all_data = {}
    for ticker in symbols:
        try:
            all_data[ticker] = web.get_data_yahoo(ticker, d1, d2)
            qclose = DataFrame(
                {tic: data['Close']
                 for tic, data in all_data.iteritems()})
            qopen = DataFrame(
                {tic: data['Open']
                 for tic, data in all_data.iteritems()})
        except:
            print "Cant find ", ticker

    symbols_edit = []
    names_edit = []
    for i, ticker in enumerate(symbols):
        if True in np.isnan(np.array(qclose[ticker])).tolist():
            print ticker, " nans found, ticker removed"
            del qclose[ticker]
            del qopen[ticker]
        else:
            symbols_edit.append(ticker)
            names_edit.append(names[i])

    # The daily variations of the quotes are what carry most information
    variation = qclose - qopen
    variation[np.isnan(variation)] = 0.

    ###############################################################################
    # Learn a graphical structure from the correlations
    edge_model = covariance.GraphLassoCV()

    # standardize the time series: using correlations rather than covariance
    # is more efficient for structure recovery
    X = variation.copy()
    #X = variation.copy().T
    X /= X.std(axis=0)
    edge_model.fit(X)

    ###############################################################################
    # Cluster using affinity propagation

    _, labels = cluster.affinity_propagation(edge_model.covariance_)
    n_labels = labels.max()

    for i in range(n_labels + 1):
        print "Cluster " + str(i) + ":"
        for j in range(len(labels)):
            if labels[j] == i:
                print " ... " + names_edit[j]
        #print('Cluster %i: %s' % ((i + 1), ', '.join(names_edit[labels == i])))

    for i in range(n_labels + 1):
        print "Cluster " + str(i) + ":"
        for j in range(len(labels)):
            if labels[j] == i:
                print " ... " + names_edit[j]

    figure7path = 'Clustered_companyNames.png'  # re-set to name without full path
    figure7_htmlText = "\n<br><h3>Daily stock clustering analyis. Based on one year performance correlations.</h3>\n"
    figure7_htmlText = figure7_htmlText + "\nClustering based on daily variation in Nasdaq 100 quotes.\n"
    figure7_htmlText = figure7_htmlText + '''<br><img src="''' + figure7path + '''" alt="PyTAAA by DonaldPG" width="850" height="500"><br>\n'''

    ###############################################################################
    # Find a low-dimension embedding for visualization: find the best position of
    # the nodes (the stocks) on a 2D plane

    # We use a dense eigen_solver to achieve reproducibility (arpack is
    # initiated with random vectors that we don't control). In addition, we
    # use a large number of neighbors to capture the large-scale structure.
    node_position_model = manifold.LocallyLinearEmbedding(n_components=2,
                                                          eigen_solver='dense',
                                                          n_neighbors=6)

    embedding = node_position_model.fit_transform(X.T).T

    ###############################################################################
    # Visualization
    pl.figure(1, facecolor='w', figsize=(10, 8))
    pl.clf()
    ax = pl.axes([0., 0., 1., 1.])
    pl.axis('off')

    # Display a graph of the partial correlations
    partial_correlations = edge_model.precision_.copy()
    d = 1 / np.sqrt(np.diag(partial_correlations))
    partial_correlations *= d
    partial_correlations *= d[:, np.newaxis]
    non_zero = (np.abs(np.triu(partial_correlations, k=1)) > 0.02)

    # Plot the nodes using the coordinates of our embedding
    pl.scatter(embedding[0],
               embedding[1],
               s=100 * d**2,
               c=labels,
               cmap=pl.cm.spectral)

    # Plot the edges
    start_idx, end_idx = np.where(non_zero)
    #a sequence of (*line0*, *line1*, *line2*), where::
    #            linen = (x0, y0), (x1, y1), ... (xm, ym)
    segments = [[embedding[:, start], embedding[:, stop]]
                for start, stop in zip(start_idx, end_idx)]
    values = np.abs(partial_correlations[non_zero])
    lc = LineCollection(segments,
                        zorder=0,
                        cmap=pl.cm.hot_r,
                        norm=pl.Normalize(0, .7 * values.max()))
    lc.set_array(values)
    lc.set_linewidths(15 * values)
    ax.add_collection(lc)

    # Add a label to each node. The challenge here is that we want to
    # position the labels to avoid overlap with other labels
    for index, (name, label,
                (x, y)) in enumerate(zip(names, labels, embedding.T)):

        dx = x - embedding[0]
        dx[index] = 1
        dy = y - embedding[1]
        dy[index] = 1
        this_dx = dx[np.argmin(np.abs(dy))]
        this_dy = dy[np.argmin(np.abs(dx))]
        if this_dx > 0:
            horizontalalignment = 'left'
            x = x + .002
        else:
            horizontalalignment = 'right'
            x = x - .002
        if this_dy > 0:
            verticalalignment = 'bottom'
            y = y + .002
        else:
            verticalalignment = 'top'
            y = y - .002
        pl.text(x,
                y,
                name,
                size=10,
                horizontalalignment=horizontalalignment,
                verticalalignment=verticalalignment,
                bbox=dict(facecolor='w',
                          edgecolor=pl.cm.spectral(label / float(n_labels)),
                          alpha=.6))

    pl.xlim(
        embedding[0].min() - .15 * embedding[0].ptp(),
        embedding[0].max() + .10 * embedding[0].ptp(),
    )
    pl.ylim(embedding[1].min() - .03 * embedding[1].ptp(),
            embedding[1].max() + .03 * embedding[1].ptp())

    pl.savefig(os.path.join(os.getcwd(), "pyTAAA_web",
                            "Clustered_companyNames.png"),
               format='png')

    return figure7_htmlText
Ejemplo n.º 25
0
def intensityCutImage(imageData, cutLevels):
    """Creates a matplotlib.pylab plot of an image array with the specified cuts in intensity
    applied. This routine is used by L{saveBitmap} and L{saveContourOverlayBitmap}, which both
    produce output as .png, .jpg, etc. images.
    
    @type imageData: numpy array
    @param imageData: image data array
    @type cutLevels: list
    @param cutLevels: sets the image scaling - available options:
        - pixel values: cutLevels=[low value, high value].
        - histogram equalisation: cutLevels=["histEq", number of bins ( e.g. 1024)]
        - relative: cutLevels=["relative", cut per cent level (e.g. 99.5)]
        - smart: cutLevels=["smart", cut per cent level (e.g. 99.5)]
    ["smart", 99.5] seems to provide good scaling over a range of different images.
    @rtype: dictionary
    @return: image section (numpy.array), matplotlib image normalisation (matplotlib.colors.Normalize), in the format {'image', 'norm'}.
    
    @note: If cutLevels[0] == "histEq", then only {'image'} is returned.
    
    """

    oImWidth = imageData.shape[1]
    oImHeight = imageData.shape[0]

    # Optional histogram equalisation
    if cutLevels[0] == "histEq":

        imageData = histEq(imageData, cutLevels[1])
        anorm = pylab.Normalize(imageData.min(), imageData.max())

    elif cutLevels[0] == "relative":

        # this turns image data into 1D array then sorts
        sorted = numpy.sort(numpy.ravel(imageData))
        maxValue = sorted.max()
        minValue = sorted.min()

        # want to discard the top and bottom specified
        topCutIndex=len(sorted-1) \
            -int(math.floor(float((100.0-cutLevels[1])/100.0)*len(sorted-1)))
        bottomCutIndex = int(
            math.ceil(float((100.0 - cutLevels[1]) / 100.0) * len(sorted - 1)))
        topCut = sorted[topCutIndex]
        bottomCut = sorted[bottomCutIndex]
        anorm = pylab.Normalize(bottomCut, topCut)

    elif cutLevels[0] == "smart":

        # this turns image data into 1Darray then sorts
        sorted = numpy.sort(numpy.ravel(imageData))
        maxValue = sorted.max()
        minValue = sorted.min()
        numBins = 10000  # 0.01 per cent accuracy
        binWidth = (maxValue - minValue) / float(numBins)
        histogram = ndimage.histogram(sorted, minValue, maxValue, numBins)

        # Find the bin with the most pixels in it, set that as our minimum
        # Then search through the bins until we get to a bin with more/or the same number of
        # pixels in it than the previous one.
        # We take that to be the maximum.
        # This means that we avoid the traps of big, bright, saturated stars that cause
        # problems for relative scaling
        backgroundValue = histogram.max()
        foundBackgroundBin = False
        foundTopBin = False
        lastBin = -10000
        for i in range(len(histogram)):

            if histogram[i] >= lastBin and foundBackgroundBin == True:

                # Added a fudge here to stop us picking for top bin a bin within
                # 10 percent of the background pixel value
                if (minValue + (binWidth * i)) > bottomBinValue * 1.1:
                    topBinValue = minValue + (binWidth * i)
                    foundTopBin = True
                    break

            if histogram[i] == backgroundValue and foundBackgroundBin == False:
                bottomBinValue = minValue + (binWidth * i)
                foundBackgroundBin = True

            lastBin = histogram[i]

        if foundTopBin == False:
            topBinValue = maxValue

        #Now we apply relative scaling to this
        smartClipped = numpy.clip(sorted, bottomBinValue, topBinValue)
        topCutIndex=len(smartClipped-1) \
            -int(math.floor(float((100.0-cutLevels[1])/100.0)*len(smartClipped-1)))
        bottomCutIndex = int(
            math.ceil(
                float((100.0 - cutLevels[1]) / 100.0) * len(smartClipped - 1)))
        topCut = smartClipped[topCutIndex]
        bottomCut = smartClipped[bottomCutIndex]
        anorm = pylab.Normalize(bottomCut, topCut)
    else:

        # Normalise using given cut levels
        anorm = pylab.Normalize(cutLevels[0], cutLevels[1])

    if cutLevels[0] == "histEq":
        return {'image': imageData.copy()}
    else:
        return {'image': imageData.copy(), 'norm': anorm}
Ejemplo n.º 26
0
def drainage_plot(
    mg,
    surface="topographic__elevation",
    receivers=None,
    proportions=None,
    surf_cmap="gray",
    quiver_cmap="viridis",
    title="Drainage Plot",
):

    if isinstance(surface, six.string_types):
        colorbar_label = surface
    else:
        colorbar_label = "topographic_elevation"
    imshow_grid(mg, surface, cmap=surf_cmap, colorbar_label=colorbar_label)

    if receivers is None:
        receivers = mg.at_node["flow__receiver_node"]
        if proportions is None:
            if "flow__receiver_proportions" in mg.at_node:
                proportions = mg.at_node["flow__receiver_proportions"]
    else:
        receivers = np.asarray(receivers)

    if receivers.ndim == 1:
        receivers = np.expand_dims(receivers, axis=1)

    nreceievers = receivers.shape[-1]

    propColor = plt.get_cmap(quiver_cmap)
    for j in range(nreceievers):
        rec = receivers[:, j]
        is_bad = rec == -1

        xdist = -0.8 * (mg.x_of_node - mg.x_of_node[rec])
        ydist = -0.8 * (mg.y_of_node - mg.y_of_node[rec])

        if proportions is None:
            proportions = np.ones_like(receivers, dtype=float)

        is_bad[proportions[:, j] == 0.0] = True

        xdist[is_bad] = np.nan
        ydist[is_bad] = np.nan

        prop = proportions[:, j] * 256.0
        lu = np.floor(prop)
        colors = propColor(lu.astype(int))

        shape = (mg.number_of_nodes, 1)

        plt.quiver(
            mg.x_of_node.reshape(shape),
            mg.y_of_node.reshape(shape),
            xdist.reshape(shape),
            ydist.reshape(shape),
            color=colors,
            angles="xy",
            scale_units="xy",
            scale=1,
            zorder=3,
        )

    # Plot differen types of nodes:
    o, = plt.plot(
        mg.x_of_node[mg.status_at_node == CORE_NODE],
        mg.y_of_node[mg.status_at_node == CORE_NODE],
        "b.",
        label="Core Nodes",
        zorder=4,
    )
    fg, = plt.plot(
        mg.x_of_node[mg.status_at_node == FIXED_VALUE_BOUNDARY],
        mg.y_of_node[mg.status_at_node == FIXED_VALUE_BOUNDARY],
        "c.",
        label="Fixed Gradient Nodes",
        zorder=5,
    )
    fv, = plt.plot(
        mg.x_of_node[mg.status_at_node == FIXED_GRADIENT_BOUNDARY],
        mg.y_of_node[mg.status_at_node == FIXED_GRADIENT_BOUNDARY],
        "g.",
        label="Fixed Value Nodes",
        zorder=6,
    )
    c, = plt.plot(
        mg.x_of_node[mg.status_at_node == CLOSED_BOUNDARY],
        mg.y_of_node[mg.status_at_node == CLOSED_BOUNDARY],
        "r.",
        label="Closed Nodes",
        zorder=7,
    )

    node_id = np.arange(mg.number_of_nodes)
    flow_to_self = receivers[:, 0] == node_id

    fts, = plt.plot(
        mg.x_of_node[flow_to_self],
        mg.y_of_node[flow_to_self],
        "kx",
        markersize=6,
        label="Flows To Self",
        zorder=8,
    )

    ax = plt.gca()

    ax.legend(
        labels=[
            "Core Nodes",
            "Fixed Gradient Nodes",
            "Fixed Value Nodes",
            "Closed Nodes",
            "Flows To Self",
        ],
        handles=[o, fg, fv, c, fts],
        numpoints=1,
        loc="center left",
        bbox_to_anchor=(1.7, 0.5),
    )
    sm = plt.cm.ScalarMappable(cmap=propColor,
                               norm=plt.Normalize(vmin=0, vmax=1))
    sm._A = []
    cx = plt.colorbar(sm)
    cx.set_label("Proportion of Flow")
    plt.title(title)
Ejemplo n.º 27
0
def T90VsT50(host, sats, rest, rad, path=''):

    t90mw, t90m31 = [[], []]
    t50mw, t50m31 = [[], []]
    rmw, rm31 = [[np.NaN], [np.NaN]]
    mvmw, mvm31 = [[np.NaN], [np.NaN]]
    for s in sats:
        if sats[s]['Vmag'] > -13.5:
            if host[sats[s]['Host']]['Mstar'] > 10**10.55:
                t90m31.append(13.4 - t_at_sffrac(sats[s]['CumSFH'], .9))
                t50m31.append(13.4 - t_at_sffrac(sats[s]['CumSFH'], .5))
                rm31.append(sats[s]['Rvir'])
                mvm31.append(sats[s]['Vmag'])
            else:
                t90mw.append(13.4 - t_at_sffrac(sats[s]['CumSFH'], .9))
                t50mw.append(13.4 - t_at_sffrac(sats[s]['CumSFH'], .5))
                rmw.append(sats[s]['Rvir'])
                mvmw.append(sats[s]['Vmag'])
    try:
        MAX = np.nanmax([np.nanmax(rm31), np.nanmax(rmw)])
    except:
        MAX = 500
    Rm31 = [0] * len(rm31)
    for i in np.arange(len(rm31)):
        Rm31[i] = (3 + 17 * (rm31[i] / MAX))**2
    Rmw = [0] * len(rmw)
    for i in np.arange(len(rmw)):
        Rmw[i] = (3 + 17 * (rmw[i] / MAX))**2

    f, ax = plt.subplots(2, 1, figsize=(6, 13), sharex=True)
    ax[0].grid(c='.5', alpha=.5, linewidth=.7)
    ax[0].set_axisbelow(True)
    ax[1].grid(c='.5', alpha=.5, linewidth=.7)
    ax[1].set_axisbelow(True)
    ax[1].scatter(4,
                  12,
                  edgecolor='k',
                  facecolor='.5',
                  s=(3 + 17 * (500 / MAX)) * 2)
    ax[1].text(3.5, 12.2, '500pc', fontsize=15)
    ax[0].plot([13.4 - 0, 13.4 - 6.7], [13.4 - 0, 13.4 - 13.4],
               c='k',
               zorder=0)
    ax[1].plot([13.4 - 0, 13.4 - 6.7], [13.4 - 0, 13.4 - 13.4],
               c='k',
               zorder=0)
    try:
        norm = plt.Normalize(np.nanmin([np.nanmin(mvmw),
                                        np.nanmin(mvm31)]),
                             np.nanmax([np.nanmax(mvmw),
                                        np.nanmax(mvm31)]))
    except:
        norm = plt.Normalize(0, 1)
    del mvmw[0], mvm31[0], rmw[0], rm31[0]
    p = ax[0].scatter(t50m31,
                      t90m31,
                      s=Rm31,
                      c=mvm31,
                      cmap='plasma_r',
                      norm=norm)
    cbar = f.colorbar(p, cax=f.add_axes([.93, .11, .075, .77]))
    ax[1].scatter(t50mw, t90mw, s=Rmw, c=mvmw, cmap='plasma_r', norm=norm)
    ax[1].set_xlabel(r'$\tau_{50}$ [Gyr ago]', fontsize=20)
    ax[0].set_xlim([13.4, 0])
    ax[0].set_ylabel(r'$\tau_{90}$ [Gyr ago]', fontsize=20)
    ax[0].yaxis.set_label_coords(-0.08, 0)
    ax[0].set_ylim([13.4, 0])
    ax[1].set_ylim([13.4, 0])
    cbar.set_label(r'M$_{V}$', fontsize=20)
    cbar.ax.tick_params(labelsize=15, length=5)
    ax[0].tick_params(labelsize=15, direction='in', length=5)
    ax[1].tick_params(labelsize=15, direction='in', length=5, top=True)
    f.subplots_adjust(hspace=0)
    f.savefig(path + 'T90vsT50.' + rest + '.' + rad + '.png',
              bbox_inches='tight',
              pad_inches=0.1)
    f.savefig(path + 'pdf/T90VsT50.' + rest + '.' + rad + '.pdf',
              bbox_inches='tight',
              pad_inches=0.1)
    plt.close()
Ejemplo n.º 28
0
def SatelliteCountVsStellarMassVsEnvironment(host, rest, rad, path=''):
    x, y, c = [[], [], []]
    for h in host:
        x.append(np.log10(host[h]['Mstar']))
        y.append(len(host[h]['Satellites']))
        c.append(host[h]['Closest'][0] / 1000.)
    xs, ys = [[], []]
    wkbk = xlrd.open_workbook('AdditionalData/NvMst.xls')
    wk = wkbk.sheet_by_index(0)
    line = 0
    while line < 8:
        xs.append(wk.cell_value(line, 0))
        ys.append(wk.cell_value(line, 2))
        line += 1
    wkbk2 = xlrd.open_workbook('AdditionalData/JL.xls')
    wk2 = wkbk2.sheet_by_index(0)
    xjl, yjl, cjl = [[], [], []]
    line = 1
    while line < 5:
        xjl.append(wk2.cell_value(line, 1))
        yjl.append(wk2.cell_value(line, 3))
        cjl.append(wk2.cell_value(line, 2))
        line += 1

    f, ax = plt.subplots(1, 1, figsize=(8, 6))
    ax.set_xlabel(r'Log(M$_{*,host}$) [M$_{\odot}$]', fontsize=20)
    ax.set_ylabel(r'N$_{sats}$', fontsize=20)
    ax.xaxis.set_major_locator(MultipleLocator(.5))
    ax.tick_params(which='major',
                   labelsize=15,
                   direction='in',
                   length=6,
                   width=1,
                   top=True,
                   right=True)
    ax.xaxis.set_minor_locator(MultipleLocator(.1))
    ax.tick_params(which='minor',
                   labelsize=0,
                   direction='in',
                   length=3,
                   width=1,
                   top=True,
                   right=True)
    ax.scatter(xs[0], ys[0], label='Rom25', c='k')
    ax.scatter(xjl[0],
               yjl[0],
               label='Justice League',
               c='k',
               marker='^',
               s=10**2)
    norm = plt.Normalize(0, max([max(c), max(cjl)]))
    ax.scatter(xs, ys, c='k', marker='*', s=13**2, label='SAGA Paper')
    p = ax.scatter(x,
                   y,
                   c=c,
                   cmap='viridis_r',
                   norm=norm,
                   edgecolor='.3',
                   linewidth=.3)
    ax.scatter(xjl,
               yjl,
               c=cjl,
               cmap='viridis_r',
               marker='^',
               s=11**2,
               norm=norm)
    ax.scatter(wk.cell_value(8, 0),
               wk.cell_value(8, 2),
               marker='*',
               c='orange',
               s=13**2,
               label='MW')
    ax.scatter(wk.cell_value(9, 0),
               wk.cell_value(9, 2),
               marker='*',
               c='purple',
               s=13**2,
               label='M31')
    cbar = f.colorbar(p, cax=f.add_axes([.91, .11, .03, .77]))
    cbar.ax.tick_params(labelsize=15)
    cbar.set_label(r'Distance to closest Large Halo' + '\n' +
                   r'(M$_{vir}$ $>$ 5*10$^{11}$) [Mpc]',
                   fontsize=20)
    ax.legend(loc='upper left',
              bbox_to_anchor=(-.03, 1.01),
              prop={'size': 14},
              frameon=False)
    f.savefig(path + 'SatelliteCountVsStellarMassVsEnvironment.' + rest + '.' +
              rad + '.png',
              bbox_inches='tight',
              pad_inches=0.1)
    f.savefig(path + 'pdf/SatelliteCountVsStellarMassVsEnvironment.' + rest +
              '.' + rad + '.pdf',
              bbox_inches='tight',
              pad_inches=0.1)
    plt.close()
Ejemplo n.º 29
0
def drainage_plot(mg,
                  surface='topographic__elevation',
                  receivers=None,
                  proportions=None,
                  surf_cmap='gray',
                  quiver_cmap='viridis',
                  title='Drainage Plot'):

    if isinstance(surface, six.string_types):
        colorbar_label = surface
    else:
        colorbar_label = 'topographic_elevation'
    imshow_node_grid(mg,
                     surface,
                     cmap=surf_cmap,
                     colorbar_label=colorbar_label)

    if receivers is None:
        try:
            receivers = mg.at_node['flow__receiver_nodes']
            if proportions is None:
                try:
                    proportions = mg.at_node['flow__receiver_proportions']
                except:
                    pass
        except:
            receivers = np.reshape(mg.at_node['flow__receiver_node'],
                                   (mg.number_of_nodes, 1))

    nreceievers = int(receivers.size / receivers.shape[0])

    propColor = plt.get_cmap(quiver_cmap)

    for j in range(nreceievers):
        rec = receivers[:, j]
        is_bad = rec == -1

        xdist = -0.8 * (mg.node_x - mg.node_x[rec])
        ydist = -0.8 * (mg.node_y - mg.node_y[rec])

        if proportions is None:
            proportions = np.ones_like(receivers, dtype=float)

        is_bad[proportions[:, j] == 0.] = True

        xdist[is_bad] = np.nan
        ydist[is_bad] = np.nan

        prop = proportions[:, j] * 256.
        lu = np.floor(prop)
        colors = propColor(lu.astype(int))

        shape = (mg.number_of_nodes, 1)

        plt.quiver(mg.node_x.reshape(shape),
                   mg.node_y.reshape(shape),
                   xdist.reshape(shape),
                   ydist.reshape(shape),
                   color=colors,
                   angles='xy',
                   scale_units='xy',
                   scale=1,
                   zorder=3)

    # Plot differen types of nodes:
    o, = plt.plot(mg.node_x[mg.status_at_node == CORE_NODE],
                  mg.node_y[mg.status_at_node == CORE_NODE],
                  'b.',
                  label='Core Nodes',
                  zorder=4)
    fg, = plt.plot(mg.node_x[mg.status_at_node == FIXED_VALUE_BOUNDARY],
                   mg.node_y[mg.status_at_node == FIXED_VALUE_BOUNDARY],
                   'c.',
                   label='Fixed Gradient Nodes',
                   zorder=5)
    fv, = plt.plot(mg.node_x[mg.status_at_node == FIXED_GRADIENT_BOUNDARY],
                   mg.node_y[mg.status_at_node == FIXED_GRADIENT_BOUNDARY],
                   'g.',
                   label='Fixed Value Nodes',
                   zorder=6)
    c, = plt.plot(mg.node_x[mg.status_at_node == CLOSED_BOUNDARY],
                  mg.node_y[mg.status_at_node == CLOSED_BOUNDARY],
                  'r.',
                  label='Closed Nodes',
                  zorder=7)

    node_id = np.arange(mg.number_of_nodes)
    flow_to_self = receivers[:, 0] == node_id

    fts, = plt.plot(mg.node_x[flow_to_self],
                    mg.node_y[flow_to_self],
                    'kx',
                    markersize=6,
                    label='Flows To Self',
                    zorder=8)

    ax = plt.gca()

    ax.legend(labels=[
        'Core Nodes', 'Fixed Gradient Nodes', 'Fixed Value Nodes',
        'Closed Nodes', 'Flows To Self'
    ],
              handles=[o, fg, fv, c, fts],
              numpoints=1,
              loc='center left',
              bbox_to_anchor=(1.7, 0.5))
    sm = plt.cm.ScalarMappable(cmap=propColor,
                               norm=plt.Normalize(vmin=0, vmax=1))
    sm._A = []
    cx = plt.colorbar(sm)
    cx.set_label('Proportion of Flow')
    plt.title(title)
    plt.show()
Ejemplo n.º 30
0
                   bbox=dict(boxstyle='square', facecolor='white'),
                   horizontalalignment='right')

clear_ax.axhline(180, color='k', lw=1.5, ls='--', zorder=0, alpha=0.6)
cloudy_ax.axhline(180, color='k', lw=1.5, ls='--', zorder=0, alpha=0.6)
cloudy_ax.annotate('eclipse',
                   xy=(0, 180),
                   xycoords='data',
                   xytext=(7.5, 175),
                   bbox=dict(boxstyle='square', facecolor='white'),
                   horizontalalignment='right')

# labels

axes[0].set_xlabel('Doppler shift [km s$^{-1}$]')
axes[1].set_xlabel('Doppler shift [km s$^{-1}$]')
axes[0].set_ylabel('Cross correlation (+ offset)')

cloudy_ax.set_ylabel('Phase angle [deg]')

sm = plt.cm.ScalarMappable(cmap=my_colors, norm=plt.Normalize(vmin=0, vmax=1))
sm._A = []
cbar = fig.colorbar(sm,
                    ax=axes.ravel().tolist(),
                    location='top',
                    aspect=80,
                    pad=0.02)
cbar.set_label('Orbital phase')

fig.savefig(savefile, bbox_inches='tight', dpi=300)