Использование xarray с повторной выборкой и интерполяцией для анимации движения контуров.Python

Программы на Python
Гость
Использование xarray с повторной выборкой и интерполяцией для анимации движения контуров.

Сообщение Гость »


Lets say I have the following python code, showing a contour value at 12pm and 1pm, indicating that it's position has moved:

import xarray as xr import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation %matplotlib widget ani = None # Create the data array for the first time point data_12 = np.zeros((1, 6, 6)) # Creating a 3D array with time dimension (1), and size 6x6 data_12[0, 1, 1] = 6 # Setting the value at 1x1 location to 6 # Create the xarray dataset for the first time point ds_12 = xr.Dataset( { "value": (["time", "x", "y"], data_12) }, coords={ "time": [np.datetime64("2024-01-01T12:00")], "x": np.arange(6), "y": np.arange(6), }, ) # Create the data array for the second time point data_13 = np.zeros((1, 6, 6)) data_13[0, 0, 0] = 0 data_13[0, 4, 4] = 6 ds_13 = xr.Dataset( { "value": (["time", "x", "y"], data_13) }, coords={ "time": [np.datetime64("2024-01-01T13:00")], "x": np.arange(6), "y": np.arange(6), }, ) ds_concat = xr.concat([ds_12, ds_13], dim="time") ds_interp = ds_concat.resample(time="1min").interpolate("linear") fig, ax = plt.subplots() ax.set_xlim(0, 5) ax.set_ylim(0, 5) data_selected = ds_interp.isel(time=1) cont = ax.contourf(data_selected["x"], data_selected["y"],data_selected["value"], cmap='viridis') def animatehere(i): global cont data_selected = ds_interp.isel(time=i) cont = ax.contourf(data_selected["x"], data_selected["y"],data_selected["value"], cmap='viridis') return cont def init(): pass ani = FuncAnimation(fig,animatehere,init_func=init, frames=60,interval=30, repeat=False) ani.save('animation.gif', writer='pillow') plt.show() This creates the following output.


Изображение


I want to know how I can interpolate this data so that it shows the contour moving from point to point rather than it simply disappearing in one location and re-appearing in another. In this example the intensity does not change, it's 6 in one location and 6 in the other but any solution should also be able to handle the intensity changing over time.

Can someone give me an example of how this might work or point me in the right direction?

Вернуться в «Python»