编程语言
首页 > 编程语言> > python的matplotlib的Basemap案例

python的matplotlib的Basemap案例

作者:互联网

在这里插入图片描述

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
import numpy as np

# city population in 2017
locations = {"Sydney":5131326,"Melbourne":4850740,
             "Brisbane":2408223,"Adelaide":1333927,
             "Perth":2043138,"Hobart":226884,
             "Darwin":146612,"Canberra":410301}

# Latitude and Longitude in degrees
names = {"Sydney":(-33.86785,151.20732),"Melbourne":(-37.8142,144.96332),
         "Brisbane":(-27.46794,153.02809),"Adelaide":(-34.92866,138.59863),
         "Perth":(-31.95224,115.8614),"Hobart":(-42.87936,147.32941),
         "Darwin":(-12.46113,130.84185),"Canberra":(-35.28346,149.12807)}

# setup mercator map projection
basemap = Basemap(projection="merc",
              resolution="h",
              area_thresh=0.1,
              llcrnrlon=112,llcrnrlat=-45,
              urcrnrlon=155,urcrnrlat=-8)

# draw several map elements
basemap.drawcoastlines(linewidth=0.6,linestyle="-",color="#b7cfe9",zorder=3)
basemap.drawrivers(linewidth=0.8,linestyle="-",color="#689CD2",zorder=2)

basemap.fillcontinents(color="#BF9E30",lake_color="#689CD2",zorder=1)
basemap.drawmapboundary(color="gray",fill_color="#689CD2")

basemap.drawmeridians(np.arange(0,360,15),color="#4e8bca",labels=[0,0,0,1],labelstyle="+/-")
basemap.drawparallels(np.arange(-90,90,15),color="#4e8bca",labels=[1,1,0,0],labelstyle="+/-")


# convert lon/lat (in degrees) to x/y map projection coordinates (in meters)
# longitude is transformed into x and latitude is transformed into y
names_values = []
names_keys = list(names.keys())
for i,name in enumerate(names_keys):
    names_values.append(names[name])

lat_x,long_y = list(zip(*names_values))
x,y = basemap(long_y,lat_x)

# draw city markers and add text to markers
size_factor = 80.0
offset_factor = 21000
rotation = 30
max_population = max(locations.values())

for city_name,city_x,city_y in zip(names_keys,x,y):
    size = (size_factor/max_population)*locations[city_name]
    x_offset = offset_factor
    y_offset = offset_factor
    basemap.scatter(city_x,
                    city_y,
                    s=size,
                    facecolor="w",
                    edgecolors="r",
                    linewidths=2.0,
                    zorder=10)
    plt.text(city_x+x_offset,city_y+y_offset,city_name)

# setup map title
font = dict(family="serif",fontsize=15,weight="bold")
plt.title("Australian Population of Capital City",**font)

plt.show()

在这里插入图片描述

import datetime
import matplotlib.pyplot as plt
import numpy as np

from mpl_toolkits.basemap import Basemap

# setup miller projection
basemap = Basemap(projection="mill",
                  resolution="h",
                  area_thresh=0.1,
                  llcrnrlon=-180,
                  llcrnrlat=-90,
                  urcrnrlon=180,
                  urcrnrlat=90)

# draw coastlines
basemap.drawcoastlines(linewidth=0.6,zorder=2)
# draw mapboundary
basemap.drawmapboundary(fill_color="aqua")
# fill continents with color "coral", and lake "aqua"
basemap.fillcontinents(color="coral",lake_color="aqua",zorder=1)
# draw meridians and parallels
basemap.drawmeridians(np.arange(-120,150,60),linewidth=0.6,labels=[0,0,0,1])
basemap.drawparallels(np.arange(-60,80,30),linewidth=0.6,labels=[1,0,0,0])

# shade the night areas, and use current time in UTC
date = datetime.datetime.utcnow()
basemap.nightshade(date)

# format title with date and time
content = "Shade dark regions of the map %s (UTC)"
dtFormat = "%d %b %Y %H:%M:%S"
stringTime = date.strftime(dtFormat)
plt.title(content % stringTime,fontsize=15)

plt.show()

在这里插入图片描述

import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

from mpl_toolkits.basemap import Basemap

class MapDisVisualization(Basemap):

    # get city names
    def getCityNames(self,names):
        namesKeys = list(names.keys())
        return namesKeys

    # define distance between cityA and cityB
    def citiesDistance(self,x,y):
        d = np.power(np.power(x[0]-y[0],2)+np.power(x[1]-y[1],2),0.5)
        distance = round(d,4)
        return distance

    # compute distance between target city and every other city
    def centerCityDistance(self,city,names):
        distanceDict = {}
        namesKeys = self.getCityNames(names)
        for i,name in enumerate(namesKeys):
            if name != city:
                distanceDict[name] = self.citiesDistance(names[city],names[name])
        return distanceDict

    # compute line width and line color
    def setcolorandwidth(self,city,names):
        size_factor = 2.0
        namesKeys = self.getCityNames(names)
        distanceDict = self.centerCityDistance(city,names)
        distanceList = list(distanceDict.values())
        maxDistance = max(distanceList)
        for i,name in enumerate(namesKeys):
            if name != city:
                self.drawgreatcircle(names[city][1],names[city][0],
                                     names[name][1],names[name][0],
                                     linewidth=size_factor,
                                     color=mpl.cm.Blues(distanceDict[name]/float(maxDistance)))

    # visualize city distance on the map
    def showmap(self,city,names):
        self.setcolorandwidth(city,names)
        namesKeys = self.getCityNames(names)
        number = len(namesKeys)
        titleContent = "a map of visualizing distance between %s and every other city (%d cities)"
        font = dict(family="serif",fontsize=15,weight="black")
        plt.title(titleContent % (city,(number-1)),fontdict=font)
        plt.show()

def main(projection,city):
    # get a Basemap instance
    m = MapDisVisualization(projection=projection,
                            resolution="h",
                            area_thresh=0.1,
                            llcrnrlon=112,llcrnrlat=-50,
                            urcrnrlon=180,urcrnrlat=-8)

    # draw several elements on the map
    m.drawcoastlines(linewidth=0.6,linestyle="-",zorder=2)
    m.fillcontinents(alpha=0.5,zorder=1)
    m.drawmapboundary(color="gray")
    m.drawmeridians(np.arange(100,180,15),linewidth=0.4,labels=[0,0,0,1])
    m.drawparallels(np.arange(-90,0,15),linewidth=0.4,labels=[1,0,0,0])
        
    # Latitude and Longitude in degrees
    names = {"Sydney":(-33.86785,151.20732),"Wellington":(-41.28664,174.77557),
             "Brisbane":(-27.46794,153.02809),"Adelaide":(-34.92866,138.59863),
             "Perth":(-31.95224,115.8614),"Auckland":(-36.86667,174.76667),
             "Darwin":(-12.46113,130.84185),"Canberra":(-35.28346,149.12807)}

    #show the distance between Sydney and every other city
    m.showmap(city,names)

if __name__ == "__main__":
    # use projection mercator and choose Sydney
    main("merc","Sydney")

标签:city,name,python,Basemap,self,matplotlib,color,names,basemap
来源: https://blog.csdn.net/weixin_43118073/article/details/113447031