Динамическая визуализация глобальных графиков

Я построил 17 глобальных графиков, которые показывают десятилетние средние максимумы приземного озона с 1850 по 2015 год. Вместо того, чтобы рисовать их по отдельности, я хочу создать анимацию, которая циклически проходит через них (почти как гифка), то есть имеет те же береговые линии, оси и цветовую полосу повсюду, но изменяет то, что отображается как контур.

Любая помощь в том, как адаптировать мой код для этого, будет принята с благодарностью - заранее спасибо!!

import numpy as np
import netCDF4 as n4
import matplotlib.pyplot as plt
from matplotlib import colorbar, colors
import matplotlib.cm as cm

import cartopy as cart
import cartopy.crs as ccrs
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
import cartopy.feature as cfeature

nc = n4.Dataset('datafile.nc','r')

# daily maximum O3 VMR (units: mol mol-1)
sfo3max = nc.variables['sfo3max']
lon = nc.variables['lon'] # longitude
lat = nc.variables['lat'] # latitude

# (I manipulate the data to produce 17 arrays containing the decadal average O3 VMR which are
#  listed below in sfo3max_avg)

sfo3max_avg = [sfo3max_1850_1860_avg, sfo3max_1860_1870_avg, sfo3max_1870_1880_avg,
               sfo3max_1880_1890_avg, sfo3max_1890_1900_avg, sfo3max_1900_1910_avg,
               sfo3max_1910_1920_avg, sfo3max_1920_1930_avg, sfo3max_1930_1940_avg,
               sfo3max_1940_1950_avg, sfo3max_1950_1960_avg, sfo3max_1960_1970_avg,
               sfo3max_1970_1980_avg, sfo3max_1980_1990_avg, sfo3max_1990_2000_avg,
               sfo3max_2000_2010_avg, sfo3max_2010_2015_avg]

# find overall min & max values for colour bar in plots
min_sfo3max_avg = np.array([])
for i in sfo3max_avg:
    sfo3max_avg_min = np.amin(i)
    min_sfo3max_avg = np.append(min_sfo3max_avg, sfo3max_avg_min)
overall_min_sfo3max_avg = np.amin(min_sfo3max_avg)

max_sfo3max_avg = np.array([])
for i in sfo3max_avg:
    sfo3max_avg_max = np.amax(i)
    max_sfo3max_avg = np.append(max_sfo3max_avg, sfo3max_avg_max)
overall_max_sfo3max_avg = np.amax(max_sfo3max_avg)

# finally plot the 17 global plots of sfo3max_avg
for k in sfo3max_avg:
    fig = plt.figure()
    ax = plt.axes(projection=ccrs.PlateCarree())
    ax.coastlines() # Adding coastlines
    cs = ax.contourf(lon[:], lat[:], k[:], cmap='magma')
    ax.set_title('Decadal Average of Maximum O3 Volume Mixing Ratio')

    m = plt.cm.ScalarMappable(cmap=cm.magma)
    m.set_array(i[:])
    m.set_clim(overall_min_sfo3max_avg, overall_max_sfo3max_avg)

    # Additional necessary information
    cbar = plt.colorbar(m, boundaries=np.arange(overall_min_sfo3max_avg, overall_max_sfo3max_avg
                        + 0.5e-08, 0.5e-08))
    cbar.set_label('mol mol-1')

    # Adding axis labels - latitude & longitude
    gridl = ax.gridlines(color="black", linestyle="dotted", draw_labels=True) 
    gridl.xformatter=LONGITUDE_FORMATTER
    gridl.yformatter=LATITUDE_FORMATTER
    gridl.xlabels_top = False
    gridl.ylabels_right = False

    fig.set_size_inches(w=20,h=10)
    plt.show() # show global plot

1 ответ

Решение

Некоторые элементы в вашем графике могут быть исключены из цикла, потому что их нужно настроить только один раз. После настройки элементов сюжета вы можете обновить сюжет и анимировать, просматривая список в цикле. Этого можно достичь, используя интерактивный режим matplotlib, как показано в приведенном ниже коде:

import numpy as np
import netCDF4 as n4
import matplotlib
matplotlib.use("nbagg")

import matplotlib.pyplot as plt
from matplotlib import colorbar, colors
import matplotlib.cm as cm

import cartopy as cart
import cartopy.crs as ccrs
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
import cartopy.feature as cfeature

nc = n4.Dataset('datafile.nc','r')

# daily maximum O3 VMR (units: mol mol-1)
sfo3max = nc.variables['sfo3max']
lon = nc.variables['lon'] # longitude
lat = nc.variables['lat'] # latitude

# (I manipulate the data to produce 17 arrays containing the decadal average O3 VMR which are
#  listed below in sfo3max_avg)

sfo3max_avg = [sfo3max_1850_1860_avg, sfo3max_1860_1870_avg, sfo3max_1870_1880_avg,
               sfo3max_1880_1890_avg, sfo3max_1890_1900_avg, sfo3max_1900_1910_avg,
               sfo3max_1910_1920_avg, sfo3max_1920_1930_avg, sfo3max_1930_1940_avg,
               sfo3max_1940_1950_avg, sfo3max_1950_1960_avg, sfo3max_1960_1970_avg,
               sfo3max_1970_1980_avg, sfo3max_1980_1990_avg, sfo3max_1990_2000_avg,
               sfo3max_2000_2010_avg, sfo3max_2010_2015_avg]

# find overall min & max values for colour bar in plots
min_sfo3max_avg = np.array([])
for i in sfo3max_avg:
    sfo3max_avg_min = np.amin(i)
    min_sfo3max_avg = np.append(min_sfo3max_avg, sfo3max_avg_min)
overall_min_sfo3max_avg = np.amin(min_sfo3max_avg)

max_sfo3max_avg = np.array([])
for i in sfo3max_avg:
    sfo3max_avg_max = np.amax(i)
    max_sfo3max_avg = np.append(max_sfo3max_avg, sfo3max_avg_max)
overall_max_sfo3max_avg = np.amax(max_sfo3max_avg)

#setup the plot elements
fig = plt.figure()
fig.set_size_inches(w=20,h=10)
ax = plt.axes(projection=ccrs.PlateCarree())
ax.coastlines() # Adding coastlines
ax.set_title('Decadal Average of Maximum O3 Volume Mixing Ratio')

m = plt.cm.ScalarMappable(cmap=cm.magma)
m.set_array(i[:])
m.set_clim(overall_min_sfo3max_avg, overall_max_sfo3max_avg)


# Additional necessary information
cbar = plt.colorbar(m, boundaries=np.arange(overall_min_sfo3max_avg, overall_max_sfo3max_avg
                    + 0.5e-08, 0.5e-08))
cbar.set_label('mol mol-1')

# plot here only the 1st item in your sfo3max_avg list.
cs = ax.contourf(lon[:], lat[:], sfo3max_avg[0][:], cmap='magma')

# Adding axis labels - latitude & longitude
gridl = ax.gridlines(color="black", linestyle="dotted", draw_labels=True) 
gridl.xformatter=LONGITUDE_FORMATTER
gridl.yformatter=LATITUDE_FORMATTER
gridl.xlabels_top = False
gridl.ylabels_right = False


plt.ion()   # set interactive mode
plt.show()

# finally plot the 17 global plots of sfo3max_avg
for k in sfo3max_avg:
    cs = ax.contourf(lon[:], lat[:], k[:], cmap='magma')
    plt.gcf().canvas.draw()
    plt.pause(1) #control the interval between successive displays, currently set to 1 sec.
Другие вопросы по тегам