简体   繁体   English

将 xarray.Dataset 的字典条目转换为 numpy 数组(用于 xy 坐标等高线绘图)

[英]Convert dictionary entry of xarray.Dataset to numpy array (for xy coord contour plotting)

I want to plot, a dictionary entry, which contains lat,on coordinates on a xy coordinate ( lat-lon).我想要 plot,一个字典条目,其中包含 xy 坐标(经纬度)上的纬度坐标。 The variable looks like:该变量如下所示:

 <class 'method'>
 <bound method Mapping.values of <xarray.Dataset>
 Dimensions:  (lat: 96, lon: 144)
 Coordinates:
 lev_p    int32 200
 Dimensions without coordinates: lat, on
 Data variables:
 V        (lat, lon) float32 dask.array<chunksize=(96, 144), meta=np.ndarray>>

I want to plot V, as contour against lat and lon but when I try to plot it I get the following error:我想 plot V,作为纬度和经度的轮廓,但是当我尝试 plot 时,我收到以下错误:

TypeError: cannot directly convert an xarray.Dataset into a numpy array. Instead, create an xarray.DataArray first, either with indexing on the Dataset or by invoking the TypeError: cannot directly convert an xarray.Dataset into a numpy array. Instead, create an xarray.DataArray first, either with indexing on the Dataset or by invoking the to_array() method. TypeError: cannot directly convert an xarray.Dataset into a numpy array. Instead, create an xarray.DataArray first, either with indexing on the Dataset or by invoking the to_array() method.

I tried to convert the dictionary values to an array with.to_array as well as using map(float,variable) but none worked.我尝试将字典值转换为带有.to_array 的数组以及使用 map(float,variable) 但没有奏效。 I can't figure out what is going on.我不知道发生了什么。 Any advice or help with this?对此有何建议或帮助? Thanks谢谢

It was actually an easy one - it was enough to extract the dictionary value and then our of that extract the variable so instead of doing something like ds['Experiment'] and get the xarray.Dataset I would do ds['Experiment']['V'] so I would get the dictionary and out of it the particular V value.这实际上是一个简单的 - 提取字典值然后我们的提取变量就足够了,所以我会做 ds['Experiment'] 之类的事情并获取 xarray.Dataset 我会做 ds['Experiment'] ['V'] 所以我会得到字典并从中取出特定的 V 值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM