首页 > 解决方案 > 使用 hvplot 从 xarray 数据集中绘制两个数据变量

问题描述

我在 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 重现这个?

标签: pythonpython-xarrayhvplot

解决方案


另一种可能:

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

推荐阅读