I went through the documentation of both but the documentation does not specify for a specific use case for both. I have also found that sometimes the imshow() property on its own displays the figures but sometimes the show() property needs to be used to display the figure. In the documentation , the imshow() property mentions about displaying the image on the axes but the word "axes" is missing from the show() property. What does the word axes more specifically refer to?
Except for both, imshow
and show
having the word "show" in them, they have nothing in common.
imshow
is a plotting command. It is hence on the same level as other plotting commands like plot
, scatter
, pcolor
, contour
etc. Those plotting command will produce some graphical data representation inside an axes. An axes is essentially the rectangle you see around your plot.
plt.show()
is the command you need to give at the end to produce the graphical output. It is the function which makes the figure which has previously been produced by one or more plotting commands to actually show up on screen - hence the name "show".
So you usually have
import matplotlib.pyplot as plt
<plotting command>
plt.show()
E.g.
plt.scatter(...)
plt.show()
or
plt.imshow(...)
plt.show()
Now in some cases, depending on the environment where you run your code, the use of plt.show()
is not needed. This is because the environment is aware of there being a matplotlib plot being generated and it will therefore generate its output automatically for you without the need to call plt.show()
. This would be mainly inside of IPython sessions or Jupyter notebooks.
In summary: In order to produce a plot with an image, you call plt.imshow(..)
. Whether or not you then need to call plt.show()
to invoke the representation on screen depends on the environment. In case you do not want to show the image on screen, but e.g. save it to a file instead, you would omit plt.show()
and call
plt.imshow(...)
plt.savefig(...)
instead.