Matplotlib 简明教程
Matplotlib - Basemap
What is Basemap?
Matplotlib Basemap 工具包是 Matplotlib 的扩展,可提供用于创建涉及地理数据的映射与可视化的功能。它允许用户在各个地图投影中绘制数据、绘制海岸线、国家和其他地图要素。这些用于在 Matplotlib 中无缝处理地理坐标。
The Matplotlib Basemap toolkit is an extension to Matplotlib that provides functionality for creating maps and visualizations involving geographical data. It allows users to plot data on various map projections, draw coastlines, countries and other map features . These are used to handle geographic coordinates seamlessly within Matplotlib.
以下是 Matplotlib Basemap 的主要功能。
The below are the key features of Matplotlib Basemap.
Map Projections
Basemap 支持各种地图投影,允许用户以圆柱、圆锥或方位投影等不同的坐标系可视化数据。例如,麦卡托、兰伯特正形圆锥投影和正射投影。
Basemap supports various map projections by allowing users to visualize data in different coordinate systems like cylindrical, conic or azimuthal projections. Examples are Mercator, Lambert Conformal Conic and Orthographic projections.
Plotting Geographic Data
Basemap 允许绘制地图上的地理数据,例如点、线或多边形。用户可以将数据集叠加到地图上,并可视化地理关系。
Basemap allows the plotting of geographical data such as points, lines, or polygons over maps. Users can overlay datasets onto maps and visualize geographic relationships.
Installing Basemap
如果我们想在 Matplotlib 库中使用 Basemap,则需要在我们的工作环境中安装它。以下为安装代码。
If we want to work with Basemap of matplotlib library we have to install it in our working environment. The below is the code.
Basic Workflow with Basemap
以下是 Matplotlib 库 Basemap 的基本工作流程。让我们详细了解其中的每一个,以便更好地理解。
The below is the basic workflow of the Basemap of the Matplotlib library. Let’s see each of them in detail for better understanding.
Create a Basemap Instance
通过指定地图投影、边界坐标和其他参数实例化一个 Basemap 对象。为了使用 Matplotlib 中的 Basemap 工具包创建 Basemap 实例,我们可以定义地图投影并指定所需的地图边界。
Instantiate a Basemap object by specifying the map projection, bounding coordinates and other parameters. To create a Basemap instance using the Basemap toolkit in Matplotlib we can define the map projection and specify the desired map boundaries.
在这个示例中,我们使用指定的墨卡托投影和提供的边界坐标生成基本地图(带海岸线和国家边界)。我们可以通过添加要素、绘制数据点或基于我们的要求使用不同的投影和分辨率来进一步自定义地图。
In this example we are generating a basic map with coastlines and country boundaries using the specified Mercator projection and the provided bounding coordinates. We can further customize the map by adding features, plotting data points or using different projections and resolutions based on our requirements.
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
# Create a Basemap instance with a specific projection and bounding coordinates
map = Basemap(
projection='merc', llcrnrlat=-80, urcrnrlat=80,
llcrnrlon=-180, urcrnrlon=180, resolution='c')
# Draw coastlines and countries
map.drawcoastlines()
map.drawcountries()
# Show the map
plt.show()
Plot Data on the Map
在这里,我们使用 Basemap 方法绘制地图要素、绘制数据点或可视化地理数据集。
In this we use the Basemap methods to draw map features, plot data points or visualize geographical datasets.
在这个示例中,我们生成随机的纬度和经度点,然后使用 map() 函数将这些坐标投影到 Basemap 实例上。然后使用 scatter() 方法将这些投影点以红色标记的形式绘制在地图上。
In this example we are generating random latitude and longitude points and then uses the map() function to project these coordinates onto the Basemap instance. The scatter() method is then used to plot these projected points on the map as red markers.
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
# Create a Basemap instance with a specific projection and bounding coordinates
map = Basemap(
projection='merc', llcrnrlat=-80, urcrnrlat=80,
llcrnrlon=-180, urcrnrlon=180, resolution='c')
# Draw coastlines and countries
map.drawcoastlines()
map.drawcountries()
# Generate random data (longitude, latitude) for plotting
num_points = 100
lons = np.random.uniform(low=-180.0, high=180.0, size=num_points)
lats = np.random.uniform(low=-80.0, high=80.0, size=num_points)
# Plot the data points on the map
x, y = map(lons, lats) # Project the latitudes and longitudes to map coordinates
map.scatter(x, y, marker='o', color='red', zorder=10) # Plotting the data points
# Show the map with plotted data
plt.title('Data Points on Map')
plt.show()
Display the Map
我们可以使用 Matplotlib 库的 show() 函数来显示最终地图。
We can use Matplotlib library to show() function to display the final map.
以下示例演示了如何使用 Basemap 创建地图和绘制数据点。
Here’s an example demonstrating the usage of Basemap to create a map and plot data points.
from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np
# Creating a Basemap instance with a specific projection and bounding coordinates
map = Basemap(
projection='merc', llcrnrlat=-80, urcrnrlat=80,
llcrnrlon=-180, urcrnrlon=180, resolution='c')
# Drawing coastlines, countries, and states
map.drawcoastlines()
map.drawcountries()
map.drawstates()
# Generating random data for plotting
num_points = 100
lons = np.random.uniform(low=-180.0, high=180.0, size=num_points)
lats = np.random.uniform(low=-80.0, high=80.0, size=num_points)
data_values = np.random.rand(num_points) * 100 # Random values for data
# Plotting data points on the map
x, y = map(lons, lats) # Projecting latitudes and longitudes to map coordinates
map.scatter(x, y, c=data_values, cmap='viridis', marker='o', alpha=0.7)
# Adding a colorbar to represent the data values
plt.colorbar(label='Data Values')
# Display the map with plotted data
plt.title('Basemap Example with Data Points')
plt.show()
Applications of Matplotlib Basemap
Geospatial Analysis − 分析和可视化地理数据,如气候模式、人口分布或地震活动。
Geospatial Analysis − Analyzing and visualizing geographical data such as climate patterns, population distributions or seismic activities.
Cartography − 为出版物、演示文稿或研究目的创建自定义地图。
Cartography − Creating custom maps for publications, presentations or research purposes.
Data Visualization − 将地理数据与其他数据集集成,以便探索性数据分析。
Data Visualization − Integrating geographical data with other datasets for exploratory data analysis.
Note
Matplotlib Basemap 已逐步淘汰,取而代之的是像 Cartopy 这样的更新的地理空间库,它们提供了更丰富的功能,并与 Matplotlib 更好地集成。Cartopy 以 Basemap 的概念为基础,但为在 Matplotlib 中处理地理空间数据提供了更现代、更灵活且积极开发的界面。
Matplotlib Basemap is being phased out in favor of newer geospatial libraries like Cartopy which offer more extensive functionality and better integration with Matplotlib. Cartopy builds on the concepts of Basemap but provides a more modern, flexible and actively developed interface for handling geospatial data within Matplotlib.