Fix: plt.show() not working

If you are dealing with the “matplotlib” library, the “plt.show()” method enables users to display the visual representation of numeric figures. This method is quite useful for creating interactive plots for analyzing data. While using the “plt.show()” method, the error occurs in the terminal.

This article will address the multiple reasons and the solutions to resolve the error. The content of the post is as follows:

Let’s start the article.

Reason: “matplotlib” Library Not Installed

The “matplotlib” is the plotting library of python in python programming. It is utilized for 2D or 3D graphs from a python script. The main reason for causing the error “plt.show() not working” is that the above library is not installed in the current operating system.

Solution 1: Installed “matplotlib” Library

The best solution is to install the “matplotlib” library in the system to resolve errors. After the library installation, the “plt.show()” will successfully execute without any hurdle. For this, the installation command for Linux distributions is given here:

$ sudo apt install python3-matplotlib              #Debian/Ubuntu-based
$ sudo dnf install python3-matplotlib              #Fedora-based
$ sudo pacman -S python-matplotlib                 #Arch-based

After the successful installation of the “matplotlib” library, execute the particular method that causes the error. Apart from that, the command executes the “test.py” file:

$ python3 test.py

The “plt.show()” method has been executed in the system.

Solution 2: Install the “matplotlib” Library

Another solution for installing the “matplotlib” library is through the pip package manager. The procedure is here for installing the particular library:

Install pip Package Manager

To install the package manager “pip” on your operating system, you need to run the following script that allows you to import the module: 

$ sudo dnf install python3-pip      # Fedora
$ sudo pacman -S python3-pip        # Arch-based
$ sudo apt install python3-pip      # Ubuntu, Mint

Install “matplotlib” Library

To import the “matplotlib” library from the “pip” manager, run the script here:

$ sudo dnf install python3-matplotlib # Fedora 
$ sudo pacman -S python-matplotlib      # Arch 
$ sudo pip3 install matplotlib        # Ubuntu

The output shows that “matplotlib” is already installed in the current system.

Verify plt.show() method

To confirm the plt.show() method, run the “test.py” file on which the error is raised:

$ python3 test.py

The output ensures that the “test.py” file has been executed.

That is how the “plt.show()” method works in Linux.

Conclusion

In Linux, the error “plt.show() not working” come across due to the ‘matplotlib’ library not being installed in the operating system. To resolve the error, run the “sudo apt install python3-matplotlib” script in the terminal to install the ‘matplotlib’ library for the “plt.show()” method. This post has explained multiple solutions to tackle the error.