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 read netcdf data and draw on matplotlib Basemap

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly shows you "how to read netcdf data and draw on matplotlib Basemap", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to read netcdf data and draw on matplotlib Basemap" this article.

Netcdf is a data format

Matplotlib is a drawing plug-in in python

Use python to do map effect, is the need of work, meteorological data are generally stored in netcdf, there is no in-depth study of python language, but in accordance with the requirements to achieve a realization effect.

The following code details how to read data from netcdf and how it works on a basemap map.

Python development environment and download code in the various wheels, please Baidu search to deploy, I am not familiar with, the following code is based on python2.7 development, netcdf sample data can also be downloaded from the Internet

# set the encoding format and reference the various class libraries needed

# coding=utf-8

Import matplotlib.pyplot as plt

Import numpy as np

Import scipy.ndimage

From mpl_toolkits.basemap import Basemap, cm

Import netCDF4 as nc

Import matplotlib.colors as colors

# the path setting of netcdf files, which uses a backslash, which is different from that commonly used in windows

Filename= "E:/git/grib2json/20171011/20171011-3KM_WEST.nc"

# Open the nc file, get the data according to variables, and learn the format of nc data in detail

Data=nc.Dataset (filename)

Lat= data.variables ['XLAT'] [:] .squeeze ()

Lon= data.variables ['XLONG'] [:] .squeeze ()

Pm25=data.variables ['pm25'] [:] .squeeze ()

Cutpm= pm25 [20, 4,:,:]

# initialize a basemap object, set the coordinate system to latitude and longitude, followed by a range

M = Basemap (epsg=4326,resolution='i',llcrnrlat=23,urcrnrlat=37,llcrnrlon=97,urcrnrlon=112)

Fig1 = plt.figure ()

X, y = m (lon, lat)

# initialize the color level and render different colors according to different values

Levels = [0,35,75,115,150,250,350,500]

MyRBB = np.array ([[153,219,231], [26,161,206], [0123,217], [103,255,136], [10,227,50], [50,244,62], [228,243,62], [2261,151,62])

Cmap = colors.ListedColormap (myRBB / 255.0)

Norm = colors.BoundaryNorm (boundaries=levels, ncolors=len (levels))

# draw on the map according to the above array of x\ y\ values

CS2 = m.contourf (x, y, cutpm,cmap=cmap,alpha=0.75)

# draw coastlines and national boundaries

M.drawcoastlines (linewidth=0.5)

M.drawcounties (linewidth=0.3)

Plt.title ('pm25', size=20)

# load a map image that comes with base

M.etopo ()

# Show the painting window

Plt.show ()

Note: when in use, please remove all comments, it is easy to report errors

The following is the effect picture:

The above is all the contents of the article "how to read netcdf data and draw on matplotlib Basemap". 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!

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