Hướng dẫn dùng geopandas plot python
(English below) GeoPandas 0.9.0 GeoPandas là một dự án mã nguồn mở giúp làm việc với dữ liệu địa không gian trong python dễ dàng hơn. GeoPandas mở rộng các kiểu dữ liệu được Khung dữ liệu Pandas sử dụng để cho phép các hoạt động không gian trên các kiểu hình học. Các phép toán hình học được thực hiện bởi shapely. Geopandas còn phụ thuộc vào fiona để truy cập tệp và matplotlib để vẽ biểu đồ.
Miêu tả GeoPandas là một dự án dẫn đầu cộng đồng được viết, sử dụng và hỗ trợ bởi nhiều người từ khắp nơi trên thế giới với nhiều nguồn gốc khác nhau. GeoPandas sẽ luôn là phần mềm nguồn mở 100%, miễn phí cho tất cả mọi người sử dụng và được phát hành theo các điều khoản tự do của giấy phép BSD-3-Khoản. Thông tin chi tiết về hướng dẫn sử dụng:
https://geopandas.org/getting_started.html
https://geopandas.org/docs.html
https://geopandas.org/about.html
https://geopandas.org/community.html ------- Using geospatial data in python easier with Geopandas GeoPandas is an open source project to make working with geospatial data in python easier. GeoPandas extends the datatypes used by pandas to allow spatial operations on geometric types. Geometric operations are performed by shapely. Geopandas further depends on fiona for file access and matplotlib for plotting. Description GeoPandas is a community-led project written, used and supported by a wide range of people from all around of world of a large variety of backgrounds. GeoPandas will always be 100% open source software, free for all to use and released under the liberal terms of the BSD-3-Clause license. Geolink tổng hợp từ Geopandas Being an intern at FORSK TECHNOLOGIES,I have explored quite a few Python libraries (Matplotlib, Pandas, Numpy, Seaborn, Shapefile, Basemap, Geopandas) which have really helped in plotting data(somehow real-time data too..) over maps. Mapping Geograph In PythonVisualizing data over a map is very helpful while working on data science which can be done through modules such as geopandas etc. Here we will be exploring the method to create geo map and visualize data over it, using shapefiles(.shp) and some other Python libraries. Here we will be working on city wise population of Rajasthan following visualisation of data on a map. The shapefile required for this article can be downloaded from this link click here Installing Shapefile Library ~ conda/pip install pyshp Importing Libraries import numpy as np Initializing Visualization Set sns.set(style=”whitegrid”, palette=”pastel”, color_codes=True) sns.mpl.rc(“figure”, figsize=(10,6)) Opening The Vector Map A vector map is a group of several files with a .shp format. #opening the vector mapshp_path = “\\District_Boundary.shp”#reading the shape file by using reader function of the shape libsf = shp.Reader(shp_path) Number of different shapes which were imported by shp.reader len(sf.shapes()) The result will come out to be 33 which tells us that there are 33 shapes or we can say cities in the region of Rajasthan. To explore those records: sf.records()
A sample outputTo explore a particular record where 1 is the Id or row number and 0 refers to the column: sf.records()[1][0] Result- Output= Barmer Converting Shapefile Data Into Pandas Dataframes: Making accessing cities easier by converting shapefile data into a more relatable Pandas Dataframe format. def read_shapefile(sf): Visualization of data after being converted into Dataframes where it refers to rows and columns df = read_shapefile(sf)df.shape Dataframe having a shape of (33,6) means it has 33 rows and 6 columns in it. Let’s See a Sample Of The Dataframe Created # sample of a data representation the last point has the coordinates of the data latitude and longitude which will be used to create a specific map shapedf.sample(5) The result will look like — OutputHere cords are the latitude and longitudes which will be used to create the map. Plotting The Map Of a City In Rajasthan Or a Specific Shape With The Help Of Matplotlib #a) Plots the shape (polygon) based on the city’s coordinates and, #b) calculates and return the medium point of that specific shape (x0, y0). #This medium point is also used to define where to print the city name. def plot_shape(id, s=None): Setting The City Name To Plot Respective Map DIST_NAME = ‘JAIPUR’#to get the id of the city map to be plottedcom_id = df[df.DIST_NAME == ‘JAIPUR’].index.get_values()[0]plot_shape(com_id, DIST_NAME)sf.shape(com_id)Output
Plotting The Full Map This particular function combines all the single shapes that were made using coordinates and show it as a combined shape. def plot_map(sf, x_lim = None, y_lim = None, figsize = (11,9)): Plotting a Zoomed Map y_lim = (2900000,3000000) # latitudex_lim = (200000, 400000) # longitudeplot_map(sf, x_lim, y_lim)output Highlighting a Single Shape Over a Complete Map Combining previous functions will do the work. Where we can plot a single shape over a complete map. ID and the colour code will be the required parameters for the function. def plot_map_fill(id, sf, x_lim = None,Desired Output Highlighting Multiple Shapes Over The Complete Map With City ID’s With the help of this function, as parameters, we can give ID’s of the multiple cities which will result in highlighting multiple cities instead of 1. def plot_map_fill_multiples_ids(title, city, sf, Let’s see how the map looks like #naming the id numbers of the cities to be colouredcity_id = [0, 1, 2, 3, 4, 5, 6]plot_map_fill_multiples_ids(“Multiple Shapes”,city_id, sf, color = ‘g’) Highlighting Multiple Shapes Over The Complete Map By City Names Recently we have encountered the problem of highlighting shapes with city ID(index) but though we have our data as Pandas Dataframe we can also do it with mentioning the city names. # plotting the city on the map to be coloured by using the dist_namedef plot_cities_2(sf, title, cities, color): Let’s have a look at the output south = [‘jaipur’,’churu’,’bikaner’]plot_cities_2(sf, ‘DIST’, south, ‘c’) Plotting a Heat Map It is a type of map where shapes are filled with a specific colour of varying intensities according to the value provided. It provides clear data interpretation in the geographic format. In the first function, we will divide our list of data on intervals or bins where each bin will have a specific colour intensity, 6 bins and 4 different colour pallets. def calc_color(data, color=None): Functions plot_cities() and plot_map_fill_multiples_ids should be adapted to take advantage of this new coloured scheme: def plot_cities_data(sf, title, cities, data=None,color=None, print_id=False): Let’s take an example to plot the data in a heat map presentational format. names= [‘jaipur’,’bikaner’,’churu’,’bhilwara’,’udaipur’]data = [100, 2000, 300, 400000, 500, 600, 100, 2000, 300, 400, 500, 600, 100, 2000, 300, 400, 500, 600]print_id = True # The shape id will be printedcolor_pallete = 1 # ‘Purple’plot_cities_data(sf, ‘Heat map of given cities’, names, data, color_pallete, print_id) Plotting Real Data Plotting the population of Rajasthan region which is here referred to the real data. # reading data set Hope you have understood the concept of plotting maps through Python libraries. You can refer my GITHUB for the exact code. Mapping With GeopandasAs we have seen the procedure of mapping with Pandas Dataframe, now its turn to visualize it with Geopandas Dataframe. Geopandas makes working easier with geospatial data (data that has a geographic component to it) in Python. It combines the capabilities of Pandas and shapely by operating a much more compact code. It is one of the best ways to get started with making choropleth maps. Let’s start with some mapping through Geopandas and let’s map Rajasthan’s population on it! Shapefile used in the previous topic is sufficient to work further for Geopandas. Installation conda install geopandas The Very First Step Is To Import Required Libraries import pandas as pd Getting The Data Of Interest Rajasthan being the largest state of India is a highly populated state. Mapping its population will make visualization much simpler and efficient. Let’s set the path to open the shapefile for the Rajasthan region through Geopandas. # set the filepath and load Let’s preview the map #plotting the map of the shape file preview of the maps without data in it Now it’s the time to open the CSV file which contains the data to plot over. Here, we could have also made a csv for the data required but I have extracted the data from the shapefile only instead of making the csv or searching it over the web which has saved a lot of time. #opening the csv(.shp) file which contains the data to be plotted on the map Let’s preview the Geodataframe # check dat dataframe Now, let’s join our geodata with our dataset # joining the geodataframe with the cleaned up csv dataframe Time To Map First, we need to do some pre-required work for the Matplotlib to plot the map as setting the variable, range and creating a basic figure for the map. # set a variable that will call whatever column we want to visualise on the map Time To Create The Map merged.plot(column=variable, cmap=’BuGn’, linewidth=0.8, ax=ax, edgecolor=’0.8') This is what we wanted, the Map is ready! but requires some beautification and customization. # remove the axis Colour bar is a must thing in a map which tells us the parameters to look for, let’s customize it to our map. # Create colorbar as a legend You might have got why mapping with Geopandas is better to get started with. It has a very compact and simple code and gives an excellent desired output. We can plot any kind of data over any region through this approach. Refer my GITHUB for the exact code. Mapping With BasemapThe Matplotlib basemap toolkit is a library for plotting 2D data on maps in Python. Basemap does not do any plotting on its own but provides the facilities to transform coordinates to one of 25 different map projections. Matplotlib is then used to plot contours, images, vectors, lines or points in the transformed coordinates. Shoreline, river and political boundary datasets are provided, along with methods for plotting them. In this section, you will be learning about plotting data on a map through basemap toolkit. Let’s see the map visualization through basemap toolkit. Installation conda install basemap Importing Libraries import numpy as np
#to import the basemap library give the direct path to the libraryimport os Let’s get our data of interest to be plotted from the csv and the shapefile. Here’s the link to download the csv required CLICK HEREand shapefile required CLICK HERE city=gpd.read_file("F:\\District_Boundary.shp") We start by loading the data. Lat-long has been imported from a separate csv made and other data such as district names and their population from the .shp file downloaded in previous sections. lat=csv['LAT'].values
Next, we set up the map projection, scatter the data, and then create a colour bar fig = plt.figure(figsize=(8, 8)) Our map background is now ready on which data can be plotted. Here lat-long of Rajasthan, India have been set with ‘lcc’ projection with a certain amount of zoom to focus only the particular state. Let’s add some details to it and separate the boundaries. m.drawcoastlines(color='blue',linewidth=3) Now its time to scatter the data over the map projection and set the colour bar. # scatter city data, with c reflecting population Doesn’t it look like something is missing? Yes! of course, the district names. We are not able to identify the district through this projection. Let’s Plot them.
dict1={} Now, lat-long have been stored into a dictionary(dict1) and district names in a list(list3). Let’s use them for naming districts over projection. i=0# Map (long, lat) to (x, y) for plotting Refer my GITHUB for the exact code. There you have it. Thank you for reading. For more on Geopandas, Basemap or you want to learn trending technologies in industry like Python, ML, DL, AI, IoT etc, Connect with Forsk Technologies. |