pythongeopandas

Geopanadas subplots:


I am using geopandas and want three subplots. But I could not get pass the subplot axis with geopandas. I am using the geopandas 1.0.1 version.

Here is my plotting code:

df=pd.read_csv('/home/ruby/Desktop/degree.csv')
gdf= gpd.read_file("https://naciscdn.org/naturalearth/110m/cultural/ne_110m_admin_0_countries.zip")
fig, axes = plt.subplots(nrows=3, ncols=1, figsize=(20,10))
geometry=gpd.points_from_xy(df.lon, df.lat)
gdf = GeoDataFrame(df, crs="EPSG:4326", geometry=geometry)
gdf.plot(ax=axes[0],column='degree',cmap='Blues',alpha=0.6,legend=True,markersize=10,legend_kwds={'label':"Avg degree",'orientation': "horizontal"})
gdf.plot(ax=axes[1],column='degree',cmap='Blues',alpha=0.6,legend=True,markersize=10,legend_kwds={'label':"Avg degree",'orientation': "horizontal"})
gdf.plot(ax=axes[2],column='degree',cmap='Blues',alpha=0.6,legend=True,markersize=10,legend_kwds={'label':"Avg degree",'orientation': "horizontal"})
plt.xlim([50.0, 130.0])
plt.ylim([0.0,40.0])
plt.show()

I got the output as enter image description here

The csv is here :https://drive.google.com/file/d/1p1wb7iHkd2tJDGRSGZWTRJRi5StN2APK/view?usp=sharing[2]


Solution

  • If you intend to plot many dataframes on top of eachother you should use zorder to specify the order:

    import matplotlib.pyplot as plt
    import geopandas as gpd
    
    #Read a polygon geojson of the world
    df_world = gpd.read_file(r"C:\Users\bera\Desktop\gistest\world.geojson")
    
    #Create a point geodataframe from the csv file of coordinates
    csv_file = r"C:\Users\bera\Desktop\gistest\degree.csv"
    df_points = gpd.pd.read_csv(csv_file)
    df_points["geometry"] = gpd.points_from_xy(x=df_points.lon, y=df_points.lat)
    df_points = gpd.GeoDataFrame(data=df_points, geometry=df_points.geometry, crs=4326)
    
    fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(10, 7))
    
    #Plot the world on the first axis
    df_world.plot(ax=axes[0][0], color="green", edgecolor="gray", linewidth=0.1)
    axes[0][0].set_title("World on axes[0][0]")
    
    #Plot the points on the second axis
    df_points.plot(ax=axes[0][1], cmap="cool", column="degree", markersize=1)
    axes[0][1].set_title("Points on axes[0][1]")
    
    #Plot the points on top of the world map using zorder
    df_world.plot(ax=axes[1][0], color="gray", edgecolor="white", linewidth=0.1, zorder=1)
    df_points.plot(ax=axes[1][0], cmap="cool", column="degree", markersize=0.2, zorder=2)
    axes[1][0].set_title("Both on axes[1][0]")
    
    #Plot the points on top of the world map using zorder, and with limits on axes
    xmin, ymin, xmax, ymax = df_points.total_bounds
    axes[1][1].set_xlim(xmin, xmax)
    axes[1][1].set_ylim(ymin, ymax)
    df_world.plot(ax=axes[1][1], color="khaki", edgecolor="gray", linewidth=0.4, zorder=1)
    df_points.plot(ax=axes[1][1], cmap="cool", column="degree", markersize=3, zorder=2)
    axes[1][1].set_title("With ax limits [1][1]")
    

    enter image description here