0

I am plotting a gpd map with an overlay of scatterplot and circle patches. However, when I run the code, the output only displays the scatterplot and circle patches without the border maps. There is nothing wrong with the map as I tried running it separately and it is showing the right output. Please see the image description below for how the plot looks like.

Here is the code:

def plot_dbscan(points, dbscan, title, pt_sizer=1, plot_circles=False):
    # Index noise and clusters out of the dbscan points
    noise = points[dbscan.labels_ == -1]
    clusters = points[dbscan.labels_ != -1]
    
    # Plot country border 
    fig, ax = plt.subplots(1, figsize=(12,8))
    map_1_prj.plot(ax=ax, fc='None', ec='k', linewidth=1.5)
    
    
    # Allow relative point size adjustment with pt_sizer argument
    sns.scatterplot(x=noise[:,0], y=noise[:,1], ax=ax, alpha=1, s=2*pt_sizer, color='gray')
    sns.scatterplot(x=clusters[:,0], y=clusters[:,1], ax=ax, s=4*pt_sizer, color='red')
    
    # Option to plot a minimum bounding circle around each cluster
    if plot_circles:
        for label in np.unique(dbscan.labels_):
            if label != -1:
                cluster_points = points[dbscan.labels_ == label]
                # Get minimum bounding circle using pointpats.centrography.minimum_bounding_circle()
                (center_x, center_y), radius = minimum_bounding_circle(cluster_points)
                # Create matplotlib patch
                circle_patch = mpatches.Circle((center_x, center_y), radius=radius, fc='None', ec='yellow', linewidth=2)
                ax.add_patch(circle_patch)

    ax.axis('equal')
    
    # Limit bounds of plot to earthquake data
    ax.set_xlim(gdf_prj.total_bounds[0], gdf_prj.total_bounds[2])
    ax.set_ylim(gdf_prj.total_bounds[1], gdf_prj.total_bounds[3])
    
    # Manually prepare legend items
    Ph_border = mlines.Line2D([], [], color='k', linewidth=1.5, label='Philippine Border')
    noise_l = mlines.Line2D([], [], marker='.', linewidth=0, markersize=4, 
                            color='gray', label='Noise')
    
    if plot_circles:
        # Draw yellow circle around red point for legend
        mec = 'yellow'
    else:
        mec = 'None'
    
    clusters_l = mlines.Line2D([], [], marker='.', linewidth=0,
                               markersize=12, color='red', markeredgecolor=mec,
                               label='DBSCAN Clusters')
    # Define legend
    plt.legend(handles=[Ph_border, noise_l, clusters_l])
    
    plt.title(title)
    
    
    plt.show()

enter image description here

Anton Menshov
  • 2,266
  • 14
  • 34
  • 55
  • have you tried plotting the country border last, so you are sure it's not being covered up by your other features? also, are you positive the crs is the same between your data and the shapefile? what does `map_1_prj.total_bounds` return? maybe try not setting the axis bounds, so that you can see if there's an issue with the position of the geometry. – Michael Delgado Dec 10 '22 at 23:44
  • Removing the axis bounds would create 2 separate maps inside the chart area. I have tried rearranging the country border after the scatterplot already but it still doesn't show. If I change it to map_1_prj.total_bounds, it will show the country border but will hide the scatterplot this time. – Gino Penaflor Dec 11 '22 at 06:51
  • No sorry I’m asking you what are the values returned by e.g. `print(map_1_prj.total_bounds)` – Michael Delgado Dec 11 '22 at 06:56
  • And if dropping ax.set_xlim or ylim results in two distinct areas within your plot that means the shapes are not covering the same area. We can’t help with that without more info about your shapefile and data – Michael Delgado Dec 11 '22 at 16:40

0 Answers0