Home > other >  plot df on map - two plots instead of one
plot df on map - two plots instead of one

Time:06-13

enter image description hereI want to plot data from a df in a map. However, I don't manage to print out the values in the plot, but I get two plots - one with an empty map and one with the values without map. What am I doing wrong?

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import cartopy
import cartopy.crs as ccrs
import cartopy.feature as cf

#ax1=plt.axes(projection=ccrs.Mercator())
#fig, ax=plt.subplots(figsize=(8,6))
ax1.add_feature(cf.BORDERS);
ax1.add_feature(cf.RIVERS);
ax1.set_extent([7.4, 8.8, 47.5, 49.1])
ax1.set_title('Niederschlag', fontsize=13);
ax.grid(b=True, alpha=0.5)


df.plot(x="longitude", y="latitude", kind="scatter", 
c='RR',colormap="YlOrRd")

plt.show()

Thanks in advance

CodePudding user response:

pandas.DataFrame.plot creates a new plot by default, this is why you are getting two plots: first you crate one with ax1=plt.axes(projection=ccrs.Mercator()), then pandas creates another one.

To fix this you can use the ax argument on pandas.DataFrame.plot:

df.plot(x="longitude", y="latitude", kind="scatter", c='RR',colormap="YlOrRd", ax=ax1)

CodePudding user response:

try this : plt.scatter(x=df['Longitude'], y=df['Latitude'])

or try using geo pandas like so

    import pandas as pd
    from shapely.geometry import Point
    import geopandas as gpd
    from geopandas import GeoDataFrame
    
    df = pd.read_csv("Long_Lats.csv", delimiter=',', skiprows=0, low_memory=False)
    
    geometry = [Point(xy) for xy in zip(df['Longitude'], df['Latitude'])]
    gdf = GeoDataFrame(df, geometry=geometry)   
    
    #this is a simple map that goes with geopandas
    world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
    gdf.plot(ax=world.plot(figsize=(10, 6)), marker='o', color='red', markersize=15);
  • Related