Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize the 3D effect of Basemap by matplotlib

Shulou Source: shulou.com Published: 2022-06-01 18:37:01 10月01日 Update

Editor to share with you how matplotlib to achieve the 3D effect of Basemap, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Matplotlib is a drawing plug-in in python

Matplotlib supports the effect of two-dimensional map, but also supports the effect of three-dimensional map. In the production of big data, you can take the map as the bottom and add the effect of a bar chart next to it to achieve a three-dimensional effect. There are many ready-made libraries in python, which is more convenient in development and can be directly used by import.

The specific implementation code is as follows:

# coding=utf-8

# reference the corresponding drawing class library

Import matplotlib.pyplot as plt

From mpl_toolkits.mplot3d import Axes3D

From mpl_toolkits.basemap import Basemap

From matplotlib.collections import PolyCollection

Import numpy as np

# initialize a basic map and 3D axis

Map = Basemap ()

Fig = plt.figure ()

Ax = Axes3D (fig)

# set 3D orientation angle, height and distance

Ax.azim = 270

Ax.elev = 50

Ax.dist = 8

# draw the coastline and national boundaries of the map on the bottom

Ax.add_collection3d (map.drawcoastlines (linewidth=0.25))

Ax.add_collection3d (map.drawcountries (linewidth=0.35))

# convert a face on a map into a picture on a three-dimensional axis

Polys = []

For polygon in map.landpolygons:

Polys.append (polygon.get_coords ())

Lc = PolyCollection (polys, edgecolor='black'

Facecolor='#123456', closed=False)

Ax.add_collection3d (lc)

# simulate bar chart data on a map, with coordinates according to longitude and latitude

Lons = np.array ([- 13.7,10.8,13.2,96.8,7.99,7.5,-17.3,3.7])

Lats = np.array ([9.6,6.3,8.5,32.7,12.5,8.9,14.7,40.39])

Cases = np.array ([1971, 7069, 6073, 4, 6, 20, 1,1])

Deaths = np.array ([1192, 2964, 1250, 1,5,8,0,0])

Places = np.array (['Guinea',' Liberia', 'Sierra Leone','United States',' Mali', 'Nigeria',' Senegal', 'Spain'])

X, y = map (lons, lats)

# the effect of adding a bar chart

Ax.bar3d (x, y, np.zeros (len (x)), 2, 2, deaths, color= 'rust, alpha=0.75)

Plt.show ()

The above is all the content of the article "how to achieve the 3D effect of Basemap by matplotlib". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Effects maps 3D coordinates articles columns content axes data support not much code national boundaries pictures foundations most bottoms plug-ins more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi macOS MariaDB Docker Apple