【问题标题】:Plotting two data variables from an xarray dataset using hvplot使用 hvplot 从 xarray 数据集中绘制两个数据变量
【发布时间】:2021-05-17 05:00:19
【问题描述】:

我在 python 中有一个 xarray 数据集。我想使用 hvplot 库绘制两个相关(数据)变量。这是一个简单的示例数据集:

import numpy as np
import xarray as xr
import hvplot.xarray

# Create the dataset
time = np.linspace(0,10)
x = time*2 + 1
y = time*3 - 1

ds = xr.Dataset()
ds.coords['time'] = time
ds['x'] = (['time'],x)
ds['y'] = (['time'],y)

# Output
<xarray.Dataset>
Dimensions:  (time: 50)
Coordinates:
  * time     (time) float64 0.0 0.2041 0.4082 0.6122 ... 9.388 9.592 9.796 10.0
Data variables:
    x        (time) float64 1.0 1.408 1.816 2.224 ... 19.78 20.18 20.59 21.0
    y        (time) float64 -1.0 -0.3878 0.2245 0.8367 ... 27.78 28.39 29.0

很容易绘制 x 或 y 与时间的关系

ds.x.hvplot()

但是我想要的是绘制 x 对 y。我原以为这会起作用:

ds.hvplot(x='x',y='y')

但这一次只绘制一个点,并带有一个用于“时间”坐标的滑块。 xarray 有一个 plot 函数,它使用 matplotlib 按预期绘制。

ds.plot.scatter(x='x',y='y')

有没有办法用 hvplot 重现这个?

【问题讨论】:

    标签: python python-xarray hvplot


    【解决方案1】:

    另一种可能性:

    ds.y.assign_coords(x=ds.x).hvplot.scatter(x="x", y="y")
    

    【讨论】:

      【解决方案2】:

      对xarray不太了解,但以下两种方法都可以,但老实说,我希望其他人能提供更好的解决方案:

      ds.reset_index(dims_or_levels='time').hvplot.scatter(x='x', y='y')
      

      或者:

      ds.hvplot.scatter(x='x', y='y', color='blue').overlay()
      

      【讨论】:

        猜你喜欢
        • 2020-12-06
        • 2018-12-11
        • 2021-06-06
        • 2018-01-10
        • 1970-01-01
        • 2020-10-08
        • 1970-01-01
        • 2019-02-26
        • 2020-10-11
        相关资源
        最近更新 更多