What could be causing a visual discrepancy when displaying two identical numpy arrays using matplotlib's imshow()?

huangapple go评论58阅读模式
英文:

What could be causing a visual discrepancy when displaying two identical numpy arrays using matplotlib's imshow()?

问题

I have two arrays, array_questionable and array_comparison, which have the same shape, datatype, and contain the same values. However when displaying the arrays with axes[c, 1].imshow(array_questionable, cmap='gray') the questionable array returns a mostly white image. It should however like the array_comparison create a visually black image.

唯一明显的差异是使用 np.where(array_questionable != array_comparison) 时返回所有条目都不同。

以下是我正在使用的代码段:

output_np = model(input_tensor)[0].detach().numpy()
array_questionable = output_np[0, 0, :, :]
array_comparison = torch.full((100, 100), 0.00217692).numpy()
diff = np.where(array_questionable != array_comparison)

这两个数组具有相同的形状,即 (100, 100)。它们的数据类型都是 float32。此外,这两个数组的大多数值都相同,元素大约为 0.00217692。
这也可以通过查看调试器来确认:

尽管存在这些相似之处,但 np.where() 函数表明所有条目都不同。我想了解为什么会出现这种差异,以及如何正确比较这两个数组以识别实际的差异。

有人能帮忙解释为什么比较这两个数组未产生预期的结果吗?

英文:

I have two arrays, array_questionable and array_comparison, which have the same shape, datatype, and contain the same values. However when displaying the arrays with axes[c, 1].imshow(array_questionable, cmap='gray') the questionable array returns a mostly white image. It should however like the array_comparison create a visually black image.

What could be causing a visual discrepancy when displaying two identical numpy arrays using matplotlib's imshow()?

The only significant difference I could find was by using np.where(array_questionable != array_comparison), which returns that all entries are different.

Here is the code snippet I am using:

output_np = model(input_tensor)[0].detach().numpy()
array_questionable = output_np[0, 0, :, :]
array_comparison = torch.full((100, 100), 0.00217692).numpy()
diff = np.where(array_questionable != array_comparison)

Both arrays have the same shape, which is (100, 100). Both have the datatype float32. Moreover the values of both arrays are mostly the same, with elements around 0.00217692.
This can also be confirmed by looking at the debugger:

What could be causing a visual discrepancy when displaying two identical numpy arrays using matplotlib's imshow()?

Despite these similarities, the np.where() function indicates that all entries are different. I would like to understand why this discrepancy is occurring and how to properly compare these two arrays to identify the actual differences.

Can someone help explain why the comparison between these two arrays is not yielding the expected result?

答案1

得分: 0

如@Joao_PS指出,解决方案是设置vminvmax。所以通过像这样设置vmin=0, vmax=1来解决了这个问题:

axes[c, 1].imshow(array_questionable, cmap='gray', vmin=0, vmax=1)

据推测,Matplotlib将所有内容缩放到一个小区间,而不是在0到1之间,这就是为什么像0.0022这样的小值仍然显示为白色的原因。

英文:

As @Joao_PS pointed out the solution was setting vmin and vmax. So the issue was solved by setting vmin=0, vmax=1 like this:

axes[c, 1].imshow(array_questionable, cmap='gray', vmin=0, vmax=1)

Presumably matplotlib scaled everything to a small interval instead of beeing between 0 and 1 which is why a small value like 0.0022 was still displayed white.

huangapple
  • 本文由 发表于 2023年5月20日 22:38:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76295760.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定