Solve the problem of "plt.show()" picture not being displayed and subsequent error "no module named 'tkinter'"

Problem Description

        Environment: windows|pycharm|python3.7.6

        When running the following code, I found that the image was not displayed. The plt.show function doesn't seem to be running.

import matplotlib.pyplot as plt

plt.imshow(train_image[0])

Find issues

    1 Try adding plt.show() at the end

         I saw a post on the Internet saying that it can be displayed by adding plt.show() at the end.

import matplotlib.pyplot as plt

plt.imshow(train_image[0])
plt.show()

           I found that adding it did not have any effect. Instead, a new error was reported:

“Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure”

matplotlib is using agg, which is a backend        without a GUI for drawing, so calling matplotlib.pyplot() cannot draw.

        Keep looking for solutions. I saw another post saying that just add the following two sentences and replace agg with tkagg.

import matplotlib
matplotlib.use('TkAgg')

        But a new error occurred:

 No module named 'tkinter'

(((φ(◎ロ◎;)φ))), why are there so many errors?

  2 Reinstall the tkinter library

            I read some posts on the Internet, saying that the tcl library is missing.

            Tcl is a library that comes with the system and is generally not missing. If an error occurs, there will be no 'tcl' folder in the root directory of Python.

             I took a look at the root directory of my python, and sure enough, there was no such 'tcl' folder.

            At the time

               (1) Open the python installation package.

                (2) Click modify

      (3) Select items 1, 2, and 4

 Notice! The blogger tried to install tcl directly first, which is the third item, but it still failed after repair.

It was almost installed before. The environment indicates that the system has installed the plug-ins required for tkinter, but some things may have been deleted by mistake later and cannot be used. However, when the system recognizes the identifier, it shows that the installation has been successful, and it will not be installed again. . So it needs to be fixed manually.

So choose to repair items 1, 2, and 4 first.

(4) Re-open the python installation package and select "tcl/tk and IDLE

 Then rerun the code and the image is displayed successfully!

Summarize

        When using plt.imshow() the picture is not displayed. You can try the following steps to solve the problem

        1 Add the sentence after plt.imshow(), plt.show

plt.imshow(train_images[0])
plt.show()

       2 If it still does not display, you need to re-click the tkinter library

           (1) Click on the python installation package, select modify, and first repair 124 items.

          (2) Then continue to repair the tcl file

        (3) When the tcl folder appears in the root directory of python, it proves that the repair is successful. to display pictures

    

Guess you like

Origin blog.csdn.net/weixin_51286347/article/details/127689050