Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to draw Geographic Map

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use Python to draw a geographic map", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Python to draw a geographic map.

Plotly

Plotly is a well-known library for creating interactive drawings and dashboards in Python.

Install Plotly

Run these two commands from a command prompt to install plotly and cufflinks and all of their packages on our local machine.

Choropleth map

Choropleth maps are popular theme maps used to represent statistics through various shadow patterns or symbols on predetermined geographic areas (that is, countries). They are good at using data to easily express the variability of measurements needed for the entire region.

How does Choropleth Maps work?

Choropleth Maps displays color, shaded, or patterned geographic areas or areas associated with data variables. This provides a way to visualize values within a geographic area that can show changes or patterns in the displayed location.

Using Choropleth in Python

Here, we will use power consumption datasets from different countries around the world in 2014. (https://github.com/ahmadbinshafiq/Geographical-Plotting---Python/blob/master/2014_World_Power_Consumption)

All right, let's get started.

Import library

Here, init_notebook_mode (connected = True) connects Javascript to our notebook.

Create / interpret our DataFrame

Here, we have three columns, and all columns have 219 non-empty entries.

Compile our data into a dictionary

Type = 'choropleth': defines the type of map, that is, choropleth in this case.

Colorscale = 'Viridis': displays a color map (f or more color scales, see here).

Location = df ['Country']: add a list of all countries.

Locationmode = 'country name': because we have a country name in the dataset, we set the location mode to 'country name'.

Z: displays a list of integer values for each state's power consumption.

Text = df ['Country']: displays a text when you hover over each status element on the map. In this case, it is the name of the country itself.

Colorbar = {'title':'Power KWH'}: a dictionary containing information about the right column. Here, the color bar contains the title of the sidebar.

Layout-an Geo object that can be used to control the appearance of the underlying map on which the data is drawn.

This is a nested dictionary that contains all relevant information about the appearance of the map / drawing.

Generate a diagram / diagram

A choropleth map of World Power consumption 2014 is generated, from which you can see that as each country hovers over each element on the map, its name and power consumption (in kWh) are displayed. The more concentrated the data in a particular area, the darker the shadows on the map. "China" consumes the most power, so it is the darkest.

Density diagram

Density mapping is just a way to show that points or lines may be concentrated in a given area.

Using density diagrams in Python

Here, we will use worldwide earthquakes and their magnitude data sets.

All right, let's get started.

Create / interpret our DataFrame

Here, we have four columns, and all columns have 23412 non-empty entries.

Drawing data

Lat = 'Latitude': gets the Latitude column of the data box.

Lon = 'Longitude': gets the longitude column of the data box.

Z: displays an integer list of earthquake magnitudes.

Radius = 10: sets the radius of influence for each point.

Center = dict (lat = 0jinlon = 180): sets the center of the map in the dictionary.

Zoom = 0: sets the zoom level of the map.

Mapbox_style = 'stamen-terrain': sets the basic map style. Here, the "stamen terrain" is the basic map style.

Fig.show (): displays the map.

Map

We have drawn a density map of "earthquake and its intensity". From above, we can see that it covers all the territories damaged by the earthquake and shows the seismic intensity of each region when we hover over it.

Because of the variety of data formats, sometimes it can be challenging to use plotly for geographic mapping, so refer to this cheat sheet for all types of plotly plot syntax.

At this point, I believe you have a deeper understanding of "how to use Python to draw a geographic map". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report