使用seaborn_image.imghist创建多组图像-直方图子图报错求解决
如何用seaborn_image创建多组“栅格图像+直方图”并排子图
我需要创建包含三组“栅格图像+对应直方图”的并排子图,发现seaborn_image库的imghist函数可生成单组此类组合图,但尝试将三组并排展示时遇到困难。
尝试方法1:使用seaborn_image的ImageGrid
该库的ImageGrid方法可用于创建子图,但对imghist对象无效,报错如下:
Traceback (most recent call last): File "/home/margot/Documents/code/plot_rasters.py", line 542, in <module> isns.ImageGrid(imghist_col) File "/home/margot/anaconda3/lib/python3.9/site-packages/seaborn_image/_grid.py", line 439, in __init__ self._map_img_to_grid() File "/home/margot/anaconda3/lib/python3.9/site-packages/seaborn_image/_grid.py", line 496, in _map_img_to_grid if _d.ndim > 2: AttributeError: 'Figure' object has no attribute 'ndim' <Figure size 1440x504 with 0 Axes>
尝试方法2:使用SeabornFig2Grid
我还尝试使用SeabornFig2Grid类,代码如下:
fig = plt.figure(figsize=(20,7)) fig.subplots_adjust(top=1) fig.suptitle('Crop rasters', fontsize=18) nrows = 1 ncols = 3 imghist_s1 = isns.imghist(test_S1[test_key], aspect=2.2, cmap=batlow, dx=10, units='m') # pixelsize =10m imghist_ndvi = isns.imghist(test_NDVI[test_key], aspect=2.2, vmin=0.1, vmax=0.95, cmap=batlow, dx=10, units='m') # pixelsize =10m imghist_bp = isns.imghist(test_BP[test_key]/1000, aspect=2.2, vmin=0.1, vmax=0.95, cmap=batlow, dx=10, units='m') # pixelsize =10m gs = gridspec.GridSpec(nrows, ncols) mg0 = SeabornFig2Grid(imghist_s1, fig, gs[0]) mg1 = SeabornFig2Grid(imghist_s1, fig, gs[1]) mg2 = SeabornFig2Grid(imghist_s1, fig, gs[2]) gs.tight_layout(fig) plt.show()
但出现了相同的错误,且现有解决方案不适用于seaborn_image库。
若上述方法不可行,我也考虑过手动组合图像与直方图再制作子图,但尚未找到合适方法。恳请帮助解决报错或提供可行方案。
内容的提问来源于stack exchange,提问作者Anne0102
相关产品推荐
相关产品推荐

