【问题标题】:Xarray plot.line with non-dimensional coordinate具有无量纲坐标的 Xarray plot.line
【发布时间】:2018-09-06 02:11:57
【问题描述】:

我希望使用无量纲坐标而不是量纲坐标绘制一系列线图。

在 pcolormesh 中是可能的:

import xarray as xr
import numpy as np
import matplotlib.pyplot as plt

# Setup data array, from http://xarray.pydata.org/en/stable/plotting.html#multidimensional-coordinates
lon, lat = np.meshgrid(np.linspace(-20, 20, 5), 
                       np.linspace(0, 30, 4))
lon += lat/10
lat += lon/10
da = xr.DataArray(np.arange(20).reshape(4, 5), dims=['y','x'],
                     coords = {'lat': (('y', 'x'), lat),
                               'lon': (('y', 'x'), lon)})

# plot in terms of y,x, the dimensional coordinate
fig,ax = plt.subplots()
da.plot.pcolormesh('y','x') 

# plot in terms of lon, lat, the non-dimensional coordinate
fig, ax = plt.subplots()
da.plot.pcolormesh('lon', 'lat')

# plot lines in terms of x,y, the dimensional coordinate
fig, ax = plt.subplots()
da.plot.line(x='x')

# plot lines in terms of lon, lat?
da.plot.line(x='lon') #gives error

plot in terms of y,x, the dimensional coordinate

plot in terms of lon, lat, the non-dimensional coordinate

plot lines in terms of x,y, the dimensional coordinate

基本上,我想绘制穿过 2D 数据的线。

我可以使用基本的 matplotlib(如下)来做到这一点,但我想要 xarray 中的 1 行。

 fig,ax = plt.subplots()
 for i in range(int(da.y.shape[0])):
     ax.plot(da.lon[i], da[i])

What I want

【问题讨论】:

    标签: python python-xarray


    【解决方案1】:

    我不确定为什么 DataArray.plot.line 不允许无维度坐标。您应该将其提交为issue

    对于单行解决方案,您可以省略 for 循环:

    ax.plot(da.lon.T, da.data.T)
    

    【讨论】:

      猜你喜欢
      • 2021-12-12
      • 2021-02-16
      • 2018-01-10
      • 1970-01-01
      • 2019-03-04
      • 2020-04-27
      • 2016-08-12
      • 2018-01-11
      • 2019-01-17
      相关资源
      最近更新 更多