14

I displayed an array with matshow and it works fine but now I want to try imshow. The issue is that the quality of imshow is really poor compared to matshow.

How can I fix this ?

Matshow:

matshow(array)

FIG 1

Imshow:

plt.imshow(array)

FIG 2

Loïc Poncin
  • 511
  • 1
  • 11
  • 30

1 Answers1

30

The issue is due to interpolation.

Matplotlib matshow is a wrapper for imshow, in that it "sets origin to ‘upper’, ‘interpolation’ to ‘nearest’ and ‘aspect’ to equal."

So while matshow always uses interpolation="nearest", imshow by default has interpolation=None. Note that this is different from interpolation="none".

  • interpolation=None uses the interpolation set in the image.interpolation variable from the matplotlib rc file (which can be different in different matplotlib versions.)
  • interpolation="none" uses no interpolation, same as "nearest"

The safest way to overcome this problem is to specifically set an interpolation method in both calls

plt.matshow(array, interpolation="none")
plt.imshow(array, interpolation="none")
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • Is there a difference between `"none"` and `"nearest"`? – Mateen Ulhaq Mar 15 '20 at 02:53
  • 2
    @MateenUlhaq Yes, "For the Agg, ps and pdf backends, interpolation = 'none' works well when a big image is scaled down, while interpolation = 'nearest' works well when a small image is scaled up" from https://matplotlib.org/gallery/images_contours_and_fields/interpolation_methods.html. For other backends, it appears that they are the same ("none" will fall back to "nearest"). – Jacob Jun 01 '20 at 23:21