Plotting latitude and longitude from csv file

Viewed 1319

Hello been trying to do this all day! I have created a map with Basemap and I am trying to plot all the latitude and longitude locations from the CSV file. Any ideas or tips? Here is an image of the csv file

def map_test():
    col_list = ["longitude", "latitude"]
    dataset = pd.read_csv('charge_point_registry.csv', usecols=col_list)
    latitudes = dataset.loc[:, 'latitude']
    longitudes = dataset.loc[:, 'longitude']
    # Creates a base map ready for attributes

    plt.figure(figsize=(20, 15))
    m = Basemap(projection='mill',
            # coordinates of a box to contain map of UK
            llcrnrlat=48.632909,
            llcrnrlon=-14.452873,
            urcrnrlon=3.136989,
            urcrnrlat=61.648162,
            # quality of map
            resolution='l')
   m.drawcoastlines()
   m.drawcounties()
   m.fillcontinents(color = "green")
   geometry = [Point(xy) for xy in zip(longitudes, latitudes)]
   gdf = GeoDataFrame(dataset, geometry=geometry)
   gdf.plot(ax=m.plot(figsize=(20, 15)), marker='o', color='red', markersize=15)


# m.bluemarble()
  plt.show()

Thanks :)

1 Answers

Matplotlib Basemap is deprecated in favor of Cartopy. Here is some code that will allow you to plot your UK Data.

import numpy as np
import cartopy
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import pandas as pd

long_list = np.arange(-179.5, 180, 1)
lat_list = np.arange(-89.5, 90, 1)

value_dict = {'latitude':[51.0, 51.2, 53.4, 54.5],
              'longitude':[-1.1, -1.3, -0.2, -0.9]}

#replace this with your df
df = pd.DataFrame(value_dict)

proj = ccrs.PlateCarree(central_longitude=0)

fig, ax = plt.subplots(subplot_kw=dict(projection=proj), figsize=(16,16))
ax.set_extent([-10, 3, 48, 61], crs=ccrs.PlateCarree())

fig.canvas.draw()
fig.tight_layout()


ax.add_feature(cfeature.LAND, facecolor='0.25')
ax.add_feature(cfeature.BORDERS, zorder=10)

ax.scatter(df['longitude'].values.tolist(), df['latitude'].values.tolist())

gl = ax.gridlines(crs=proj, draw_labels=False, alpha=1, linewidth=0.25)
gl.xlocator = mticker.FixedLocator(long_list)
gl.ylocator = mticker.FixedLocator(lat_list)

plt.show()

England Map

Related