Я пытаюсь построить временной ряд. Но у меня есть ошибка ValueError: cannot reshape array of size 12 into shape (34,12)
Я собираюсь изменить изображение. Как мне это исправить?
import numpy as np
import matplotlib.pyplot as plt
from netCDF4 import Dataset
from mpl_toolkits.basemap import Basemap
print("Reading in the data from the netCDF file")
filename = "C:/Users/zayam/Downloads/python_lesson/LAI_mean_monthly_1981-2015.nc4"
data = Dataset(filename)
print("Setting the variable from the data")
lai = data.variables['LAI'][:]
ntimes, nlat, nlon = np.shape(lai)
print("The temp array has dimensions: \n"
"Time (number of years x 12 months): {}\n"
"Latitudes: {}\n"
"Longitudes: {}".format(ntimes, nlat, nlon)
)
print("Calculating the average across all years")
lai_av_1981_2015 = np.mean(lai[:,:,:], axis = 0)
print("Plotting a global map")
plt.figure()
map = Basemap(projection="cyl", resolution='c', llcrnrlat=-90, urcrnrlat=90, llcrnrlon=-180, urcrnrlon=180)
map.drawcoastlines(color="black")
lons,lats = np.meshgrid(data.variables['lon'][:], data.variables['lat'][:])
x,y = map(lons, lats)
lai_plot = map.contourf(x, y, lai_av_1981_2015, cmap=plt.cm.viridis)
cb = map.colorbar(lai_plot, "bottom", size="5%", pad="2%", extend = 'both')
cb.set_label("LAI")
plt.title("Mean LAI (1981-2015)")
plt.show()
print("Calculating the global average")
global_average = np.mean(lai[:,:,:], axis=(1,2))
print("Calculating the annual average")
annual_lai = np.mean(np.reshape(global_average, (34,12)), axis = 1)
print("Calculate the 1981-1990 average")
av_1981_1990 = np.mean(annual_lai[80:89])
print("Calculate the lai anomaly compared to the 1981-1990 average")
lai_anomaly = annual_lai - av_1981_1990
# Plot 2 - Timeseries
print("Plotting a time series graph")
plt.figure()
plt.plot(np.arange(1981,2015), lai_anomaly)
plt.ylim(np.floor(min(lai_anomaly)), np.ceil(max(lai_anomaly)))
plt.title("Global Average LAI Anomaly (1981-2015)")
plt.xlabel("Years") # label for the x axis
plt.ylabel("Difference from 1981-1990 average")
plt.show()
Трассировка ошибок:
Traceback (most recent call last):
File "<ipython-input-99-e2d53fc7b62c>", line 1, in <module>
runfile('C:/Users/zayam/OneDrive/Documents/python_lesson/plot_cru-ts_examples.py', wdir='C:/Users/zayam/OneDrive/Documents/python_lesson')
File "C:\Users\zayam\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
execfile(filename, namespace)
File "C:\Users\zayam\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/zayam/OneDrive/Documents/python_lesson/plot_cru-ts_examples.py", line 32, in <module>
annual_lai = np.mean(np.reshape(global_average, (34,12)), axis = 1)
File "C:\Users\zayam\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py", line 292, in reshape
return _wrapfunc(a, 'reshape', newshape, order=order)
File "C:\Users\zayam\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py", line 56, in _wrapfunc
return getattr(obj, method)(*args, **kwds)
File "C:\Users\zayam\Anaconda3\lib\site-packages\numpy\ma\core.py", line 4591, in reshape
result = self._data.reshape(*s, **kwargs).view(type(self))
ValueError: cannot reshape array of size 12 into shape (34,12)