小编Lig*_*t_B的帖子

就地更新 xarray 中的属性

我对使用 xarrays 还很陌生。我想就地修改 NetCDF 文件的属性。但是,内置函数提供了另一个新的数据集。

ds = xr.open_dataset(file_)
# ds has "time" as one of the coordinates whose attributes I want to modify
#here is ds for more clarity
ds
>><xarray.Dataset>
Dimensions:  (lat: 361, lev: 1, lon: 720, time: 1)
Coordinates:
* lon      (lon) float32 0.0 0.5 1.0 1.5 2.0 ... 357.5 358.0 358.5 359.0 359.5
* lat      (lat) float32 -90.0 -89.5 -89.0 -88.5 -88.0 ... 88.5 89.0 89.5 90.0
* lev      (lev) float32 1.0
* time     (time) timedelta64[ns] 00:00:00 …
Run Code Online (Sandbox Code Playgroud)

python netcdf python-xarray

8
推荐指数
2
解决办法
9493
查看次数

Jupyter Notebook: (OperationalError('磁盘 I/O 错误',))

我在运行现有 Jupyter Notebook 文件时收到以下错误

The history saving thread hit an unexpected error (OperationalError('disk I/O error',)).History will not be written to the database.
Run Code Online (Sandbox Code Playgroud)

尽管如此,笔记本中的其余代码此后仍可以正常执行。但是,当我尝试创建新的笔记本文件时,此错误不允许我创建它,并且出现以下错误:

Error while saving file: python_notebook_ollie/Untitled2.ipynb disk I/O error
Traceback (most recent call last):
  File "/work/ollie/muali/miniconda3/lib/python3.6/site-packages/notebook/services/contents/filemanager.py", line 421, in save
    self.check_and_sign(nb, path)
  File "/work/ollie/muali/miniconda3/lib/python3.6/site-packages/notebook/services/contents/manager.py", line 440, in check_and_sign
    self.notary.sign(nb)
  File "/work/ollie/muali/miniconda3/lib/python3.6/site-packages/nbformat/sign.py", line 449, in sign
    self.store.store_signature(signature, self.algorithm)
  File "/work/ollie/muali/miniconda3/lib/python3.6/site-packages/nbformat/sign.py", line 207, in store_signature
    if not self.check_signature(digest, algorithm):
  File "/work/ollie/muali/miniconda3/lib/python3.6/site-packages/nbformat/sign.py", line 241, in check_signature
    self.db.commit()
sqlite3.OperationalError: disk I/O error …
Run Code Online (Sandbox Code Playgroud)

python jupyter-notebook

5
推荐指数
1
解决办法
1万
查看次数

修改绘图轴,使其刻度标签的顺序及其各自的点相应改变 - 无需修改数据本身

我想重新排序 x 轴刻度标签,以便数据也相应更改。

例子

y = [5,8,9,10]
x = ['a', 'b', 'c', 'd']
plt.plot(y, x)

Run Code Online (Sandbox Code Playgroud)

原剧情

我希望通过修改轴刻度的位置来使绘图看起来像什么。 预期图像

请注意,我不想通过修改数据的顺序来实现此目的

我的尝试

# attempt 1
fig, ax =plt.subplots()
plt.plot(y,x)
ax.set_xticklabels(['b', 'c', 'a', 'd'])
# this just overwrites the labels, not what we intended

# attempt2
fig, ax =plt.subplots()
plt.plot(y,x)
locs, labels = plt.xticks()
plt.xticks((1,2,0,3)); # This is essentially showing the location 
# of the labels to dsiplay irrespective of the order of the tuple.

Run Code Online (Sandbox Code Playgroud)

编辑: 根据这里的评论,有一些进一步的说明。

假设(a,5)图 1 中的第一点。如果我更改了x-axis定义,现在将 a …

python matplotlib

5
推荐指数
1
解决办法
2146
查看次数

使用 Python-Xarray 重新网格坐标

我有一个 NetCDF 文件,其中的变量存储在 0 到 360 度经度。我想将其转换为 -180 到 180 度。这应该是一项相当简单的任务,但出于某种原因,我似乎无法使教程中给出的一些示例得到解决。

ds = xr.open_dataset(file_)   
>ds
<xarray.Dataset>
Dimensions:  (lev: 1, lon: 720, time: 1460)
Coordinates:
* lon      (lon) float64 0.0 0.5 1.0 1.5 2.0 2.5 ... -2.5 -2.0 -1.5 -1.0 -0.5
* lev      (lev) float32 1.0
* time     (time) datetime64[ns] 2001-01-01 ... 2001-12-31T18:00:00
Data variables:
 V        (time, lev, lon) float32 13.281297 11.417505 ... -19.312767
Run Code Online (Sandbox Code Playgroud)

我尝试使用Dataset.assign_coord的帮助

ds.V.assign_coords(lon=((ds.V.lon + 180) % 360 - 180)) 
#gives me a new array with lon …
Run Code Online (Sandbox Code Playgroud)

python netcdf python-xarray

3
推荐指数
1
解决办法
2463
查看次数

删除 Xarray 子图中的共享颜色条

我正在尝试通过使用 Xarrays 的高级绘图功能制作共享颜色条来微调我的子图。目前我的情节看起来在此处输入图片说明我想制作一个通用的颜色条并修复 yaxis,以便只有第一个子图 y 轴有刻度和标签。我用来绘制它的代码如下所示:

fig, (ax1, ax2) = plt.subplots(ncols=2, sharey=True, figsize=(14,10))
Var1_monthly_clim_N.plot(ax=ax1,vmin=6, vmax=14, cmap='jet');

Var2_monthly_clim_S.plot(ax=ax2, vmin=6, vmax=14, cmap='jet', yticks=[]);
## why is yticks removing ticks from both axis
## Var1 and Var 2 have similiar dimensions
Var1
>><xarray.DataArray 'R_metric' (month: 12, lat: 1, lon: 720)>
array([[[ 9.899495,  9.942897, ...,  9.814826,  9.856807]],

   [[ 9.578215,  9.610594, ...,  9.514423,  9.546191]],

   ...,

   [[11.974784, 12.019565, ..., 11.886591, 11.930419]],

   [[10.237672, 10.285891, ..., 10.142638, 10.189836]]])
Coordinates:
* lon      (lon) float32 -180.0 -179.5 -179.0 -178.5 …
Run Code Online (Sandbox Code Playgroud)

python plot matplotlib python-xarray

3
推荐指数
1
解决办法
3532
查看次数

从 xarray 数据集中的闰年中删除闰日

我在 xarray 数据集中加载了 Netcdf 文件,我想制作没有闰日的日常气候学,即不包含 2 月 29 日。我正在尝试该Dataset.drop方法的语法对我来说不是那么直观。这是数据集

print(ds)
>><xarray.Dataset>
Dimensions:        (lat: 1, lev: 1, lon: 720, time: 27133)
Coordinates:
* lon            (lon) float32 -180.0 -179.5 -179.0 ... 178.5 179.0 179.5
* lev            (lev) float32 1.0
* time           (time) datetime64[ns] 2000-01-02T18:00:00 ... 2018-07-30
Dimensions without coordinates: lat
Data variables:
Var1              (time, lev, lon) float32 ...
Var2              (time, lat, lon) float64 ...
Var3              (time, lat, lon) float64 ...
Run Code Online (Sandbox Code Playgroud)

我试过

ds_N_R.drop(['Var1', 'Var2', 'Var3'], time='2000-02-29')
>>TypeError: drop() got …
Run Code Online (Sandbox Code Playgroud)

python python-xarray

1
推荐指数
1
解决办法
1296
查看次数