import matplotlib fails with "'module' object not callable" error - python

This question may appear similar to previously asked questions, but it is not.
I have a Python script with a single line:
import matplotlib
This fails with the error:
'module' object is not callable
random.py - print a random integer between 1 and 100
(followed by 3 more lines of usage of random.py)
If I start python from the command line, then type
import matplotlib
That works. I can instantiate classes from the module, plot figures and so on.
I am completely lost as to what is going on. Any clue appreciated.
Python version 2.6.6 on 64 bit x86 Linux machine.

"Matplotlib is the whole package; matplotlib.pyplot is a module in matplotlib; and pylab is a module that gets installed alongside matplotlib.
Pyplot provides the state-machine interface to the underlying object-oriented plotting library"
I took this from matplotlib manual website so You might want to take a look at.
https://matplotlib.org/faq/usage_faq.html
I dont know how you are trying you access your module but mistake is about matplotlib is not a module but its a whole package so you should call a module from inside of it. So you should call your module as one of style shown in the below
import matplotlib.pyplot
from matplotlib import figure or pyplot this part upto your module.

Matplotlib is an entire library, so if you are using import matplotlib as plt in your code, it might not work. Use 'import matplotlib.plyplot as plt' instead.

Related

How do I change the backend in CoCalc to show a matplotlib scatter graph?

I have a program that is working fine locally, but I need to use it in CoCalc on the cloud to share with a group. Everything works fine until the end, when I want to create a scatter graph, in standard way. There are a bunch of other specs, but essentially, it's just
plt.scatter(x, y, color = 'blue')
plt.show()
Nothing tricky. But in Cocalc, it tells me "UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure."
Here are the imports from the beginning of the file:
import nltk
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
I've searched for this error and tried the solutions I've found, such as importing tkinter. So, for instance, if I change the headers to
import nltk
import tkinter as tk
import pandas as pd
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import numpy as np
I get a different error: "Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running."
I've seen instructions about pip installations and such, but a) I'm not sure the installations would make everything work for my group, and b) CoCalc generally has most of the standard packages ready to go, so maybe that's not the problem?
Obviously, I'm not experienced with these issues, so help is much appreciated.
If you're using a Jupyter notebook in CoCalc, then the above will just work fine. Here's an example:
https://cocalc.com/wstein/support/matplotlib-scatter-in-jupyter
If you're writing all of your code as part of a Python program (so, e.g., .py files) and running this from a Terminal, CoCalc will also fully support Tk and this working! However, you have to start a new X11 desktop session in CoCalc by clicking +New --> "Linux Graphical X11 Desktop", then run your Python program from the terminal in the upper left. You'll see any windows that pop up in the main window off to the right.
See https://cocalc.com/wstein/support/scatter-matplotlib-x11

how to copy matplotlib figures to clipboard

After plotting the figure, I want to copy it to the clipbaoard instead of saving to figures. I searched the internet and the code does not work, either the required modules only support python 2.x or the program stopped by not well-defined functions.
I am using Python 3.6 in Windows 10. My matplotlib version is 2.0.2.
I tried 'PyGTK: copy matplotlib figure to clipboard'
and got the error: ImportError: Gtk* backend requires pygtk to be installed. I then visited www.pygtk.org/downloads.html to download pygtk/PyGObject (Windows) but it requires python 2.6 or file not found.
Can anyone show how to achieve it?
import matplotlib.pyplot as plt
a = [2,3,6,7,1]
plt.plot(a)
plt.show()

No module named matplotlib with matplotlib installed Python 2.7

I am fairly new to python as well as matplotlib and I can't get it to work. From the code :
import networkx as nx
import matplotlib.pyplot as plt
G=nx.Graph()
G.add_node("spam")
G.add_edge(1,2)
plt.show()
I get the error:
Traceback (most recent call last):
File "test.py2", line 2, in <module>
import matplotlib.pyplot as plt
ImportError: No module named matplotlib.pyplot
This occurs even though it seems to be installed in:
usr/lib/python2.7/dist-packages/matplotlib/
Do you guys have any ideas?
Thanks in advance
You have 2 pythons installed on your machine, one is the standard python that comes with MacOSX and the second is the one you installed with ports (this is the one that has matplotlib installed in its library, the one that comes with macosx does not).
/usr/bin/python
Is the standard mac python and since it doesn't have matplotlib you should always start your script with the one installed with ports.
If python your_script.py works, then change the shebang (#!) to:
\#!/usr/bin/env python
Or put the full path to the python interpreter that has the matplotlib installed in its library.
thanks for your help. It appeared the wrong Python Version was used. By using
alias python=/usr/lib/python
it was fixed, but only temporarly.
To permanently set the alias correctly, I had to edit the ~/.bash_aliases and insert:
alias python=/usr/bin/python2.7
The other python version installed was 3.0 which was set as the defualt one, but without the matplotlib library.
You can check whether usr/lib/python2.7/dist-packages (if you are pretty sure matplotlib is installed here) is in your sys.path.
>>> import sys
>>> sys.path
If you don't find the path in the list, you can add lines below before importing matplotlib.
import sys
sys.path.insert(0, '/path/to/matplotlib')

pylab cannot find reference for its modules

I have a mac OS X Yosimite and I'm using python 2.7.10 and Pycharm as my IDLE. I have pylab installed properly but I cannot use any of its modules.
When a try:
from pylab import show
(or any module) it says
ImportError: cannot import name show
But when I run just the line import pylab I get no errors!
I tried leaving that way and calling the module anyway.
pylab.imshow(...)
But I got the same error obviously. Do I have to install those modules separately?
PS: I'm almost sure the problem has nothing to do with the interpreter
Try importing from matplotlib.pyplot, rather than from pylab (this is now the recommended way to import matplotlib):
From example:
from matplotlib.pyplot import imshow
imshow()
Or:
import matplotlib.pyplot as plt
plt.imshow()

Using Matplotlib & Abaqus 6.12

I'm currently making a plug-in for abaqus using python. I've searched high and low for a solution to this problem. Ive managed to install matplotlib 1.3.1 into abaqus and its relevent modules (Six, numpy, dateutil, pytz and pyparsing) by simply copying them into the abaqus site-packages folder. It seems Abaqus 6.12 has numpy 1.4 inbuilt but this needs to be updated to 1.5 or later...I simply deleted and replaced it with 1.8.0. I encountered the 'Tkinter' error and resolve it using the following:
import sys
sys.path.append("C:\Python26\Lib\lib-tk")
import Tkinter
import matplotlib.pyplot as plt
Now I can seem to use matplotlib. However whenever I use the plt.plot() function in my plug-in, abaqus freezes. When I shut abaqus down the plot opens soon after. Im not really sure what is going on here, has anyone else experienced this problem or have a potential solution??
I hope this is clear, please ask if I need to elaborate on anything else.

Categories