Trying to plot one variable, for all existing coordinates into a single line plot. Let's assume a simple example set of data:
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."))
Target plot (sans the title):
I can do this with the following code:
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)
I would expect to be able to do this with a syntax of
ds0.temperature.plot()
But this gives me a histogram plot.
I can assign hue or call subplots in columns
ds0.temperature.plot(hue='c1',col='c2')
But I would like to simply have all data in a single plot. Random colors or color coded according to a coordinate would be a welcome addition.
How can I get the target plot without calling all coordinates explicitly?
CodePudding user response:
This isn't awesome, but it does create lines along multiple dimensions in one command. Or you can loop through the dimensions and style it yourself.
CodePudding user response:
ds0.temperature.stack(stacked=("c1", "c2")).plot.line(x='time')




