Search code examples
pythonarraysnumpymatplotlibpytorch

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.

enter image description here

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:

enter image description here

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?


Solution

  • 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.