0

enter image description hereenter 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

2 Answers2

0

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)

Edit

There is also a problem with the projection. You are setting the projection as Mercator which uses a different coordinates system from the standard one. When you set the bounds you are not specifying the projection and it being converted automatically. Your points though are in a different projection and no automatic conversion is performed.

To fix this I suggest using a different projection to start with (e.g.: PlateCarree). If this is not possible then you would need to convert the coordinates yourself. This code produces a map with points:

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

df = pd.DataFrame(
    {
        "longitude": [7.99, 8.42, 7.78],
        "latitude": [48.98, 48.74, 47.59],
        "RR": [5, 15, 25],
    }
)

ax1=plt.axes(projection=ccrs.PlateCarree())
ax1.add_feature(cf.BORDERS)
ax1.add_feature(cf.RIVERS)
ax1.set_extent([7.4, 8.8, 47.5, 49.1], crs=ccrs.PlateCarree())
ax1.set_title('Niederschlag', fontsize=13);
ax1.grid(b=True, alpha=0.5)


df.plot(x="longitude", y="latitude", kind="scatter", c="RR", colormap="YlOrRd", ax=ax1)
Matteo Zanoni
  • 3,429
  • 9
  • 27
0

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);
Atlas Bravoos
  • 360
  • 2
  • 12