问题描述
我正在尝试绘制包含地理气候数据的下载的 .nc 文件。
在最后一步中,我收到以下行的语法错误:plt.gcf().set_size_inches(20,10)
。我似乎无法找到错误。
import numpy as np
import xarray # used for reading the data.
import matplotlib.pyplot as plt # used to plot the data.
import ipywidgets as widgets # For ease in selecting variables.
import cartopy.crs as ccrs # Used to georeference data.
filelist_arr = [save_dir + os.path.basename(file) for file in filelist]
selected_file = widgets.Dropdown(options=filelist_arr,description='data file')
display(selected_file)
# Now to load in the data to xarray
ds = xarray.open_dataset(selected_file.value)
# Helper methods# Define function to get standard dimensions
def get_time(dataset):
for _,cur_coord in dataset.coords.items:
if cur_coord.attrs['standard_name'] == 'time':
return cur_coord
def get_lat(dataset):
for _,cur_coord in dataset.coords.items:
if cur_coord.attrs['standard_name'] == 'longitude':
return cur_coord
def get_lon(dataset):
for _,cur_coord in dataset.coords.items:
if cur_coord.attrs['standard_name'] == 'latitude':
return cur_coord
def get_primary(dataset):
primary_variables = {}
coords = dataset.coords.keys()
highest_dims = 0
for cur_key,cur_var in dataset.variables.items():
if cur_key not in coords:
primary_variables[cur_key] = cur_var
return primary_variables
var = widgets.Dropdown(
options=get_primary(ds).keys(),description='Variable')
display(var)
到目前为止一切顺利。现在,在最后一个块中,我在第 3 行收到一个语法错误。
var = widgets.Dropdown(
proj = ccrs.Mercator()
plt.gcf().set_size_inches(20,10)
ax = plt.axes(projection=proj)
data_slice = ds[var.value].isel(time=10)
data_slice.plot.contourf(ax=ax,transform=ccrs.PlateCarree())
ax.set_global()
ax.coastlines()
这是错误:
File "<ipython-input-80-8848cc5cc689>",line 3
plt.gcf().set_size_inches(20,10)
^
SyntaxError: invalid Syntax
谁能解释一下我做错了什么?
解决方法
我认为您在尝试将代码包装在 widgets.Dropdown()
代码中时有点困惑。
也许先编写没有小部件的代码是个好主意,如果您的代码有效,然后再添加它们。你可以试试下面的代码,看看会出现哪些错误:
import numpy as np
import xarray
import matplotlib.pyplot as plt
import ipywidgets as widgets
import cartopy.crs as ccrs
# Load data into memory
filelist_arr = [save_dir + os.path.basename(file) for file in filelist]
selected_file = widgets.Dropdown(options=filelist_arr,description='data file')
# Create xarray
ds = xarray.open_dataset(selected_file.value)
# Helper methods
def get_time(dataset):
for _,cur_coord in dataset.coords.items:
if cur_coord.attrs['standard_name'] == 'time':
return cur_coord
def get_lat(dataset):
for _,cur_coord in dataset.coords.items:
if cur_coord.attrs['standard_name'] == 'longitude':
return cur_coord
def get_lon(dataset):
for _,cur_coord in dataset.coords.items:
if cur_coord.attrs['standard_name'] == 'latitude':
return cur_coord
def get_primary(dataset):
primary_variables = {}
coords = dataset.coords.keys()
highest_dims = 0
for cur_key,cur_var in dataset.variables.items():
if cur_key not in coords:
primary_variables[cur_key] = cur_var
return primary_variables
# Ask user to select dataset
var = widgets.Dropdown(
options=get_primary(ds).keys(),description='Variable')
display(var)
# Initialize new figure and specify figure size
plt.figure(num=None,figsize=(20,10))
# Create Mercator projection with dateline in the middle
ax = plt.axes(projection=ccrs.Mercator(central_longitude=180))
# Draw coastlines
ax.coastlines()
# Select the appropriate data and plot to the axes
data_slice = ds[var.value].isel(time=10)
data_slice.plot.contourf(ax=ax,transform=ccrs.PlateCarree())
# Optional: set the map extent,for geographical coordinates
# ax.set_extent([90,270,-40,40],crs=ccrs.PlateCarree())
# Optional: add title
# plt.title('Geographical climate data at time = 10')
# Show the plot
plt.show()
如果仍然出现错误,请在下面的评论中告诉我。