To limit the margins of one of the axis you're plotting, just pass xlim
/ylim
to plot
:
import xarray as xr
ds = xr.tutorial.open_dataset("air_temperature")
xx = ds.sel(lon=200, time=ds.time.values[0], method="nearest").air
xx.plot()
gives you
and to limit the Latitude
axis on x to 30
-60
:
xx.plot(xlim=(30,60))
A legend is added by default if you plot multiple lines:
xx = ds.sel(lon=[200,207,220], time=ds.time.values[0], method="nearest").air
# note the slight change in syntax for multiple lines
xx.plot.line(x="lat", xlim=(30,50))
Edit:
To plot multiple lines from different datasets, you need to use matplotlib
:
xx = ds.sel(lon=200, time=ds.time.values[0], method="nearest").air
import matplotlib.pyplot as plt
xlim = (30,60)
plt.figure()
xx.air.plot(xlim=xlim, label="Air")
(xx.air*1.5).plot(xlim=xlim, label="Air*1.5")
(xx.air*2).plot(xlim=xlim, label="Air*2")
# adds legend
plt.legend()
And to access values of your dataset, you can use the values
property of your DataArray
(note air
is the variable in my dataset and will return the underlying DataArray
):
print(xx.air.values[10:20])
#array([277.29 , 278.4 , 280. , 282.79 , 284.6 , 286.5 ,
# 287.9 , 290.19998, 293.1 , 293.79 ], dtype=float32)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…