【数据可视化】全球 CO2 浓度展示


前言

数据可视化,用到了matplot和cartopy
cartopy文档:https://scitools.org.uk/cartopy/docs/latest/gallery/scalar_data/waves.html#sphx-glr-gallery-scalar-data-waves-py


安装cartopy

conda install -c conda-forge cartopy

1.数据

1.1 数据读取

import h5py
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt # plt 用于显示图片
import matplotlib.image as mpimg # mpimg 用于读取图片
import numpy as np

f=h5py.File("./GOSATTFTS2020060120200630_03C01SV0291.h5","r+")
f.keys()
# <KeysViewHDF5 ['Attribute', 'Data', 'Global']>
# 读取所有latitude 纬度信息
dset1 = f["Data"]["geolocation"]["latitude"]
latitude = np.array(dset1)
latitude
###
array([[ 88.75,  88.75,  88.75, ...,  88.75,  88.75,  88.75],
       [ 86.25,  86.25,  86.25, ...,  86.25,  86.25,  86.25],
       [ 83.75,  83.75,  83.75, ...,  83.75,  83.75,  83.75],
       ...,
       [-83.75, -83.75, -83.75, ..., -83.75, -83.75, -83.75],
       [-86.25, -86.25, -86.25, ..., -86.25, -86.25, -86.25],
       [-88.75, -88.75, -88.75, ..., -88.75, -88.75, -88.75]],
      dtype=float32)
###
# 读取所有latitude 纬度信息
dset2 = f["Data"]["geolocation"]["longitude"]
longitude = np.array(dset2)
longitude
###
dset2 = f["Data"]["geolocation"]["longitude"]
longitude = np.array(dset2)
longitude
###

###
###
# 读取所有co2 浓度
dset3 = f["Data"]["mixingRatio"]["XCO2"]
co2 = np.array(dset3)

在这里插入图片描述

1.2 数据预处理

co2的浓度,-9999时没测到的,

mask = co2 == -9999
mask

在这里插入图片描述

co2[mask] = 0.0

1.3 查看最大,最小值

np.amax(co2)
np.amin(co2)

在这里插入图片描述

2.出图

import cartopy.crs as ccrs
from cartopy.mpl.geoaxes import GeoAxes
from cartopy.mpl.ticker import LongitudeFormatter, LatitudeFormatter
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import AxesGrid
import numpy as np


def sample_data_3d(shape):
    """Return `lons`, `lats`, `times` and fake `data`"""
    ntimes, nlats, nlons = shape
    lats = np.linspace(-np.pi / 2, np.pi / 2, nlats)
    lons = np.linspace(0, 2 * np.pi, nlons)
    lons, lats = np.meshgrid(lons, lats)
    wave = 0.75 * (np.sin(2 * lats) ** 8) * np.cos(4 * lons)
    mean = 0.5 * np.cos(2 * lats) * ((np.sin(2 * lats)) ** 2 + 2)

    lats = np.rad2deg(lats)
    print(lats.shape)
    lons = np.rad2deg(lons)
    data = co2[::-1,:]

    times = np.linspace(-1, 1, ntimes)
    new_shape = data.shape + (ntimes, )
    data = np.rollaxis(data.repeat(ntimes).reshape(new_shape), -1)
    data *= times[:, np.newaxis, np.newaxis]

    return lons, lats, times, data


def main():
    projection = ccrs.PlateCarree()
    axes_class = (GeoAxes,
                  dict(projection=projection))

    lons, lats, times, data = sample_data_3d((6, 72, 144))


    fig = plt.figure(figsize=(10,5))
    axgr = AxesGrid(fig, 111, axes_class=axes_class,
                    nrows_ncols=(1, 1),
                    axes_pad=0.6,
                    cbar_location='right',
                    cbar_mode='single',
                    cbar_pad=0.2,
                    cbar_size='3%',
                    label_mode='')  # note the empty label_mode

    for i, ax in enumerate(axgr):
        if i != 0:
            continue
        ax.coastlines()
        ax.set_xticks(np.linspace(-180, 180, 18), crs=projection)
        ax.set_yticks(np.linspace(-90, 90, 10), crs=projection)
        lon_formatter = LongitudeFormatter(zero_direction_label=True)
        lat_formatter = LatitudeFormatter()
        ax.xaxis.set_major_formatter(lon_formatter)
        ax.yaxis.set_major_formatter(lat_formatter)

        p = ax.contourf(lons, lats, co2[::-1,:],
                        transform=projection,
                        levels=[390, 395, 400, 405, 410, 415],
                        colors=['#80f9ad', '#fff39a', '#ffc512', '#fb7d07', '#E50000']
                )
    

    axgr.cbar_axes[0].colorbar(p)
    ax.axis('off')  # 去掉坐标轴
    plt.savefig("./1.jpg", transparent=True, dpi=300, pad_inches=0, bbox_inches='tight')
    plt.show()
main()

在这里插入图片描述

注意点:左下角是数据的起点,所以,我们的co2的数据需要倒置一下。这是一个注意点,否则对不上。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值