[英]Matplotlib colorbar when LogNorm and ImageGrid is used
I have been trying to plot some panels using ImageGrid
. 我一直在尝试使用
ImageGrid
绘制一些面板。 When I use grid.cbar_axes[0].colorbar(im)
to set the colorbar the colors look fine, but the scale on the colorbar reads like it's going from 2x10^0
to None
. 当我使用
grid.cbar_axes[0].colorbar(im)
设置颜色条时,颜色看起来不错,但是颜色条上的比例看起来像是从2x10^0
变为None
。
I have tried dozens of workarounds but nothing worked. 我尝试了数十种解决方法,但没有任何效果。 Here's the figure I'm trying to make (my wrong version):
这是我要制作的数字(我的版本错误):
Unfortunately I couldn't make a MWE that perfectly reproduces the problem. 不幸的是,我无法制作出能够完美再现问题的MWE。 I did produce a MWE that partially reproduces it.
我确实制作了一部分复制的MWE。 If I use this code:
如果我使用此代码:
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
from matplotlib.colors import LogNorm
def get_demo_image():
import numpy as np
from matplotlib.cbook import get_sample_data
f = get_sample_data("axes_grid/bivariate_normal.npy", asfileobj=False)
z = np.load(f)
return abs(z), (-3, 4, -4, 3)
F = plt.figure(figsize=(5.5, 3.5))
grid = ImageGrid(F, 111, # similar to subplot(111)
nrows_ncols=(1, 3),
axes_pad=0.1,
add_all=True,
label_mode="L",
cbar_mode='single'
)
Z, extent = get_demo_image() # demo image
im1 = Z
im2 = Z
im3 = Z
vmin, vmax = 1e-3, 1e10
for i, im in enumerate([im1, im2, im3]):
ax = grid[i]
imc = ax.pcolormesh(range(15), range(15), im, norm=LogNorm(vmin=vmin, vmax=vmax), linewidth=0, rasterized=True)
cb=grid.cbar_axes[0].colorbar(imc)
I get almost the same behavior, except that the upper limit appears to be fine. 除了上限似乎很好之外,我几乎得到了相同的行为。 The lower limit still presents the same weird behavior, no matter what values I use for
vmin
and vmax
. 不管我对
vmin
和vmax
使用什么值,下限仍然呈现相同的怪异行为。
Any idea of what might be going on? 对可能发生的事情有任何想法吗?
Although the official example uses something similar to cb=grid.cbar_axes[0].colorbar(imc)
, which would translate here into grid[2].cax.colorbar(im)
, I'm lost on why that would even make sense. 尽管官方示例使用了类似于
cb=grid.cbar_axes[0].colorbar(imc)
,它将在此处转换为grid[2].cax.colorbar(im)
,但我仍然迷失了为什么这样甚至有意义。
Instead, the usual way to produce a colorbar would also work here, using the colorbar method of Figure
with the ScalarMappable (here imc
) as argument and the axes to produce the colorbar as keywordargument to cax
(here grid[2].cax
): 取而代之的是,通常使用产生颜色条的方法在这里也可以使用,使用
Figure
的颜色条方法,将ScalarMappable(此处为imc
)作为参数,将轴产生颜色条作为cax的cax
(此处为grid[2].cax
):
cb=F.colorbar(imc, cax=grid[2].cax)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.