【问题标题】:How do I convert the Xarray plot xr.ufuncs.log(data_slice).plot(cmap='magma', vmin=0, vmax = max_value*.7) into the matplotlib plot?如何将 Xarray 图 xr.ufuncs.log(data_slice).plot(cmap='magma', vmin=0, vmax = max_value*.7) 转换为 matplotlib 图?
【发布时间】:2020-10-09 01:32:03
【问题描述】:

所以基本上我有一个 Xarray 和一个 NumPy 数组来表示频谱图的相同数据。以下代码用于以非常清晰的方式绘制 Xarray(它将频谱图切割到最大值以上)

plt.figure(figsize=(3,5))
data_slice = data['__xarray_dataarray_variable__'].sel(slices=41625.0)
max_value = np.log(data.sel(slices=slice(67.5, 5.999625e+05)).max(xr.ALL_DIMS)['__xarray_dataarray_variable__'].values)
xr.ufuncs.log(data_slice).plot(cmap='magma', vmin=0, vmax = max_value*.7)

这里我们有 Xarray -> 数据,我们选择其中的一个特定部分,然后使用 xf.plot 绘制它。同样,我有一个形状为 (256, 12333) 的 Numpy 数组,其中 12333 代表时间戳的数量,256 代表频率区间。我如何将我的绘图告诉鞋子数据,直到我需要绘制的东西中的最大值?我想这样做,以便获得频谱图的放大图像,以便我可以清楚地看到声音。 截至目前,我一直在绘制我的 numpy 数组 -

plt.imshow(data[:, 30:100])

【问题讨论】:

    标签: python numpy matplotlib plot python-xarray


    【解决方案1】:

    为什么不直接将其转换为 DataArray?

    然后您将利用 xarray 的绘图实用程序。

    特别是,您将能够使用 robust=True kwarg 自动从动态颜色范围中删除潜在的异常值。

    还有自动标注,使用"long_name"属性。

    freq_count, time_count = 256, 12333
    
    data = np.random.randn(freq_count, time_count)
    
    da = xr.DataArray(
        dims=("frequency", "time"),
        data=data,
        coords=dict( 
            frequency=np.arange(freq_count), 
            time=np.arange(time_count), 
        ), 
        attrs=dict(long_name="Spectral intensity over time") 
    )
    

    绘图:

    fig, axes = plt.subplots(ncols=2)
    da.plot.imshow(robust=False, ax=axes[0])
    da.plot.imshow(robust=True, ax=axes[1])
    plt.show()
    

    robust=False 图和robust=True 图之间的比较:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-10-05
      • 1970-01-01
      • 1970-01-01
      • 2018-10-15
      • 1970-01-01
      • 2011-12-20
      • 2014-10-17
      相关资源
      最近更新 更多