Simple Example
- dataarrays 最簡單的畫圖方法就是呼叫
dataarray.plot() - xarray 可以通過使用坐標名稱或者資料名稱進行資料索引,如
attrs.long_name, attrs.standard_name, DataArray.name and attrs.units,而這些名稱可以通過dataarray.attrs命令獲得,示例如下:
sst.attrs
{'long_name': 'Monthly Mean of Sea Surface Temperature',
'unpacked_valid_range': array([-5., 40.], dtype=float32),
'actual_range': array([-1.7999996, 35.56862 ], dtype=float32),
'units': 'degC',
'precision': 2,
'var_desc': 'Sea Surface Temperature',
'dataset': 'NOAA Optimum Interpolation (OI) SST V2',
'level_desc': 'Surface',
'statistic': 'Mean',
'parent_stat': 'Weekly Mean',
'standard_name': 'sea_surface_temperature',
'cell_methods': 'time: mean (monthly from weekly values interpolated to daily)',
'valid_range': array([-500, 4000], dtype=int16)}
呼叫dataarray.plot()方法進行快速繪圖:
sst1 =sst.isel(lat=10, lon=10)
sst1.plot()

除此之外,還可以繪制一些其他的繪圖功能,例如,通過xarray.plot.line()命令呼叫matplotlib.pyplot.plot功能,分別傳遞索引和陣列中的x,y值,例如,用藍線繪制三角形matplotlib格式字串可以使用:
sst1[:200].plot.line("b-^")

注意!:不是所有的xarray 繪圖都支持傳遞位置的引數,但是都支持關鍵字功能,關鍵字引數呼叫使用相同的方式,且更明確,如所示:
sst1[:200].plot.line(color="purple", marker="o")

Adding to Existing Axis
添加繪圖到一個現有的坐標軸,并作為一個關鍵字:ax,這對所有的xaray繪圖方法都適用,在下面這個例子中axes是一個由plt.subplots陣列組成的左軸和右軸,
fig, axes = plt.subplots(ncols=2)
axes
air1d.plot(ax=axes[0])
air1d.plot.hist(ax=axes[1])
plt.tight_layout()
plt.draw()

右邊是一個直方圖由xarray.plot.hist ()繪制,
Controlling the figure size:控制圖的大小
通過傳遞figsieze來控制圖片的大小,為了方便起見,xarray的繪圖方法還支持aspect和size控制生成影像的大小通過公式figsize =(aspect*size,size)
sst1.plot(aspect=2, size=3)
plt.tight_layout()

Determine x-axis values
每個默認維度坐標用于 x 軸(這里是時間坐標),但是,您也可以沿 x 軸使用非維度坐標、多索引級別和沒有坐標的維度,為了說明這一點,讓我們從時間中計算一個“小數日”(epoch) ,并將其分配為一個非維度坐標:
每個默認維度坐標用于 x 軸(這里意思就是說將時間坐標作為x坐標軸),但是,也可以沿 x 軸使用非維度坐標、多索引級別和沒有坐標的維度,為了說明這一點,讓我們從時間中計算一個“小數日”(epoch) ,并將其分配為一個非維度坐標:
decimal_day = (sst1.time - sst1.time[0]) / pd.Timedelta("1d")
sst1_multi = sst1.assign_coords(decimal_day=("time", decimal_day.data))
sst1_multi

使用 ‘decimal_day’ 作為 x 坐標,必須明確指定:
sst1_multi.plot(x="decimal_day")

從“ time”和“ decimal _ day”創建一個名為“ date”的新 MultiIndex,也可以使用 MultiIndex 級別作為 x 軸:
sst1_multi = sst1_multi.set_index(date=("time", "decimal_day"))
sst1_multi.plot(x="decimal_day")
最后,如果資料集沒有任何坐標,它將列舉所有資料點:
sst1_multi = sst1_multi.drop("date")
sst1_multi.plot()

同樣的情況也適用于下面的二維圖,
Multiple lines showing variation along a dimension :多條線顯示一個維度上的變化
通過使用適當的引數呼叫 xary.plot.line () ,可以對二維資料繪制線圖,考慮上面定義的三維變數,我們可以用直線圖來檢查經線上三個不同緯度地區的氣溫變化:
sst.isel(lon=10, lat=[19, 21, 22]).plot.line(x="time")

它需要明確的指定:
- 1 x:用于x軸的維度
- 2 hue(色調):要用多條線表示的維度
因此,我們可以通過指定hue = lat而不是x = ‘time來繪制前面的圖,如果需要,可以使用add _ legend = False關閉自動圖例,或者,可以直接傳遞給xary.plot.line ():sst.isel (lon = 10,lat = [19,21,22]).plot.line (hue ='lat'),

Dimension along y-axis :沿著y軸的維度
還可以繪制線圖,使資料在 x 軸上,維度在 y 軸上,這可以通過指定適當的 y 關鍵字引數來實作,
sst.isel(time=10, lon=[10, 11]).plot(y="lat", hue="lon")

Step plots :步驟圖
作為替代方案,也可以使用1維資料繪制類似 matplotlib 的 plt.step,
sst1[:20].plot.step(where="mid")

引數 where 定義了步驟應該放在哪里,選項是“ pre”(默認值)、“ post”和“ mid”,當繪制與 Dataset.groupby _ bin ()分組的資料時,這尤其方便,
sst_grp = sst.mean(["time", "lon"]).groupby_bins("lat", [0, 23.5, 66.5, 90])
sst_mean = sst_grp.mean()
sst_std = sst_grp.std()
sst_mean.plot.step()
(sst_mean + sst_std).plot.step(ls=":")
(sst_mean - sst_std).plot.step(ls=":")
plt.ylim(-20, 30)
plt.title("Zonal mean temperature")

Other axes kwargs :其他關鍵字
關鍵字 arguments xincrease 和 yincrease 可以控制軸的方向,
sst.isel(time=10, lon=[10, 11]).plot.line( y="lat", hue="lon", xincrease=False, yincrease=False)

此外,可以使用 xscale、 yscale 來設定軸伸縮性; xticks、 yticks 來設定軸刻度,xlim、 ylim 來設定軸限制,它們接受與 matplotlib 方法 Axes.set _ (x,y) scale ()、 Axes.set _ (x,y) ticks ()、 Axes.set _ (x,y) lim ()相同的值,
原文參考鏈接:https://xarray.pydata.org/en/stable/user-guide/plotting.html#plotting-faceting
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/430248.html
標籤:AI
