Xarray:在单个图中跨坐标绘制所有变量数据
Xarray: Plot all variable data across coordinates in a single plot
正在尝试绘制一个变量,将所有现有坐标绘制成单线图。让我们假设一组简单的数据示例:
import numpy as np
import pandas as pd
import xarray as xr
import matplotlib.pyplot as plt
np.random.seed(0)
temperature = 15 + 8 * np.random.randn(2, 2, 20)
precipitation = 10 * np.random.rand(2, 2, 20)
coord1 = [0, 1]
coord2 = [2, 3]
time = pd.date_range("2014-09-06", periods=20)
reference_time = pd.Timestamp("2014-09-05")
ds0 = xr.Dataset(
data_vars=dict(temperature=(["c1", "c2", "time"], temperature),
precipitation=(["c1", "c2", "time"], precipitation)),
coords=dict(c1=("c1", coord1),
c2=("c2", coord2),
time=time,
reference_time=reference_time),
attrs=dict(description="Weather related data."))
目标情节(无标题):
我可以用下面的代码来做到这一点:
fig, ax = plt.subplots()
ds0.temperature.sel(c1=0,c2=2).plot(ax=ax)
ds0.temperature.sel(c1=0,c2=3).plot(ax=ax)
ds0.temperature.sel(c1=1,c2=2).plot(ax=ax)
ds0.temperature.sel(c1=1,c2=3).plot(ax=ax)
我希望能够使用
的语法来做到这一点
ds0.temperature.plot()
但这给了我一个直方图。
我可以在列中分配色调或调用子图
ds0.temperature.plot(hue='c1',col='c2')
但我只想将所有数据放在一个图中。随机颜色或根据坐标编码的颜色将是一个受欢迎的补充。
如何在不显式调用所有坐标的情况下获得目标图?
请参阅 xr.plot.line
的 API 文档。您可以将 hue 和 x args 传递给 da.plot.line()
,但您不能提供色调列表,例如,基于遍历多个维度来确定线条颜色。
一种解决方法是堆叠您想要绘制所有线条的维度:
In [2]: da = xr.DataArray(
...: np.random.random(size=(2, 3, 5)) * np.arange(6).reshape(2, 3, 1),
...: dims=["x", "y", "z"],
...: coords=[range(2), range(3), range(5)],
...: )
In [3]: da.stack(stacked=("x", "y")).plot.line(x="z")
这不是很棒,但它确实可以在一个命令中沿多个维度创建线条。或者您可以遍历维度并自己设计样式。
ds0.temperature.stack(stacked=("c1", "c2")).plot.line(x='time')
正在尝试绘制一个变量,将所有现有坐标绘制成单线图。让我们假设一组简单的数据示例:
import numpy as np
import pandas as pd
import xarray as xr
import matplotlib.pyplot as plt
np.random.seed(0)
temperature = 15 + 8 * np.random.randn(2, 2, 20)
precipitation = 10 * np.random.rand(2, 2, 20)
coord1 = [0, 1]
coord2 = [2, 3]
time = pd.date_range("2014-09-06", periods=20)
reference_time = pd.Timestamp("2014-09-05")
ds0 = xr.Dataset(
data_vars=dict(temperature=(["c1", "c2", "time"], temperature),
precipitation=(["c1", "c2", "time"], precipitation)),
coords=dict(c1=("c1", coord1),
c2=("c2", coord2),
time=time,
reference_time=reference_time),
attrs=dict(description="Weather related data."))
目标情节(无标题):
我可以用下面的代码来做到这一点:
fig, ax = plt.subplots()
ds0.temperature.sel(c1=0,c2=2).plot(ax=ax)
ds0.temperature.sel(c1=0,c2=3).plot(ax=ax)
ds0.temperature.sel(c1=1,c2=2).plot(ax=ax)
ds0.temperature.sel(c1=1,c2=3).plot(ax=ax)
我希望能够使用
的语法来做到这一点ds0.temperature.plot()
但这给了我一个直方图。
我可以在列中分配色调或调用子图
ds0.temperature.plot(hue='c1',col='c2')
但我只想将所有数据放在一个图中。随机颜色或根据坐标编码的颜色将是一个受欢迎的补充。
如何在不显式调用所有坐标的情况下获得目标图?
请参阅 xr.plot.line
的 API 文档。您可以将 hue 和 x args 传递给 da.plot.line()
,但您不能提供色调列表,例如,基于遍历多个维度来确定线条颜色。
一种解决方法是堆叠您想要绘制所有线条的维度:
In [2]: da = xr.DataArray(
...: np.random.random(size=(2, 3, 5)) * np.arange(6).reshape(2, 3, 1),
...: dims=["x", "y", "z"],
...: coords=[range(2), range(3), range(5)],
...: )
In [3]: da.stack(stacked=("x", "y")).plot.line(x="z")
这不是很棒,但它确实可以在一个命令中沿多个维度创建线条。或者您可以遍历维度并自己设计样式。
ds0.temperature.stack(stacked=("c1", "c2")).plot.line(x='time')