【问题标题】:In metpy i would like to create skew-t plot animations does anyone know how to do that?在 metpy 中,我想创建 skew-t 绘图动画,有人知道该怎么做吗?
【发布时间】:2022-12-24 11:39:41
【问题描述】:

因此,在 metpy 中,我想创建一个基本的倾斜 t 图,其中包含具有近乎实时数据的动画 但我不知道要使用什么功能,我已经查找了一些 skew-t plot 动画,但找不到任何建议。

【问题讨论】:

  • 请提供足够的代码,以便其他人可以更好地理解或重现问题。

标签: python matplotlib weather


【解决方案1】:

所以我所做的是将 skewT 图的每一帧保存为 png 图像,然后使用 imageio 将图像放入 gif 中。

一、进口:

import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
import pandas as pd
import metpy as mp
import numpy as np
import metpy.plots as plots
from metpy.units import units
import glob
import imageio

我将每个 CSV(包含 skewT 数据)都放在同一个目录中,并且我使用 glob 将文件名放在一起:

df = []
titleNames = []

files = glob.glob("*.csv")
for a in files:
    df.append(pd.read_csv(a))
    titleNames.append(a[0:10])
    
#print(titleNames)

titleNames 是不带“.csv”的文件名列表,因此我可以根据文件名命名每个 skewT 图像。接下来,我定义了一个 skewT 绘图函数,并将这些图像中的每一个保存到当前目录中,并在底部使用 for 循环。

def skewT(dataframe, i):
    
    titleName = titleNames[i][0:4] + "Dec_" + titleNames[i][4:10]
    
    temp = np.array(dataframe.temp)*units.degC
    dew = np.array(dataframe.dew)*units.degC
    p = np.array(dataframe.press)*units.hPa
    wspd = np.array(dataframe.wspd)*units.knots
    wdir = np.array(dataframe.wdir)*units.degrees
    u, v = mp.calc.wind_components(wspd, wdir)
    
    fig = plt.figure(figsize=(12, 12))
    skew = plots.SkewT(fig, rotation=45)
    skew.ax.set_ylim(1000, 10)
    skew.plot_dry_adiabats()
    skew.plot_moist_adiabats()
    skew.plot_mixing_lines()
    skew.ax.set_ylabel('Pressure (hPa)')
    skew.ax.set_xlabel('Temperature (℃)')

    
    skew.plot(p, temp, 'r')
    skew.plot(p, dew, 'g')
    skew.plot_barbs(p, u, v)
    
    title = plt.title(u"{}".format(titleName))

for i in range(len(titleNames)):
    skewT(df[i], i)
    plt.savefig('test_{}.png'.format(titleNames[i]))

在每个图像保存到目录中后,我使用 glob 将它们组合在一起并使用 imageio 生成 gif:

images = []
filenames = glob.glob("*.png")

for filename in filenames:
    images.append(imageio.imread(filename))
imageio.mimsave('BNA_SkewT.gif', images, duration = 0.5)

Produces a GIF like this

【讨论】:

    猜你喜欢
    • 2018-01-11
    • 2022-01-08
    • 2021-01-21
    • 2019-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-03
    • 1970-01-01
    相关资源
    最近更新 更多