使用 X 射线绘制选定的坐标范围
Plot a selected range of co-ordinates using x-aaray
我正在尝试从 3-D 数据网格绘图。例如:
data10.isel(z=76,x=256).plot(ax=ax1)
给出以下输出:
我只想绘制此图的顶部,以便更容易比较多条曲线。我知道我们可以使用:
ax1.margins(-0.45,0.09)
但每次我都必须 guess/trial 括号中的数字才能放大到正确的位置。有什么方法可以设置要在绘图上显示的数据的坐标范围吗?
谢谢!
编辑:绘制多条曲线时如何添加图例?
Edit2:感谢您的反馈!这是我用来从不同数组绘制的代码:
f, ((ax1)) = plt.subplots(1, 1, figsize=(5,5))
data5.isel(x=127,z=125).plot(xlim=(-25,25))
data10.isel(x=127,z=125).plot(xlim=(-25,25))
data15.isel(x=127,z=125).plot(xlim=(-25,25))
data20.isel(x=127,z=125).plot(xlim=(-25,25))
给出以下输出:
默认情况下不显示图例。
编辑 3:
最后一个问题:我正在尝试将选定的值从数据数组复制到列表中,以特定格式写入文本文件。:
data = []
data = data5.isel(x=127,z=125)
但这也会复制坐标,例如:data[200]
给出:
<xarray.DataArray ()>
array(0.)
Coordinates:
z float64 25.5
y float64 26.7
x float64 0.2
执行此操作的正确方法是什么?
想通了:data[200].data
给出存储在数组中的值。
要限制您正在绘制的轴之一的边距,只需将 xlim
/ylim
传递给 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()
给你
并将 x 上的 Latitude
轴限制为 30
-60
:
xx.plot(xlim=(30,60))
如果绘制多条线,默认添加图例:
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))
编辑:
要绘制来自不同数据集的多条线,您需要使用 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()
要访问数据集的值,您可以使用 DataArray
的 values
属性(注意 air
是我数据集中的变量,并且会 return 底层 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)
我正在尝试从 3-D 数据网格绘图。例如:
data10.isel(z=76,x=256).plot(ax=ax1)
给出以下输出:
我只想绘制此图的顶部,以便更容易比较多条曲线。我知道我们可以使用:
ax1.margins(-0.45,0.09)
但每次我都必须 guess/trial 括号中的数字才能放大到正确的位置。有什么方法可以设置要在绘图上显示的数据的坐标范围吗?
谢谢!
编辑:绘制多条曲线时如何添加图例?
Edit2:感谢您的反馈!这是我用来从不同数组绘制的代码:
f, ((ax1)) = plt.subplots(1, 1, figsize=(5,5))
data5.isel(x=127,z=125).plot(xlim=(-25,25))
data10.isel(x=127,z=125).plot(xlim=(-25,25))
data15.isel(x=127,z=125).plot(xlim=(-25,25))
data20.isel(x=127,z=125).plot(xlim=(-25,25))
给出以下输出:
默认情况下不显示图例。
编辑 3: 最后一个问题:我正在尝试将选定的值从数据数组复制到列表中,以特定格式写入文本文件。:
data = []
data = data5.isel(x=127,z=125)
但这也会复制坐标,例如:data[200]
给出:
<xarray.DataArray ()>
array(0.)
Coordinates:
z float64 25.5
y float64 26.7
x float64 0.2
执行此操作的正确方法是什么?
想通了:data[200].data
给出存储在数组中的值。
要限制您正在绘制的轴之一的边距,只需将 xlim
/ylim
传递给 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()
给你
并将 x 上的 Latitude
轴限制为 30
-60
:
xx.plot(xlim=(30,60))
如果绘制多条线,默认添加图例:
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))
编辑:
要绘制来自不同数据集的多条线,您需要使用 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()
要访问数据集的值,您可以使用 DataArray
的 values
属性(注意 air
是我数据集中的变量,并且会 return 底层 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)