Why is matplotlib failing on import matplotlib.pyplot as plt

2024/11/20 16:33:32

I installed matplotlib using conda:

conda install matplotlib

The following code failed:

#!/usr/bin/env python
import matplotlib 
import matplotlib.pyplot as plt

With this error message:

"ImportError: No module named 'matplotlib.pyplot'"

I tried installing matplotlib with apt-get:

sudo apt-get install python3-matplotlib

I got the same error.

I tried loading matplotlib with ubuntu application load and got the same error.

I tried cloning from GitHub with:

git clone git://github.com/matplotlib/matplotlib.git

I got the same error.

I looked at the matplotlib directory and did not see a pyplot.py entry. I did find pyplot.py in matplotlib/lib/matplotlib. I copied it to matplotlib. The error went away, but I got another module that pyplot was trying to include. I found it in matplotlib/lib/matplotlib. I copied it to matplotlib. Got another error for another module. Copied it. Eventually I got an error for a module I could not find.

I do not know what to try next.

Answer

Normally conda isn't added to your path (it asks you during the installation if it should do that but the default is "no"), so the default python will be the systems Python 2 (you start Python 3 with python3).

You could verify this by using:

$ which python

That will return the path associated with the python command. Likely this will return the path to the systems Python 2 "installation".

For example on my Ubuntu machine the commands which python and which python3 return:

usr/bin/python   (starts Python 2.7.12)
usr/bin/python3  (starts Python 3.5.2)

While my conda installation is somewhere in the /home/michael/miniconda directory.

There are several options how you could use the conda Python:

  • Temporarily add the conda directory it to the PATH. (See for example How to add [...] to path)
  • Permanently add the conda directory to the PATH. (see for example How to add a directory to the path)
  • Use the anaconda promt which gives you a terminal with the conda directory prepended to the PATH.
https://en.xdnf.cn/q/118395.html

Related Q&A

Setting cell color of matplotlib table and save as a figure?

Im following this code a link! to save a table as the image, and I have some feature like check value in a cell then set color for a cell, but I added some code stylemap, it doesnt workimport pandas a…

Errno 111 Connection refused - Python Mininet API hosts client/server no connection?

I am new to Mininet and I am trying to find a way to use a script in python to execute a few tests using Mininet. More precisely I want to build topology and send a few xmlrpc request from one host t…

Finding the Corners of the an array of coordinates

I have a 2D array of Coordinates in Numpy.My goal is to attempt to find the corners (as if it were a square). So the :Top left: smallest x, highest y Top right: largest x, largest y bottom left: smalle…

How to make a dictionary retain its sort order?

def positive(self):total = {}final = {}for word in envir:for i in self.lst:if word in i:if word in total:total[word] += 1else:total[word] = 1final = sorted(total, reverse = True)return totalThis return…

Counting line frequencies and producing output files

With a textfile like this:a;b b;a c;d d;c e;a f;g h;b b;f b;f c;g a;b d;fHow can one read it, and produce two output text files: one keeping only the lines representing the most often occurring couple …

Check if parent dict is not empty and retrieve the value of the nested dict

Lets suppose that I have a nested dictionary which looks like that:parent_dict = { parent_key: {child_key: child_value}How can I write the following code:if parent_dict.get(parent_key) is not None and …

List combinations in defined range

I am writing parallel rainbow tables generator using parallel python and multiple machines. So far, I have it working on a single machine. It creates all possible passwords, hashes them, saves to file.…

Python turtle drawing a symbol

import turtlewin=turtle.Screen()t = turtle.Turtle() t.width(5)#The vertical and horizontal lines t.left(90) t.forward(70) t.left(90) t.forward(20)t.left(90) t.forward(60) t.left(120) t.forward(35) t.b…

Display a countdown for the python sleep function in discord embed in python

hi all I am doing one discord bot I need to send one countdown its like a cooldown embed after every request I did this code but I dont know how to add this in my embedfor i in range(60,0,-1):print(f&q…

Bypass rate limit for requests.get

I want to constantly scrape a website - once every 3-5 seconds withrequests.get(http://www.example.com, headers=headers2, timeout=35).json()But the example website has a rate limit and I want to bypass…