geomap-from-csv.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # conda activate GEOGRAPHICMAP
  2. # conda install basemap basemap-data-hires
  3. # pip install pandas
  4. # https://jakevdp.github.io/PythonDataScienceHandbook/04.13-geographic-data-with-basemap.html
  5. # https://github.com/reddyprasade/DataSet-for-ML-and-Data-Science/blob/master/DataSets/california_cities.csv
  6. import numpy as np
  7. import matplotlib.pyplot as plt
  8. from mpl_toolkits.basemap import Basemap
  9. import pandas as pd
  10. cities = pd.read_csv('california_cities.csv')
  11. # Extract the data we're interested in
  12. lat = cities['latd'].values
  13. lon = cities['longd'].values
  14. population = cities['population_total'].values
  15. area = cities['area_total_km2'].values
  16. # 1. Draw the map background
  17. fig = plt.figure(figsize=(8, 8))
  18. m = Basemap(projection='lcc', resolution='h',
  19. lat_0=37.5, lon_0=-119,
  20. width=1E6, height=1.2E6)
  21. m.shadedrelief()
  22. m.drawcoastlines(color='gray')
  23. m.drawcountries(color='gray')
  24. m.drawstates(color='gray')
  25. # 2. scatter city data, with color reflecting population
  26. # and size reflecting area
  27. m.scatter(lon, lat, latlon=True,
  28. c=np.log10(population), s=area,
  29. cmap='Reds', alpha=0.5)
  30. # 3. create colorbar and legend
  31. plt.colorbar(label=r'$\log_{10}({\rm population})$')
  32. plt.clim(3, 7)
  33. # make legend with dummy points
  34. for a in [100, 300, 500]:
  35. plt.scatter([], [], c='k', alpha=0.5, s=a,
  36. label=str(a) + ' km$^2$')
  37. plt.legend(scatterpoints=1, frameon=False,
  38. labelspacing=1, loc='lower left')
  39. plt.title('California Cities')
  40. plt.show()